mirror of
https://github.com/retailcrm/api-client-go.git
synced 2025-04-01 12:16:16 +03:00
Enhanced the capabilities of OrderDeliveryData
This commit is contained in:
commit
3a3b4590c3
2 changed files with 104 additions and 3 deletions
52
v5/types.go
52
v5/types.go
|
@ -1,6 +1,11 @@
|
|||
package v5
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ByID is "id" constant to use as `by` property in methods
|
||||
const ByID = "id"
|
||||
|
@ -355,14 +360,55 @@ 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 {
|
||||
name := strings.Split(i, ",")[0]
|
||||
delete(additionalData, strings.TrimSpace(name))
|
||||
} 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.Errorf("Marshaled: %s\nExpected: %s\n", data, expectedStr)
|
||||
}
|
||||
|
||||
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.Errorf("Marshaled: %s\nExpected: %s\n", data, expectedStr)
|
||||
}
|
||||
|
||||
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…
Add table
Reference in a new issue