Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type GetBalanceReplyLevel struct {
type Child struct {
Name string `json:"name,omitempty"`
Birthdate string `json:"birthdate,omitempty"`
Gender int `json:"gender"`
Gender Int `json:"gender"`
}

// new-client
Expand Down Expand Up @@ -128,10 +128,10 @@ type NewClientClient struct {
Name string `json:"name,omitempty"`
PatronymicName string `json:"patronymicName,omitempty"`
FullName string `json:"fullName,omitempty"`
Gender int `json:"gender,omitempty"`
Gender Int `json:"gender,omitempty"`
Birthdate string `json:"birthdate,omitempty"`
Email string `json:"email,omitempty"`
Level int `json:"level,omitempty"`
Level Int `json:"level,omitempty"`
IsEmailSubscribed *bool `json:"isEmailSubscribed,omitempty"`
IsPhoneSubscribed *bool `json:"isPhoneSubscribed,omitempty"`
ExtraFields ExtraFields `json:"extraFields,omitempty"`
Expand Down Expand Up @@ -399,8 +399,8 @@ type ClientQuery struct {
}

type BalanceAdjustmentQuery struct {
AmountDelta int `json:"amountDelta"`
ExpirationPeriodDays int `json:"expirationPeriodDays,omitempty"`
AmountDelta Int `json:"amountDelta"`
ExpirationPeriodDays Int `json:"expirationPeriodDays,omitempty"`
Comment string `json:"comment,omitempty"`
Notify bool `json:"notify,omitempty"`
}
Expand Down Expand Up @@ -509,8 +509,8 @@ type GetHistoryQuery struct {
}

type PaginationQuery struct {
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
Limit Int `json:"limit,omitempty"`
Offset Int `json:"offset,omitempty"`
}

type GetHistoryReply struct {
Expand Down
2 changes: 1 addition & 1 deletion models_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type CalculationQueryRowProduct struct {
BlackPrice decimal.Decimal `json:"blackPrice"`
RedPrice *decimal.Decimal `json:"redPrice,omitempty"`
MinPrice decimal.Decimal `json:"minPrice,omitempty"`
VatPercent int `json:"vatPercent,omitempty"`
VatPercent Int `json:"vatPercent,omitempty"`
}

type V2CalculatePurchaseReply struct {
Expand Down
24 changes: 15 additions & 9 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type IntOrAuto struct {
json.Unmarshaler

Auto bool
Value int
Value Int
}

func (i *IntOrAuto) UnmarshalJSON(v []byte) error {
Expand All @@ -46,6 +46,19 @@ func (i *IntOrAuto) UnmarshalJSON(v []byte) error {
return nil
}
i.Auto = false
return i.Value.UnmarshalJSON(v)
}

func (i *IntOrAuto) MarshalJSON() ([]byte, error) {
if i.Auto {
return []byte("\"auto\""), nil
}
return []byte(strconv.Itoa(int(i.Value))), nil
}

type Int int

func (i *Int) UnmarshalJSON(v []byte) error {
// it is allowed for value to be a float with zero fractional part, e.g. 1.0
f, err := strconv.ParseFloat(string(v), 64)
if err != nil {
Expand All @@ -55,17 +68,10 @@ func (i *IntOrAuto) UnmarshalJSON(v []byte) error {
if frac != 0 {
return fmt.Errorf("unexpected fractional part in integer value: %f", f)
}
i.Value = int(iv)
*i = Int(iv)
return nil
}

func (i *IntOrAuto) MarshalJSON() ([]byte, error) {
if i.Auto {
return []byte("\"auto\""), nil
}
return []byte(strconv.Itoa(i.Value)), nil
}

type ValidRangeTime time.Time

var (
Expand Down
95 changes: 95 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,98 @@ func TestIntOrString_UnmarshalJSON(t *testing.T) {
}
}
}

func TestIntOrAuto_UnmarshalJSON(t *testing.T) {
cases := []struct {
input string
expectedAuto bool
expectedInt int
expectedErr bool
}{
{
"null",
false,
0,
true,
},
{
"0",
false,
0,
false,
},
{
"10",
false,
10,
false,
},
{
"10.467",
false,
0,
true,
},
{
"\"non-int\"",
false,
0,
true,
},
{
"",
false,
0,
true,
},
{
"-10",
false,
-10,
false,
},
{
"-10.445",
false,
0,
true,
},
{
"-10.0",
false,
-10,
false,
},
{
"\"auto\"",
true,
0,
false,
},
{
"\"AUTO\"",
true,
0,
false,
},
}

for _, c := range cases {
t.Run(c.input, func(t *testing.T) {
var i IntOrAuto
err := i.UnmarshalJSON([]byte(c.input))
if c.expectedErr && err == nil {
t.Fatalf("expected error, got nil")
}
if !c.expectedErr && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if i.Auto != c.expectedAuto {
t.Fatalf("expected Auto to be %t, got %t", c.expectedAuto, i.Auto)
}
if int(i.Value) != c.expectedInt {
t.Fatalf("expected Value to be %d, got %d", c.expectedInt, i.Value)
}
})
}
}
Loading