Skip to content

Commit 3cf45a9

Browse files
Codelaxremyleone
andauthored
fix(human): interface in struct marshalling (#2742)
Co-authored-by: Rémy Léone <rleone@scaleway.com>
1 parent 17c863c commit 3cf45a9

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

internal/human/marshal.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ func marshalStruct(value reflect.Value, opt *MarshalOpt) (string, error) {
183183
}
184184

185185
return data, nil
186+
case rType.Kind() == reflect.Interface:
187+
// If type is interface{}
188+
// marshal the underlying type
189+
return marshal(value.Elem(), keys)
186190
default:
187191
str, err := defaultMarshalerFunc(value.Interface(), subOpts)
188192
if err != nil {

internal/human/marshal_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ type Struct struct {
2626
Size *scw.Size
2727
}
2828

29+
type StructAny struct {
30+
String interface{}
31+
StringPtr interface{}
32+
Map map[string]interface{}
33+
MapPtr map[string]interface{}
34+
}
35+
2936
type Address struct {
3037
Street string
3138
City string
@@ -184,6 +191,26 @@ func TestMarshal(t *testing.T) {
184191
},
185192
result: `Name Paul`,
186193
}))
194+
195+
var testAnyString = "MyString"
196+
t.Run("any", run(&testCase{
197+
data: &StructAny{
198+
String: testAnyString,
199+
StringPtr: &testAnyString,
200+
Map: map[string]interface{}{
201+
"String": testAnyString,
202+
},
203+
MapPtr: map[string]interface{}{
204+
"String": &testAnyString,
205+
},
206+
},
207+
result: `
208+
String MyString
209+
StringPtr MyString
210+
Map.String MyString
211+
MapPtr.String MyString
212+
`,
213+
}))
187214
}
188215

189216
func Test_getStructFieldsIndex(t *testing.T) {

0 commit comments

Comments
 (0)