Enhanced the capabilities of OrderDeliveryData

This commit is contained in:
Daniel Weiser 2020-08-05 17:34:38 +03:00
parent a975815296
commit f46bffa9b3
2 changed files with 123 additions and 24 deletions

View File

@ -1,6 +1,10 @@
package v5 package v5
import "net/http" import (
"encoding/json"
"net/http"
"reflect"
)
// ByID is "id" constant to use as `by` property in methods // ByID is "id" constant to use as `by` property in methods
const ByID = "id" const ByID = "id"
@ -26,24 +30,24 @@ type Pagination struct {
// Address type // Address type
type Address struct { type Address struct {
Index string `json:"index,omitempty"` Index string `json:"index,omitempty"`
CountryIso string `json:"countryIso,omitempty"` CountryIso string `json:"countryIso,omitempty"`
Region string `json:"region,omitempty"` Region string `json:"region,omitempty"`
RegionID int `json:"regionId,omitempty"` RegionID int `json:"regionId,omitempty"`
City string `json:"city,omitempty"` City string `json:"city,omitempty"`
CityID int `json:"cityId,omitempty"` CityID int `json:"cityId,omitempty"`
CityType string `json:"cityType,omitempty"` CityType string `json:"cityType,omitempty"`
Street string `json:"street,omitempty"` Street string `json:"street,omitempty"`
StreetID int `json:"streetId,omitempty"` StreetID int `json:"streetId,omitempty"`
StreetType string `json:"streetType,omitempty"` StreetType string `json:"streetType,omitempty"`
Building string `json:"building,omitempty"` Building string `json:"building,omitempty"`
Flat string `json:"flat,omitempty"` Flat string `json:"flat,omitempty"`
Floor int `json:"floor,omitempty"` Floor int `json:"floor,omitempty"`
Block int `json:"block,omitempty"` Block int `json:"block,omitempty"`
House string `json:"house,omitempty"` House string `json:"house,omitempty"`
Metro string `json:"metro,omitempty"` Metro string `json:"metro,omitempty"`
Notes string `json:"notes,omitempty"` Notes string `json:"notes,omitempty"`
Text string `json:"text,omitempty"` Text string `json:"text,omitempty"`
} }
// GeoHierarchyRow type // GeoHierarchyRow type
@ -354,14 +358,54 @@ type OrderDeliveryService struct {
Active bool `json:"active,omitempty"` Active bool `json:"active,omitempty"`
} }
// OrderDeliveryData type // OrderDeliveryDataBasic type
type OrderDeliveryData struct { type OrderDeliveryDataBasic struct {
TrackNumber string `json:"trackNumber,omitempty"` TrackNumber string `json:"trackNumber,omitempty"`
Status string `json:"status,omitempty"` Status string `json:"status,omitempty"`
PickuppointAddress string `json:"pickuppointAddress,omitempty"` PickuppointAddress string `json:"pickuppointAddress,omitempty"`
PayerType string `json:"payerType,omitempty"` PayerType string `json:"payerType,omitempty"`
} }
// OrderDeliveryData type
type OrderDeliveryData struct {
OrderDeliveryDataBasic
AdditionalFields map[string]interface{}
}
// UnmarshalJSON method
func (v *OrderDeliveryData) UnmarshalJSON(b []byte) error {
var additionalData map[string]interface{}
json.Unmarshal(b, &additionalData)
json.Unmarshal(b, &v.OrderDeliveryDataBasic)
object := reflect.TypeOf(v.OrderDeliveryDataBasic)
for i := 0; i < object.NumField(); i++ {
field := object.Field(i)
if i, ok := field.Tag.Lookup("json"); ok {
delete(additionalData, i[:len(i)-10])
} else {
delete(additionalData, field.Name)
}
}
v.AdditionalFields = additionalData
return nil
}
// MarshalJSON method
func (v OrderDeliveryData) MarshalJSON() ([]byte, error) {
result := map[string]interface{}{}
data, _ := json.Marshal(v.OrderDeliveryDataBasic)
json.Unmarshal(data, &result)
for key, value := range v.AdditionalFields {
result[key] = value
}
return json.Marshal(result)
}
// OrderMarketplace type // OrderMarketplace type
type OrderMarketplace struct { type OrderMarketplace struct {
Code string `json:"code,omitempty"` Code string `json:"code,omitempty"`
@ -628,15 +672,15 @@ type Segment struct {
// SettingsNode represents an item in settings. All settings nodes contains only string value and update time for now. // SettingsNode represents an item in settings. All settings nodes contains only string value and update time for now.
type SettingsNode struct { type SettingsNode struct {
Value string `json:"value"` Value string `json:"value"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`
} }
// Settings type. Contains retailCRM configuration. // Settings type. Contains retailCRM configuration.
type Settings struct { type Settings struct {
DefaultCurrency SettingsNode `json:"default_currency"` DefaultCurrency SettingsNode `json:"default_currency"`
SystemLanguage SettingsNode `json:"system_language"` SystemLanguage SettingsNode `json:"system_language"`
Timezone SettingsNode `json:"timezone"` Timezone SettingsNode `json:"timezone"`
} }
/** /**

55
v5/types_test.go Normal file
View File

@ -0,0 +1,55 @@
package v5
import (
"encoding/json"
"reflect"
"testing"
)
func TestClient_OrderDeliveryData(t *testing.T) {
d := OrderDeliveryData{
OrderDeliveryDataBasic: OrderDeliveryDataBasic{
"track",
"status",
"address",
"type",
},
}
data, _ := json.Marshal(d)
expectedStr := `{"payerType":"type","pickuppointAddress":"address","status":"status","trackNumber":"track"}`
if string(data) != expectedStr {
t.Error("error")
}
d.AdditionalFields = map[string]interface{}{
"customFirst": "one",
"customSecond": "two",
}
data, _ = json.Marshal(d)
expectedStr = `{"customFirst":"one","customSecond":"two","payerType":"type","pickuppointAddress":"address","status":"status","trackNumber":"track"}`
if string(data) != expectedStr {
t.Error("error")
}
d = OrderDeliveryData{}
json.Unmarshal(data, &d)
expected := OrderDeliveryData{
OrderDeliveryDataBasic: OrderDeliveryDataBasic{
"track",
"status",
"address",
"type",
},
AdditionalFields: map[string]interface{}{
"customFirst": "one",
"customSecond": "two",
},
}
eq := reflect.DeepEqual(expected, d)
if eq != true {
t.Errorf("Unmarshaled: %#v\nExpected: %#v\n", d, expected)
}
}