Skip to content

Commit 7935417

Browse files
committedJan 30, 2025
fix: fix apijson.Port for embedded structs (#3846)
1 parent 540534c commit 7935417

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed
 

‎internal/apijson/port.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ func Port(from any, to any) error {
3636

3737
// Iterate through the fields of v and load all the "normal" fields in the struct to the map of
3838
// string to reflect.Value, as well as their raw .JSON.Foo counterpart indicated by j.
39-
var getFields func(t reflect.Type, v, j reflect.Value)
40-
getFields = func(t reflect.Type, v, j reflect.Value) {
39+
var getFields func(t reflect.Type, v reflect.Value)
40+
getFields = func(t reflect.Type, v reflect.Value) {
41+
j := v.FieldByName("JSON")
42+
4143
// Recurse into anonymous fields first, since the fields on the object should win over the fields in the
4244
// embedded object.
4345
for i := 0; i < t.NumField(); i++ {
4446
field := t.Field(i)
4547
if field.Anonymous {
46-
getFields(field.Type, v.Field(i), v.FieldByName("JSON"))
48+
getFields(field.Type, v.Field(i))
4749
continue
4850
}
4951
}
@@ -60,7 +62,7 @@ func Port(from any, to any) error {
6062
}
6163
}
6264
}
63-
getFields(fromType, fromVal, fromJSON)
65+
getFields(fromType, fromVal)
6466

6567
// Use the values from the previous step to populate the 'to' struct.
6668
for i := 0; i < toType.NumField(); i++ {

‎internal/apijson/port_test.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,15 @@ type CardMastercardData struct {
9797
type CommonFields struct {
9898
Metadata Metadata `json:"metadata"`
9999
Value string `json:"value"`
100+
101+
JSON commonFieldsJSON
100102
}
101103

102104
type commonFieldsJSON struct {
103105
Metadata Field
104106
Value Field
105107
ExtraFields map[string]Field
108+
raw string
106109
}
107110

108111
type CardEmbedded struct {
@@ -115,7 +118,6 @@ type CardEmbedded struct {
115118
}
116119

117120
type cardEmbeddedJSON struct {
118-
commonFieldsJSON
119121
Processor Field
120122
Data Field
121123
IsFoo Field
@@ -196,18 +198,19 @@ var portTests = map[string]struct {
196198
CreatedAt: "Mar 29 2024",
197199
},
198200
Value: "embedded_value",
201+
JSON: commonFieldsJSON{
202+
Metadata: Field{raw: `{"created_at":"Mar 29 2024"}`, status: valid},
203+
Value: Field{raw: `"embedded_value"`, status: valid},
204+
raw: `should not matter`,
205+
},
199206
},
200207
Processor: "visa",
201208
IsFoo: true,
202209
Data: CardVisaData{
203210
Foo: "embedded_foo",
204211
},
205212
JSON: cardEmbeddedJSON{
206-
commonFieldsJSON: commonFieldsJSON{
207-
Metadata: Field{raw: `{"created_at":"Mar 29 2024"}`, status: valid},
208-
Value: Field{raw: `"embedded_value"`, status: valid},
209-
},
210-
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"embedded_foo"}}`,
213+
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"embedded_foo"},"metadata":{"created_at":"Mar 29 2024"},"value":"embedded_value"}`,
211214
Processor: Field{raw: `"visa"`, status: valid},
212215
IsFoo: Field{raw: `true`, status: valid},
213216
Data: Field{raw: `{"foo":"embedded_foo"}`, status: valid},
@@ -225,7 +228,7 @@ var portTests = map[string]struct {
225228
},
226229
Value: "embedded_value",
227230
JSON: cardJSON{
228-
raw: "{\"processor\":\"visa\",\"is_foo\":true,\"data\":{\"foo\":\"embedded_foo\"}}",
231+
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"embedded_foo"},"metadata":{"created_at":"Mar 29 2024"},"value":"embedded_value"}`,
229232
Processor: Field{raw: `"visa"`, status: 0x3},
230233
IsFoo: Field{raw: "true", status: 0x3},
231234
Data: Field{raw: `{"foo":"embedded_foo"}`, status: 0x3},

0 commit comments

Comments
 (0)
Please sign in to comment.