[WIP] Implement server to parse address #1
@ -1,26 +1,56 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import parser "github.com/openvenues/gopostal/parser"
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
parser "github.com/openvenues/gopostal/parser"
|
||||||
|
)
|
||||||
|
|
||||||
type Address struct {
|
type Address struct {
|
||||||
Country string
|
ID string `json:"id"`
|
||||||
State string
|
Country string `json:"country"`
|
||||||
City string
|
State string `json:"state"`
|
||||||
Neighborhood string
|
City string `json:"city"`
|
||||||
Road string
|
Neighborhood string `json:"neighborhood"`
|
||||||
HouseNumber string
|
Road string `json:"road"`
|
||||||
House string
|
HouseNumber string `json:"house_number"`
|
||||||
Unit string
|
House string `json:"house"`
|
||||||
PostalCode string
|
Unit string `json:"unit"`
|
||||||
|
PostalCode string `json:"postal_code"`
|
||||||
|
Raw string `json:"raw_address"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Address) String() string {
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"%s, %s, %s %s. %s, %s, %s, %s.",
|
||||||
|
a.Road,
|
||||||
|
a.HouseNumber,
|
||||||
|
a.Unit,
|
||||||
|
a.House,
|
||||||
|
a.Neighborhood,
|
||||||
|
a.City,
|
||||||
|
a.State,
|
||||||
|
a.PostalCode,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Address) id() string {
|
||||||
|
hasher := sha1.New()
|
||||||
|
hasher.Write([]byte(a.String()))
|
||||||
|
return hex.EncodeToString(hasher.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAddress(address string) Address {
|
func parseAddress(address string) Address {
|
||||||
|
// log.Printf("Address to parse: %s", address)
|
||||||
components := parser.ParseAddress(address)
|
components := parser.ParseAddress(address)
|
||||||
return newFromComponents(components)
|
// log.Printf("Address components: %s", components)
|
||||||
|
return newFromComponents(address, components)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFromComponents(components []parser.ParsedComponent) Address {
|
func newFromComponents(raw string, components []parser.ParsedComponent) Address {
|
||||||
address := Address{}
|
address := Address{Raw: raw}
|
||||||
for _, component := range components {
|
for _, component := range components {
|
||||||
switch component.Label {
|
switch component.Label {
|
||||||
case "house":
|
case "house":
|
||||||
@ -47,5 +77,7 @@ func newFromComponents(components []parser.ParsedComponent) Address {
|
|||||||
address.Country = component.Value
|
address.Country = component.Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
address.ID = address.id()
|
||||||
|
// log.Printf("Address parsed: %s", address)
|
||||||
return address
|
return address
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user