Skip to content

Commit 702587e

Browse files
authored
Merge pull request #4 from matsuri-tech/feat/marshal/interface
feat(*): support interface{} …
2 parents 6b73804 + 384bfa7 commit 702587e

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

marshal.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ func scanToMap(val interface{}) (map[string]interface{}, error) {
4444
}
4545

4646
m[fieldName] = v
47+
} else if ival.Kind() == reflect.Interface {
48+
if reflect.TypeOf(ival.Interface()).Kind() == reflect.Struct {
49+
// PERF: 再帰をやめる
50+
v, err := scanToMap(ival.Interface())
51+
if err != nil {
52+
return nil, err
53+
}
54+
m[fieldName] = v
55+
} else {
56+
m[fieldName] = ival.Interface()
57+
}
4758
} else {
4859
// 構造体でないときはbase typeなのでそのまま
4960
m[fieldName] = ival.Interface()

marshal_test.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ import (
66
)
77

88
func TestMarshal(t *testing.T) {
9+
type testStruct struct {
10+
Name string `json:"name"`
11+
Name2 string `json:"name2" hidden:"-"` // skip
12+
Name3 string `json:"name3" hidden:"true"` // skip
13+
Password string `json:"password" hidden:"mask"` // masked
14+
}
15+
916
cases := []struct {
1017
in interface{}
1118
expected string
1219
}{
1320
{
1421
// hidden:- or hidden:true to skip, hidden:mask to mask
15-
in: struct {
16-
Name string `json:"name"`
17-
Name2 string `json:"name2" hidden:"-"` // skip
18-
Name3 string `json:"name3" hidden:"true"` // skip
19-
Password string `json:"password" hidden:"mask"` // masked
20-
}{
22+
in: testStruct{
2123
Name: "foo",
2224
Password: "password",
2325
},
@@ -63,6 +65,39 @@ func TestMarshal(t *testing.T) {
6365
},
6466
expected: `{"name":"foo","password":"********"}`,
6567
},
68+
{
69+
// interface{} and struct
70+
in: struct {
71+
User interface{} `json:"user"`
72+
}{
73+
testStruct{
74+
Name: "foo",
75+
Password: "password",
76+
},
77+
},
78+
expected: `{"user":{"name":"foo","password":"********"}}`,
79+
},
80+
{
81+
// interface{} with hidden tag
82+
in: struct {
83+
Name string `json:"name"`
84+
Name2 string `json:"name2" hidden:"-"` // skip
85+
InterfaceValue interface{} `json:"interface_value"`
86+
InterfaceHidden interface{} `json:"interface_hidden" hidden:"true"`
87+
Password string `json:"password" hidden:"mask"` // masked
88+
}{
89+
Name: "foo",
90+
InterfaceValue: true,
91+
InterfaceHidden: testStruct{
92+
Name: "",
93+
Name2: "",
94+
Name3: "",
95+
Password: "",
96+
},
97+
Password: "password",
98+
},
99+
expected: `{"name":"foo","interface_value": true,"password":"********"}`,
100+
},
66101
}
67102

68103
for _, c := range cases {
@@ -71,6 +106,6 @@ func TestMarshal(t *testing.T) {
71106
t.Errorf("%+v\n", err)
72107
}
73108

74-
assert.JSONEq(t, string(out), c.expected)
109+
assert.JSONEq(t, c.expected, string(out))
75110
}
76111
}

0 commit comments

Comments
 (0)