Skip to content

Commit b5b7e71

Browse files
committed
Adds libpostal raw result
1 parent 389aa48 commit b5b7e71

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ without the need for the libpostal library to be included as a dependency in the
99
## Quickstart
1010

1111
```
12-
docker run -p 8080:8080 gosom/address-parser-go-rest:v1.0.1
12+
docker run -p 8080:8080 gosom/address-parser-go-rest
1313
```
1414

1515
This will take some time to load
@@ -30,7 +30,35 @@ curl -X 'POST' \
3030
Response:
3131

3232
```
33-
{"house_number":"48","road":"Leicester Square","postcode":"Wc2h 7Lu","city":"London","country":"United Kingdom"}
33+
{
34+
"house_number": "48",
35+
"road": "Leicester Square",
36+
"postcode": "Wc2h 7Lu",
37+
"city": "London",
38+
"country": "United Kingdom",
39+
"components": [
40+
{
41+
"label": "house_number",
42+
"value": "48"
43+
},
44+
{
45+
"label": "road",
46+
"value": "Leicester Square"
47+
},
48+
{
49+
"label": "city",
50+
"value": "London"
51+
},
52+
{
53+
"label": "postcode",
54+
"value": "Wc2h 7Lu"
55+
},
56+
{
57+
"label": "country",
58+
"value": "United Kingdom"
59+
}
60+
]
61+
}
3462
```
3563

3664
[swagger documentation](http://localhost:8080/docs/)

addressparser/addressparser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ type Address struct {
4646
Country string `json:"country,omitempty"`
4747
// currently only used for appending “West Indies” after the country name, a pattern frequently used in the English-speaking Caribbean e.g. “Jamaica, West Indies”
4848
WorldRegion string `json:"world_region,omitempty"`
49+
// Components is the raw response from libpostal
50+
Components []AddressComponent `json:"components"`
51+
}
52+
53+
// AddressComponent is a struct for an address component
54+
type AddressComponent struct {
55+
// Label is the label of the component as defined by libpostal
56+
Label string `json:"label"`
57+
// Value is the value of the component as defined by libpostal
58+
Value string `json:"value"`
4959
}
5060

5161
// AddressParserInput is a struct for the input to the address parser

addressparser/libpostal/libpostal.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ func (o *libPostalParser) Parse(input addressparser.AddressParserInput) (address
8484
default:
8585
o.log.Warn("Unknown component", "component", components[i].Label)
8686
}
87+
88+
component := addressparser.AddressComponent{
89+
Label: components[i].Label,
90+
Value: components[i].Value,
91+
}
92+
address.Components = append(address.Components, component)
8793
}
8894
address.HouseNumber = strings.TrimSpace(address.HouseNumber)
8995
return address, nil

docs/docs.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ const docTemplate = `{
9494
"description": "these are usually boroughs or districts within a city that serve some official purpose e.g. \"Brooklyn\" or \"Hackney\" or \"Bratislava IV\"",
9595
"type": "string"
9696
},
97+
"components": {
98+
"description": "Components is the raw response from libpostal",
99+
"type": "array",
100+
"items": {
101+
"$ref": "#/definitions/addressparser.AddressComponent"
102+
}
103+
},
97104
"country": {
98105
"description": "sovereign nations and their dependent territories, anything with an ISO-3166 code.",
99106
"type": "string"
@@ -164,19 +171,35 @@ const docTemplate = `{
164171
}
165172
}
166173
},
174+
"addressparser.AddressComponent": {
175+
"type": "object",
176+
"properties": {
177+
"label": {
178+
"description": "Label is the label of the component as defined by libpostal",
179+
"type": "string"
180+
},
181+
"value": {
182+
"description": "Value is the value of the component as defined by libpostal",
183+
"type": "string"
184+
}
185+
}
186+
},
167187
"addressparser.AddressParserInput": {
168188
"type": "object",
169189
"required": [
170190
"address"
171191
],
172192
"properties": {
173193
"address": {
194+
"description": "the address to parse",
174195
"type": "string"
175196
},
176197
"country": {
198+
"description": "the country of the address. Leave empty if you don't know",
177199
"type": "string"
178200
},
179201
"language": {
202+
"description": "the language of the address. Leave empty if you don't know",
180203
"type": "string"
181204
},
182205
"title_case": {

docs/swagger.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
"description": "these are usually boroughs or districts within a city that serve some official purpose e.g. \"Brooklyn\" or \"Hackney\" or \"Bratislava IV\"",
8888
"type": "string"
8989
},
90+
"components": {
91+
"description": "Components is the raw response from libpostal",
92+
"type": "array",
93+
"items": {
94+
"$ref": "#/definitions/addressparser.AddressComponent"
95+
}
96+
},
9097
"country": {
9198
"description": "sovereign nations and their dependent territories, anything with an ISO-3166 code.",
9299
"type": "string"
@@ -157,19 +164,35 @@
157164
}
158165
}
159166
},
167+
"addressparser.AddressComponent": {
168+
"type": "object",
169+
"properties": {
170+
"label": {
171+
"description": "Label is the label of the component as defined by libpostal",
172+
"type": "string"
173+
},
174+
"value": {
175+
"description": "Value is the value of the component as defined by libpostal",
176+
"type": "string"
177+
}
178+
}
179+
},
160180
"addressparser.AddressParserInput": {
161181
"type": "object",
162182
"required": [
163183
"address"
164184
],
165185
"properties": {
166186
"address": {
187+
"description": "the address to parse",
167188
"type": "string"
168189
},
169190
"country": {
191+
"description": "the country of the address. Leave empty if you don't know",
170192
"type": "string"
171193
},
172194
"language": {
195+
"description": "the language of the address. Leave empty if you don't know",
173196
"type": "string"
174197
},
175198
"title_case": {

docs/swagger.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ definitions:
1515
description: these are usually boroughs or districts within a city that serve
1616
some official purpose e.g. "Brooklyn" or "Hackney" or "Bratislava IV"
1717
type: string
18+
components:
19+
description: Components is the raw response from libpostal
20+
items:
21+
$ref: '#/definitions/addressparser.AddressComponent'
22+
type: array
1823
country:
1924
description: sovereign nations and their dependent territories, anything with
2025
an ISO-3166 code.
@@ -80,13 +85,25 @@ definitions:
8085
West Indies”
8186
type: string
8287
type: object
88+
addressparser.AddressComponent:
89+
properties:
90+
label:
91+
description: Label is the label of the component as defined by libpostal
92+
type: string
93+
value:
94+
description: Value is the value of the component as defined by libpostal
95+
type: string
96+
type: object
8397
addressparser.AddressParserInput:
8498
properties:
8599
address:
100+
description: the address to parse
86101
type: string
87102
country:
103+
description: the country of the address. Leave empty if you don't know
88104
type: string
89105
language:
106+
description: the language of the address. Leave empty if you don't know
90107
type: string
91108
title_case:
92109
description: if true then the responses will be title Cased. Default behavior

0 commit comments

Comments
 (0)