mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-25 06:16:03 +03:00
Enhanced the capabilities of OrderDeliveryData
This commit is contained in:
parent
a975815296
commit
f46bffa9b3
50
v5/types.go
50
v5/types.go
@ -1,6 +1,10 @@
|
||||
package v5
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// ByID is "id" constant to use as `by` property in methods
|
||||
const ByID = "id"
|
||||
@ -354,14 +358,54 @@ type OrderDeliveryService struct {
|
||||
Active bool `json:"active,omitempty"`
|
||||
}
|
||||
|
||||
// OrderDeliveryData type
|
||||
type OrderDeliveryData struct {
|
||||
// OrderDeliveryDataBasic type
|
||||
type OrderDeliveryDataBasic struct {
|
||||
TrackNumber string `json:"trackNumber,omitempty"`
|
||||
Status string `json:"status,omitempty"`
|
||||
PickuppointAddress string `json:"pickuppointAddress,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
|
||||
type OrderMarketplace struct {
|
||||
Code string `json:"code,omitempty"`
|
||||
|
55
v5/types_test.go
Normal file
55
v5/types_test.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user