Documentation ¶
Overview ¶
Package address is a library that validates and formats addresses using data generated from Google's Address Data Service.
Code generated by address. DO NOT EDIT.
Index ¶
- Variables
- func Validate(address Address) error
- func WithAdministrativeArea(administrativeArea string) func(*Address)
- func WithCountry(countryCode string) func(*Address)
- func WithDependentLocality(dependentLocality string) func(*Address)
- func WithLocality(locality string) func(*Address)
- func WithName(name string) func(*Address)
- func WithOrganization(organization string) func(*Address)
- func WithPostCode(postCode string) func(*Address)
- func WithSortingCode(sortingCode string) func(*Address)
- func WithStreetAddress(streetAddress []string) func(*Address)
- type Address
- type Addresses
- type AdministrativeAreaData
- type CountryData
- type CountryList
- type CountryListItem
- type DefaultFormatter
- type DependentLocalityData
- type ErrMissingRequiredFields
- type ErrUnsupportedFields
- type ExactMatcher
- type Field
- type FieldName
- type HTMLOutputter
- type LocalityData
- type Outputter
- type PostCodeMatcher
- type PostCodeRange
- type PostCodeRegexData
- type PostalLabelFormatter
- type RegexMatcher
- type StringOutputter
- type Territory
- type Zone
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidAdministrativeArea = errors.New("invalid administrative area")
ErrInvalidAdministrativeArea indicates that the administrative area is invalid. This is usually due to the country having a pre-determined list of administrative areas and the value does not match any of the keys in the list of administrative areas.
var ErrInvalidCountryCode = errors.New("invalid country code")
ErrInvalidCountryCode indicate that the country code used to create an address is invalid.
var ErrInvalidDependentLocality = errors.New("invalid dependent locality")
ErrInvalidDependentLocality indicates that the dependent locality is invalid. This is usually due to the country having a pre-determined list of dependent localities and the value does not match any of the keys in the list of dependent localities.
var ErrInvalidLocality = errors.New("invalid locality")
ErrInvalidLocality indicates that the locality is invalid. This is usually due to the country having a pre-determined list of localities and the value does not match any of the keys in the list of localities.
var ErrInvalidPostCode = errors.New("invalid post code")
ErrInvalidPostCode indicates that the post code did not valid using the regular expressions of the country.
Functions ¶
func Validate ¶
Validate checks and address to determine if it is valid. If you want to create valid addresses, the `address.NewValid()` function does it in one call.
func WithAdministrativeArea ¶
WithAdministrativeArea sets the administrative area (commonly known as the state) of an address. If the country of the address has a list of administrative area, then the key of the administrative area should used, otherwise, the validation will fail.
func WithCountry ¶
WithCountry sets the country code of an address. The country code must be an ISO 3166-1 country code.
func WithDependentLocality ¶
WithDependentLocality sets the dependent locality (commonly known as the suburb) of an address. If the country of the address has a list of dependent localities, then the key of the dependent locality should be used, otherwise, the validation will fail.
func WithLocality ¶
WithLocality sets the locality (commonly known as the city) of an address. If the country of the address has a list of localities, then the key of the locality should be used, otherwise, the validation will fail.
func WithOrganization ¶
WithOrganization sets the addressee's organization of an address.
func WithPostCode ¶
WithPostCode sets the post code of an address.
func WithSortingCode ¶
WithSortingCode sets the sorting code of an address.
func WithStreetAddress ¶
WithStreetAddress sets the street address of an address. The street address is a slice of strings, with each element representing an address line.
Types ¶
type Address ¶
type Address struct { ID string `json:"id,omitempty" db:"id"` UserID string `json:"user_id,omitempty" db:"user_id"` Country string `json:"country" db:"country"` Name string `json:"name" db:"name"` Organization string `json:"organization"` StreetAddress Addresses `json:"street" db:"street"` DependentLocality string `json:"dependent_locality" db:"dependent_locality"` Locality string `json:"locality" db:"locality"` AdministrativeArea string `json:"administrative_area" db:"administrative_area"` PostCode string `json:"postal_code" db:"postal_code"` SortingCode string `json:"sorting_code" db:"sorting_code"` CreatedAt time.Time `json:"created_at,omitempty" db:"created_at"` UpdatedAt time.Time `json:"updated_at,omitempty" db:"updated_at"` DeletedAt sql.NullTime `json:"deleted_at,omitempty" db:"deleted_at"` }
Address represents a valid address made up of its child components.
func New ¶
New creates a new unvalidated address. The validity of the address should be checked using the validator.
func NewValid ¶
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.
type AdministrativeAreaData ¶
type AdministrativeAreaData struct { ID string Name string Localities []LocalityData }
AdministrativeAreaData contains the name and ID of and administrative area. The ID must be passed to WithAdministrativeArea() when creating an address. The name is useful for displaying to the end user.
type CountryData ¶
type CountryData struct { Format string LatinizedFormat string Required []Field Allowed []Field DefaultLanguage string AdministrativeAreaNameType FieldName LocalityNameType FieldName DependentLocalityNameType FieldName PostCodeNameType FieldName PostCodeRegex PostCodeRegexData AdministrativeAreas map[string][]AdministrativeAreaData }
CountryData contains the address data for a country. The AdministrativeAreas field contains a list of nested subdivisions (administrative areas, localities and dependent localities) grouped by their translated languages. They are also sorted according to the sort order of the languages they are in.
func GetCountry ¶
func GetCountry(countryCode string) CountryData
GetCountry returns address information for a given country.
type CountryList ¶
type CountryList []CountryListItem
CountryList contains a list of countries that can be used to create addresses.
func (CountryList) Bytes ¶
func (c CountryList) Bytes(i int) []byte
Bytes returns a country name in bytes. This is used for sorting the countries and would not generally be used in client code.
func (CountryList) Len ¶
func (c CountryList) Len() int
Len returns the number of countries in the list. This is used for sorting the countries and would not generally be used in client code.
func (CountryList) Swap ¶
func (c CountryList) Swap(i, j int)
Swap swaps 2 countries in the list. This is used for sorting the countries and would not generally be used in client code.
type CountryListItem ¶
CountryListItem represents a single country, containing the ISO 3166-1 code and the name of the country.
func ListCountries ¶
func ListCountries(language string) []CountryListItem
ListCountries returns a list of countries that can be used to create addresses. Language must be a valid ISO 639-1 language code such as: en, jp, zh, etc. If the language does not have any translations or is invalid, then English is used as the fallback language. The returned list of countries is sorted according to the chosing language.
type DefaultFormatter ¶
DefaultFormatter formats an address using the country's address format and includes the name of the country. If Latinize is set to true, in countries where a latinized address format is provided, the latinized format is used.
func (DefaultFormatter) Format ¶
func (d DefaultFormatter) Format(address Address, language string) string
Format formats an address. The language must be a valid ISO 639-1 language code. It is used to convert the keys in administrative areas, localities and dependent localities into their actual names. If the provided language does not have any translations, it falls back to the default language used by the country.
type DependentLocalityData ¶
DependentLocalityData contains the name and ID of and administrative area. The ID must be passed to WithDependentLocalityData() when creating an address. The name is useful for displaying to the end user.
type ErrMissingRequiredFields ¶
type ErrMissingRequiredFields struct { Fields []Field // contains filtered or unexported fields }
ErrMissingRequiredFields indicates the a required address field is missing. The Fields field can be used to get a list of missing fields.
func (ErrMissingRequiredFields) Error ¶
func (e ErrMissingRequiredFields) Error() string
type ErrUnsupportedFields ¶
type ErrUnsupportedFields struct { Fields []Field // contains filtered or unexported fields }
ErrUnsupportedFields indicates that an address field as provided, but it is not supported by the address format of the country. The Fields field can be used to get a list of unsupported fields.
func (ErrUnsupportedFields) Error ¶
func (e ErrUnsupportedFields) Error() string
type ExactMatcher ¶
type ExactMatcher struct { Matches []string Ranges []PostCodeRange }
ExactMatcher matches post codes exactly in the list defined in the Matches field. If the post code is numeric, it's also possible to define a slice of ranges using the Ranges fiel.d
func (ExactMatcher) Match ¶
func (m ExactMatcher) Match(postCode string) bool
Match checks to see if the post code matches a post code defined in Matches or if it is within a range defined in Ranges.
type Field ¶
type Field int
Field is an address field type.
func (Field) Key ¶
Key returns the corresponding one-letter abbreviation used by Google to refer to address fields. This is useful for parsing the address format for a country. See https://github.com/googlei18n/libaddressinput/wiki/AddressValidationMetadata for more information.
type FieldName ¶
type FieldName int
FieldName is the name to be used when referring to a field. For example, in India, the post code is called PIN Code instead of Post Code. The field name allows you to display the appropriate form labels to the user.
type HTMLOutputter ¶
type HTMLOutputter struct{}
HTMLOutputter outputs the formatted address as an HTML fragments. The address fields are annotated using the class attribute and `<br>`s are used for new lines.
func (HTMLOutputter) TransformFormat ¶
func (h HTMLOutputter) TransformFormat(format string, upper map[Field]struct{}) string
TransformFormat transforms an address format in Google's format into a HTML template. The upper map is used to determine which fields should be converted to UPPERCASE.
type LocalityData ¶
type LocalityData struct { ID string Name string DependentLocalities []DependentLocalityData }
LocalityData contains the name and ID of and administrative area. The ID must be passed to WithLocalityData() when creating an address. The name is useful for displaying to the end user.
type Outputter ¶
Outputter defines an interface to transform an address format in Google's format into a Go template that is merged with the address data to produce a formatted address.
type PostCodeMatcher ¶
PostCodeMatcher returns a boolean signaling whether the post code matched or not.
type PostCodeRange ¶
PostCodeRange defines a range of numeric post codes, inclusive of the Start and End.
type PostCodeRegexData ¶
type PostCodeRegexData struct { Regex string SubdivisionRegex map[string]PostCodeRegexData }
PostCodeRegexData contains regular expressions for validating post codes for a given country. If the country has subdivisions (administrative areas, localities and dependent localities), the SubdivisionRegex field may contain further regular expressions to Validate the post code.
type PostalLabelFormatter ¶
PostalLabelFormatter formats an address for postal labels. It uppercases address fields as required by the country's addressing standards. If the address it in the same country as the origin country, the country is omitted. The country name is added to the address, both in the language of the origin country as well as English, following recommendations of the Universal Postal Union, to avoid difficulties in transit. The OriginCountryCode field should be set to the ISO 3166-1 country code of the originating country. If Latinize is set to true, in countries where a latinized address format is provided, the latinized format is used.
func (PostalLabelFormatter) Format ¶
func (f PostalLabelFormatter) Format(address Address, language string) string
Format formats an address. The language must be a valid ISO 639-1 language code. It is used to convert the keys in administrative areas, localities and dependent localities into their actual names. If the provided language does not have any translations, it falls back to the default language used by the country.
type RegexMatcher ¶
RegexMatcher defines a post code matcher that uses a regular expression.
func (RegexMatcher) Match ¶
func (m RegexMatcher) Match(postCode string) bool
Match returns whether the post code is matched by the regular expression defined in the matcher.
type StringOutputter ¶
type StringOutputter struct{}
StringOutputter outputs the formatted address as a string and `\n`s are used for new lines.
func (StringOutputter) TransformFormat ¶
func (s StringOutputter) TransformFormat(format string, upper map[Field]struct{}) string
TransformFormat transforms an address format in Google's format into a string template. The upper map is used to determine which fields should be converted to UPPERCASE.
type Territory ¶
type Territory struct { Country string AdministrativeArea string Locality string DependentLocality string IncludedPostCodes PostCodeMatcher ExcludedPostCodes PostCodeMatcher }
Territory is a rule within a zone. It is able to match the following fields: - Country: An ISO 3166-1 country code - AdministrativeArea: The administrative area name. If the country has a list of pre-defined administrative areas, use the key of the administrative area. - Locality: The locality name. If the country has a list of pre-defined localities, use the key of the locality. - DependentLocality: The dependent locality name. If the country has a list of pre-defined dependent localities, use the key of the locality. - IncludedPostCodes: A PostCodeMatcher that includes the address within the territory if the post code matches. - ExcludedPostCodes: A PostCodeMatcher that excludes the address from the territory if the post code matches.