Skip to content

Commit fd0e1e7

Browse files
committed
fix(core): marshal map in sections
* when calling Marshal with sections in options, the map kind was not correctly handled because the map marshaling occured only inside the struct case
1 parent c785adb commit fd0e1e7

File tree

2 files changed

+69
-26
lines changed

2 files changed

+69
-26
lines changed

core/human/marshal.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func Marshal(data interface{}, opt *MarshalOpt) (string, error) {
4545
}
4646

4747
rType := rValue.Type()
48-
4948
// safely get the marshalerFunc
5049
marshalerFunc, _ := getMarshalerFunc(rType)
5150
isNil := isInterfaceNil(data)
@@ -84,6 +83,9 @@ func Marshal(data interface{}, opt *MarshalOpt) (string, error) {
8483
case rType.Kind() == reflect.Struct:
8584
return marshalStruct(rValue, opt)
8685

86+
case rType.Kind() == reflect.Map:
87+
return MarshalMap(rValue, opt)
88+
8789
// by default we use defaultMarshalerFunc
8890
default:
8991
return defaultMarshalerFunc(rValue.Interface(), opt)
@@ -481,3 +483,40 @@ func getDefaultFieldsOpt(t reflect.Type) []*MarshalFieldOpt {
481483

482484
return results
483485
}
486+
487+
func MarshalMap(m reflect.Value, opt *MarshalOpt) (string, error) {
488+
buffer := bytes.Buffer{}
489+
490+
w := tabwriter.NewWriter(&buffer, 5, 1, colPadding, ' ', tabwriter.ANSIGraphicsRendition)
491+
492+
mapKeys := m.MapKeys()
493+
sort.Slice(mapKeys, func(i, j int) bool {
494+
return mapKeys[i].String() < mapKeys[j].String()
495+
})
496+
497+
for _, mapKey := range mapKeys {
498+
mapValue := m.MapIndex(mapKey)
499+
500+
if mapValue.Type().Kind() == reflect.Slice {
501+
sort.Slice(mapValue.Interface(), func(i, j int) bool {
502+
return mapValue.Index(i).String() < mapValue.Index(j).String()
503+
})
504+
sliceLen := mapValue.Len()
505+
values := make([]string, sliceLen)
506+
for i := range sliceLen {
507+
values[i] = fmt.Sprint(mapValue.Index(i).Interface())
508+
}
509+
fmt.Fprintf(w, "%s\t%s\n", mapKey.String(), strings.Join(values, " "))
510+
} else {
511+
content, err := Marshal(mapValue.Interface(), opt)
512+
if err != nil {
513+
return "", err
514+
}
515+
fmt.Fprintf(w, "%s\t%s\n", mapKey.String(), content)
516+
}
517+
}
518+
519+
w.Flush()
520+
521+
return strings.TrimSpace(buffer.String()), nil
522+
}

core/human/marshal_test.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,15 @@ func TestMarshal(t *testing.T) {
152152
`,
153153
}))
154154

155-
t.Run("structWithTitle", run(&testCase{
155+
t.Run("structWithMapsInSection", run(&testCase{
156156
opt: &human.MarshalOpt{
157157
Sections: []*human.MarshalSection{
158158
{
159-
Title: "MapStringList",
160159
FieldName: "MapStringList",
161160
},
161+
{
162+
FieldName: "Map",
163+
},
162164
},
163165
},
164166
data: &Struct{
@@ -188,32 +190,34 @@ func TestMarshal(t *testing.T) {
188190
Bytes: []byte{0, 1},
189191
},
190192
result: `
191-
String This is a string
192-
Int 42
193-
Bool true
194-
Strings.0 s1
195-
Strings.1 s2
196-
Time ` + humanDate + `
197-
Struct.String -
198-
Struct.Int 0
199-
Struct.Bool false
200-
Struct.Time a long while ago
201-
Struct.Stringer a stringer
202-
Structs.0.String Nested string
203-
Structs.0.Int 0
204-
Structs.0.Bool false
205-
Structs.0.Time a long while ago
206-
Structs.0.Stringer a stringer
207-
Map.key1 v1
208-
Map.key2 v2
209-
Stringer a stringer
210-
StringerPtr a stringer
211-
Size 13 kB
212-
Bytes AAE=
193+
String This is a string
194+
Int 42
195+
Bool true
196+
Strings.0 s1
197+
Strings.1 s2
198+
Time 34 years ago
199+
Struct.String -
200+
Struct.Int 0
201+
Struct.Bool false
202+
Struct.Time a long while ago
203+
Struct.Stringer a stringer
204+
Structs.0.String Nested string
205+
Structs.0.Int 0
206+
Structs.0.Bool false
207+
Structs.0.Time a long while ago
208+
Structs.0.Stringer a stringer
209+
Stringer a stringer
210+
StringerPtr a stringer
211+
Size 13 kB
212+
Bytes AAE=
213213
214-
MapStringList:
214+
Map String List:
215215
key1 v1 v2
216216
key2 v3 v4
217+
218+
Map:
219+
key1 v1
220+
key2 v2
217221
`,
218222
}))
219223

0 commit comments

Comments
 (0)