From 3477ec23032a9e2f1a71a5e841546e815141aa89 Mon Sep 17 00:00:00 2001 From: Denis G Date: Sat, 23 Nov 2024 15:16:10 +0300 Subject: [PATCH] Replaced int with custom type Int --- models.go | 14 ++++---- models_v2.go | 2 +- types.go | 24 ++++++++----- types_test.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 17 deletions(-) diff --git a/models.go b/models.go index e94dfd6..cc108ca 100644 --- a/models.go +++ b/models.go @@ -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 @@ -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"` @@ -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"` } @@ -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 { diff --git a/models_v2.go b/models_v2.go index de1023a..b236c21 100644 --- a/models_v2.go +++ b/models_v2.go @@ -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 { diff --git a/types.go b/types.go index eb0b557..a72248a 100644 --- a/types.go +++ b/types.go @@ -36,7 +36,7 @@ type IntOrAuto struct { json.Unmarshaler Auto bool - Value int + Value Int } func (i *IntOrAuto) UnmarshalJSON(v []byte) error { @@ -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 { @@ -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 ( diff --git a/types_test.go b/types_test.go index 81a12c0..5cd1f01 100644 --- a/types_test.go +++ b/types_test.go @@ -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) + } + }) + } +}