chore(go): improve address definition
This commit is contained in:
parent
adc39dc755
commit
1c56398e21
@ -1,26 +1,56 @@
|
||||
package main
|
||||
|
||||
import parser "github.com/openvenues/gopostal/parser"
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
parser "github.com/openvenues/gopostal/parser"
|
||||
)
|
||||
|
||||
type Address struct {
|
||||
Country string
|
||||
State string
|
||||
City string
|
||||
Neighborhood string
|
||||
Road string
|
||||
HouseNumber string
|
||||
House string
|
||||
Unit string
|
||||
PostalCode string
|
||||
ID string `json:"id"`
|
||||
Country string `json:"country"`
|
||||
State string `json:"state"`
|
||||
City string `json:"city"`
|
||||
Neighborhood string `json:"neighborhood"`
|
||||
Road string `json:"road"`
|
||||
HouseNumber string `json:"house_number"`
|
||||
House string `json:"house"`
|
||||
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 {
|
||||
// log.Printf("Address to parse: %s", address)
|
||||
components := parser.ParseAddress(address)
|
||||
return newFromComponents(components)
|
||||
// log.Printf("Address components: %s", components)
|
||||
return newFromComponents(address, components)
|
||||
}
|
||||
|
||||
func newFromComponents(components []parser.ParsedComponent) Address {
|
||||
address := Address{}
|
||||
func newFromComponents(raw string, components []parser.ParsedComponent) Address {
|
||||
address := Address{Raw: raw}
|
||||
for _, component := range components {
|
||||
switch component.Label {
|
||||
case "house":
|
||||
@ -47,5 +77,7 @@ func newFromComponents(components []parser.ParsedComponent) Address {
|
||||
address.Country = component.Value
|
||||
}
|
||||
}
|
||||
address.ID = address.id()
|
||||
// log.Printf("Address parsed: %s", address)
|
||||
return address
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user