Skip to content

Commit

Permalink
fix: fix port function for interface{} types (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored Jun 6, 2024
1 parent f56e69f commit 0af2458
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
28 changes: 16 additions & 12 deletions internal/apijson/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,23 @@ func Port(from any, to any) error {
}
if value, ok := values[ptag.name]; ok {
delete(values, ptag.name)
switch value.Kind() {
case reflect.String:
toVal.Field(i).SetString(value.String())
case reflect.Bool:
toVal.Field(i).SetBool(value.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
toVal.Field(i).SetInt(value.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
toVal.Field(i).SetUint(value.Uint())
case reflect.Float32, reflect.Float64:
toVal.Field(i).SetFloat(value.Float())
default:
if field.Type.Kind() == reflect.Interface {
toVal.Field(i).Set(value)
} else {
switch value.Kind() {
case reflect.String:
toVal.Field(i).SetString(value.String())
case reflect.Bool:
toVal.Field(i).SetBool(value.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
toVal.Field(i).SetInt(value.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
toVal.Field(i).SetUint(value.Uint())
case reflect.Float32, reflect.Float64:
toVal.Field(i).SetFloat(value.Float())
default:
toVal.Field(i).Set(value)
}
}
}

Expand Down
16 changes: 14 additions & 2 deletions internal/apijson/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Card struct {
IsFoo bool `json:"is_foo"`
IsBar bool `json:"is_bar"`
Metadata Metadata `json:"metadata"`
Value interface{} `json:"value"`

JSON cardJSON
}
Expand All @@ -26,6 +27,7 @@ type cardJSON struct {
IsFoo Field
IsBar Field
Metadata Field
Value Field
Extras map[string]Field
raw string
}
Expand All @@ -40,6 +42,7 @@ type CardVisa struct {
Data CardVisaData `json:"data"`
IsFoo bool `json:"is_foo"`
Metadata Metadata `json:"metadata"`
Value string `json:"value"`

JSON cardVisaJSON
}
Expand All @@ -49,6 +52,7 @@ type cardVisaJSON struct {
Data Field
IsFoo Field
Metadata Field
Value Field
Extras map[string]Field
raw string
}
Expand All @@ -67,6 +71,7 @@ type CardMastercard struct {
Data CardMastercardData `json:"data"`
IsBar bool `json:"is_bar"`
Metadata Metadata `json:"metadata"`
Value bool `json:"value"`

JSON cardMastercardJSON
}
Expand All @@ -76,6 +81,7 @@ type cardMastercardJSON struct {
Data Field
IsBar Field
Metadata Field
Value Field
Extras map[string]Field
raw string
}
Expand All @@ -102,11 +108,13 @@ var portTests = map[string]struct {
Metadata: Metadata{
CreatedAt: "Mar 29 2024",
},
Value: "value",
JSON: cardVisaJSON{
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"foo"}}`,
Processor: Field{raw: `"visa"`, status: valid},
IsFoo: Field{raw: `true`, status: valid},
Data: Field{raw: `{"foo":"foo"}`, status: valid},
Value: Field{raw: `"value"`, status: valid},
Extras: map[string]Field{"extra": {raw: `"yo"`, status: valid}},
},
},
Expand All @@ -120,30 +128,34 @@ var portTests = map[string]struct {
Metadata: Metadata{
CreatedAt: "Mar 29 2024",
},
Value: "value",
JSON: cardJSON{
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"foo"}}`,
Processor: Field{raw: `"visa"`, status: valid},
IsFoo: Field{raw: `true`, status: valid},
Data: Field{raw: `{"foo":"foo"}`, status: valid},
Value: Field{raw: `"value"`, status: valid},
Extras: map[string]Field{"extra": {raw: `"yo"`, status: valid}},
},
},
},
"mastercard to card": {
CardMastercard{
Processor: "visa",
Processor: "mastercard",
IsBar: true,
Data: CardMastercardData{
Bar: 13,
},
Value: false,
},
Card{
Processor: "visa",
Processor: "mastercard",
IsFoo: false,
IsBar: true,
Data: CardMastercardData{
Bar: 13,
},
Value: false,
},
},
}
Expand Down

0 comments on commit 0af2458

Please sign in to comment.