/
address.go
49 lines (40 loc) · 1.25 KB
/
address.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//go:generate go run generator/main.go
// Package libaddress is a library that validates address using data
// generated from Google's Address Data Service
package libaddress
import "fmt"
// Address represents a valid address made up of its
// child components
type Address struct {
Country string
Name string
Organization string
StreetAddress []string
DependentLocality string
Locality string
AdministrativeArea string
PostCode string
SortingCode string
}
// New creates a new unvalidated address. The validity of the address
// should be checked using the validator.
func New(fields ...func(*Address)) Address {
address := Address{}
for _, f := range fields {
f(&address)
}
return address
}
// NewValid creates a new Address. If the address is invalid, an error
// is returned. In the case where an error is returned, the error is a
// hashicorp/go-multierror (https://github.com/hashicorp/go-multierror).
// You can use a type switch to get a list of validation errors for
// the address.
func NewValid(fields ...func(*Address)) (Address, error) {
address := New(fields...)
err := Validate(address)
if err != nil {
return address, fmt.Errorf("%s", err)
}
return address, nil
}