diff options
Diffstat (limited to 'ship.go')
| -rw-r--r-- | ship.go | 78 |
1 files changed, 25 insertions, 53 deletions
@@ -30,33 +30,33 @@ const ( type ShippingAddress struct { // ISO 3166-2 country code - Country string `json:"country_code"` + CountryCode string `json:"country_code"` // 2 or 3 letter state code (officially called ISO-3166-2 // subdivision codes). They are required for some countries (e.g. // US, MX, CA, AU). - State string `json:"state_code"` + StateCode string `json:"state_code"` City string `json:"city"` - Street1 string `json:"street1"` // First address line - Street2 string `json:"street2"` // Second address line - PostCode string `json:"postcode"` // Required for most countries + Street1 string `json:"street1"` // First address line + Street2 string `json:"street2,omitempty"` // Second address line + PostCode string `json:"postcode"` // Required for most countries // Only relevant for US addresses. Some US carriers don't deliver // to business-addresses on Saturday. - IsBusiness bool `json:"is_business"` + IsBusiness bool `json:"is_business,omitempty"` // Full name of the person, including first and last name. - Name string `json:"name"` - Title Title `json:"title"` + Name string `json:"name,omitempty"` + Title Title `json:"title,omitempty"` // Name of an organization. Required if no person name is given. - Organization string `json:"organization"` + Organization string `json:"organization,omitempty"` // Shipping carriers require an email address for notifications // or handling delivery issues. If no email is given, the default // email in the user profile will be used. - Email EmailAddress `json:"email"` + Email EmailAddress `json:"email,omitzero"` // Shipping carriers require a phone number for handling delivery // issues. If no phone number is given, the default in the API @@ -65,10 +65,10 @@ type ShippingAddress struct { // The recipient's tax identification number. Required for // shipping addresses to Brazil, Chile, and Mexico. - TaxId string `json:"recipient_tax_id"` + TaxId string `json:"recipient_tax_id,omitempty"` } -func (a *ShippingAddress) UnmarshalJSON(data []byte) error { +func unmarshalShippingAddressJSON(data []byte, ap *ShippingAddress) error { var alias extShippingAddress if err := json.Unmarshal(data, &alias); err != nil { return err @@ -77,7 +77,7 @@ func (a *ShippingAddress) UnmarshalJSON(data []byte) error { if err != nil { return err } - *a = addr + *ap = addr return nil } @@ -114,28 +114,14 @@ type ShippingAddressWarning struct { // also unmarshals either "country" or "country_code" etc. to account for // the irregularities in the API. type extShippingAddress struct { - Country string `json:"country"` - CountryCode string `json:"country_code"` + ShippingAddress - State string `json:"state"` - StateCode string `json:"state_code"` + Country string `json:"country"` + State string `json:"state"` - City string `json:"city"` - Street1 string `json:"street1"` - Street2 string `json:"street2"` - PostCode string `json:"postcode"` - IsBusiness bool `json:"is_business"` - - Name string `json:"name"` FirstName string `json:"first_name"` LastName string `json:"last_name"` - Title Title `json:"title"` - Organization string `json:"organization"` - Email EmailAddress `json:"email"` - Phone PhoneNumber `json:"phone_number"` - TaxId string `json:"recipient_tax_id"` - Warnings []ShippingAddressWarning `json:"warnings"` Suggested ShippingAddress `json:"suggested_address"` } @@ -146,47 +132,33 @@ type extShippingAddress struct { // It returns error if two or more of the "same field" are present, eg. // both "country" and "country_code". func (ext extShippingAddress) addr() (ShippingAddress, error) { + addr := ext.ShippingAddress + country, both := either(ext.Country, ext.CountryCode) if both { return ShippingAddress{}, fmt.Errorf(`address contains both "country" and "country_code"`) } + addr.CountryCode = country state, both := either(ext.State, ext.StateCode) if both { return ShippingAddress{}, fmt.Errorf(`address contains both "state" and "state_code"`) } + addr.StateCode = state - var name string - hasName := ext.Name != "" hasFirst := ext.FirstName != "" hasLast := ext.LastName != "" - if hasName && (hasFirst || hasLast) { + if addr.Name != "" && (hasFirst || hasLast) { return ShippingAddress{}, fmt.Errorf(`address contains both "name" and {"first_name", "last_name"}`) - } else if hasName { - name = ext.Name } else if hasFirst && hasLast { - name = fmt.Sprintf("%s %s", ext.FirstName, ext.LastName) + addr.Name = fmt.Sprintf("%s %s", ext.FirstName, ext.LastName) } else if hasLast { - name = ext.LastName + addr.Name = ext.LastName } else if hasFirst { - name = ext.FirstName + addr.Name = ext.FirstName } - return ShippingAddress{ - Country: country, - State: state, - City: ext.City, - Street1: ext.Street1, - Street2: ext.Street2, - PostCode: ext.PostCode, - IsBusiness: ext.IsBusiness, - Name: name, - Title: ext.Title, - Organization: ext.Organization, - Email: ext.Email, - Phone: ext.Phone, - TaxId: ext.TaxId, - }, nil + return addr, nil } func either(a, b string) (string, bool) { |