Skip to content

Commit

Permalink
Allow address formats to declare default values.
Browse files Browse the repository at this point in the history
Use case: Singapore (SG) addresses have a locality field, but
it's always supposed to contain "Singapore".
  • Loading branch information
bojanz committed Jan 16, 2024
1 parent b722152 commit 6564da1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
25 changes: 13 additions & 12 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ func (a Address) IsEmpty() bool {

// Format represents an address 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"`
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"`
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"`
}

// IsRequired returns whether the given field is required.
Expand Down
7 changes: 5 additions & 2 deletions formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -1439,8 +1439,11 @@ var formats = map[string]Format{
PostalCodePattern: `\d{3} ?\d{2}`,
},
"SG": {
Layout: "%1\n%2\n%3\n%L %P",
Required: []Field{FieldLine1, FieldLocality, FieldPostalCode},
Layout: "%1\n%2\n%3\n%L %P",
Required: []Field{FieldLine1, FieldLocality, FieldPostalCode},
Defaults: map[Field]string{
FieldLocality: "Singapore",
},
PostalCodePattern: `\d{6}`,
},
"SH": {
Expand Down
22 changes: 12 additions & 10 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ func (h *FormatHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
locale := h.getLocale(r)
// Preselecting the layout and regions reduces HTTP request size by ~20%.
type localizedFormat struct {
Locale string `json:"locale,omitempty"`
Layout string `json:"layout,omitempty"`
Required []Field `json:"required,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"`
Locale string `json:"locale,omitempty"`
Layout string `json:"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"`
}
data := make(map[string]localizedFormat, len(formats))
for countryCode, format := range formats {
lf := localizedFormat{
Locale: format.Locale.String(),
Layout: format.SelectLayout(locale),
Required: format.Required,
Defaults: format.Defaults,
SublocalityType: format.SublocalityType,
LocalityType: format.LocalityType,
RegionType: format.RegionType,
Expand Down

0 comments on commit 6564da1

Please sign in to comment.