Documentation ¶
Overview ¶
Package address handles address representation, validation and formatting.
Index ¶
- Constants
- func CheckCountryCode(countryCode string) bool
- func GetCountryCodes() []string
- func GetCountryNames() map[string]string
- func GetFormats() map[string]Format
- type Address
- type Field
- type Format
- func (f Format) CheckPostalCode(postalCode string) bool
- func (f Format) CheckRegion(region string) bool
- func (f Format) CheckRequired(field Field, value string) bool
- func (f Format) IsRequired(field Field) bool
- func (f *Format) PostalCodeValidationPattern() string
- func (f Format) SelectLayout(locale Locale) string
- func (f Format) SelectRegions(locale Locale) RegionMap
- type FormatHandler
- type Formatter
- type Locale
- type LocalityType
- type PostalCodeType
- type RegionMap
- type RegionType
- type SublocalityType
Examples ¶
Constants ¶
const CLDRVersion = "46.0.0"
CLDRVersion is the CLDR version from which the country list is derived.
Variables ¶
This section is empty.
Functions ¶
func CheckCountryCode ¶
CheckCountryCode checks whether the given country code is valid.
An empty country code is considered valid.
func GetCountryCodes ¶
func GetCountryCodes() []string
GetCountryCodes returns all known country codes.
func GetCountryNames ¶
GetCountryNames returns all known country names, keyed by country code.
func GetFormats ¶
GetFormats returns all known address formats, keyed by country code.
The ZZ address format represents the generic fallback.
Types ¶
type Address ¶
type Address struct { Line1 string `json:"line1"` Line2 string `json:"line2"` Line3 string `json:"line3"` // Sublocality is the neighborhood/suburb/district. Sublocality string `json:"sublocality"` // Locality is the city/village/post town. Locality string `json:"locality"` // Region is the state/province/prefecture. // An ISO code is used when available. Region string `json:"region"` // PostalCode is the postal/zip/pin code. PostalCode string `json:"postal_code"` // CountryCode is the two-letter code as defined by CLDR. CountryCode string `json:"country"` }
Address represents an address.
type Format ¶
type Format struct { Locale Locale `json:"locale,omitempty"` Layout string `json:"layout,omitempty"` LocalLayout string `json:"local_layout,omitempty"` Required []Field `json:"required,omitempty"` Defaults map[Field]string `json:"defaults,omitempty"` SublocalityType SublocalityType `json:"sublocality_type,omitempty"` LocalityType LocalityType `json:"locality_type,omitempty"` RegionType RegionType `json:"region_type,omitempty"` PostalCodeType PostalCodeType `json:"postal_code_type,omitempty"` PostalCodePattern string `json:"postal_code_pattern,omitempty"` ShowRegionID bool `json:"show_region_id,omitempty"` Regions RegionMap `json:"regions,omitempty"` LocalRegions RegionMap `json:"local_regions,omitempty"` }
Format represents an address format.
func (Format) CheckPostalCode ¶
CheckPostalCode checks whether the given postal code is valid.
An empty postal code is considered valid.
func (Format) CheckRegion ¶
CheckRegion checks whether the given region is valid.
An empty region is considered valid.
func (Format) CheckRequired ¶
CheckRequired checks whether a required field is valid (non-blank).
Non-required fields are considered valid even if they're blank.
func (Format) IsRequired ¶
IsRequired returns whether the given field is required.
func (*Format) PostalCodeValidationPattern ¶ added in v1.2.0
PostalCodeValidationPattern returns the full regex pattern for validating the postal code.
func (Format) SelectLayout ¶
SelectLayout selects the correct layout for the given locale.
func (Format) SelectRegions ¶
SelectRegions selects the correct regions for the given locale.
type FormatHandler ¶
type FormatHandler struct{}
FormatHandler is an HTTP handler for serving address formats.
Response size is ~47kb, or ~14kb if gzip compression is used.
The locale can be provided either as a query string (?locale=fr) or as a header (Accept-Language:fr). Defaults to "en".
func (*FormatHandler) ServeHTTP ¶
func (h *FormatHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface.
type Formatter ¶
type Formatter struct { // CountryMapper maps country codes to country names. // Can be used to retrieve country names from another (localized) source. // Defaults to a function that uses English country names included in the package. CountryMapper func(countryCode string, locale Locale) string // NoCountry turns off displaying the country name. // Defaults to false. NoCountry bool // WrapperElement is the wrapper HTML element. // Defaults to "p". WrapperElement string // WrapperClass is the wrapper HTML class. // Defaults to "address". WrapperClass string // contains filtered or unexported fields }
Formatter formats addresses for display.
func NewFormatter ¶
NewFormatter creates a new formatter for the given locale.
func (*Formatter) Format ¶
Format formats the given address.
Example ¶
package main import ( "fmt" "github.com/bojanz/address" ) func main() { locale := address.NewLocale("en") formatter := address.NewFormatter(locale) addr := address.Address{ Line1: "1098 Alta Ave", Locality: "Mountain View", Region: "CA", PostalCode: "94043", CountryCode: "US", } fmt.Println(formatter.Format(addr)) addr = address.Address{ Line1: "幸福中路", Sublocality: "新城区", Locality: "西安市", Region: "SN", PostalCode: "710043", CountryCode: "CN", } locale = address.NewLocale("zh") formatter = address.NewFormatter(locale) formatter.NoCountry = true formatter.WrapperElement = "div" formatter.WrapperClass = "postal-address" fmt.Println(formatter.Format(addr)) }
Output: <p class="address" translate="no"> <span class="line1">1098 Alta Ave</span><br> <span class="locality">Mountain View</span>, <span class="region">CA</span> <span class="postal-code">94043</span><br> <span class="country" data-value="US">United States</span> </p> <div class="postal-address" translate="no"> <span class="postal-code">710043</span><br> <span class="region">陕西省</span><span class="locality">西安市</span><span class="sublocality">新城区</span><br> <span class="line1">幸福中路</span> </div>
type Locale ¶
Locale represents a Unicode locale identifier.
func NewLocale ¶
NewLocale creates a new Locale from its string representation.
Example ¶
package main import ( "fmt" "github.com/bojanz/address" ) func main() { firstLocale := address.NewLocale("en-US") fmt.Println(firstLocale) fmt.Println(firstLocale.Language, firstLocale.Territory) // Locale IDs are normalized. secondLocale := address.NewLocale("sr_rs_latn") fmt.Println(secondLocale) fmt.Println(secondLocale.Language, secondLocale.Script, secondLocale.Territory) }
Output: en-US en US sr-Latn-RS sr Latn RS
func (Locale) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*Locale) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
type LocalityType ¶
type LocalityType uint8
LocalityType represents the locality type.
const ( LocalityTypeCity LocalityType = iota LocalityTypeDistrict LocalityTypePostTown LocalityTypeSuburb LocalityTypeTownCity )
func (LocalityType) MarshalText ¶
func (l LocalityType) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface.
func (LocalityType) String ¶
func (l LocalityType) String() string
String returns the string representation of l.
func (*LocalityType) UnmarshalText ¶
func (l *LocalityType) UnmarshalText(b []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface.
type PostalCodeType ¶
type PostalCodeType uint8
PostalCodeType represents the postal code type.
const ( PostalCodeTypePostal PostalCodeType = iota PostalCodeTypeEir PostalCodeTypePin PostalCodeTypeZip )
func (PostalCodeType) MarshalText ¶
func (p PostalCodeType) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface.
func (PostalCodeType) String ¶
func (p PostalCodeType) String() string
String returns the string representation of p.
func (*PostalCodeType) UnmarshalText ¶
func (p *PostalCodeType) UnmarshalText(b []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface.
type RegionMap ¶
type RegionMap struct {
// contains filtered or unexported fields
}
RegionMap represents a read-only ordered map of regions.
func NewRegionMap ¶
NewRegionMap creates a new region map from the given pairs.
func (RegionMap) MarshalJSON ¶
type RegionType ¶
type RegionType uint8
RegionType represents the region type.
const ( RegionTypeProvince RegionType = iota RegionTypeArea RegionTypeCanton RegionTypeCounty RegionTypeDepartment RegionTypeDistrict RegionTypeDoSi RegionTypeEmirate RegionTypeIsland RegionTypeParish RegionTypePrefecture RegionTypeRegion RegionTypeState )
func (RegionType) MarshalText ¶
func (r RegionType) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface.
func (RegionType) String ¶
func (r RegionType) String() string
String returns the string representation of r.
func (*RegionType) UnmarshalText ¶
func (r *RegionType) UnmarshalText(b []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface.
type SublocalityType ¶
type SublocalityType uint8
SublocalityType represents the sublocality type.
const ( SublocalityTypeSuburb SublocalityType = iota SublocalityTypeDistrict SublocalityTypeNeighborhood SublocalityTypeVillageTownship SublocalityTypeTownland )
func (SublocalityType) MarshalText ¶
func (s SublocalityType) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface.
func (SublocalityType) String ¶
func (s SublocalityType) String() string
String returns the string representation of s.
func (*SublocalityType) UnmarshalText ¶
func (s *SublocalityType) UnmarshalText(b []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface.