Skip to content

Commit 28b0ed6

Browse files
committed
feat: stm extra export model
1 parent 21bc940 commit 28b0ed6

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed

tools/stm/stm.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import (
88
// struct => map
99

1010
func StructToMap(val interface{}, tag string) map[string]interface{} {
11-
return structToMap(val, tag)
11+
return structToMap(val, tag, false)
1212
}
1313

14-
func structToMap(val interface{}, tag string) map[string]interface{} {
14+
func StructToMapExtraExport(val interface{}, tag string) map[string]interface{} {
15+
return structToMap(val, tag, true)
16+
}
17+
18+
func structToMap(val interface{}, tag string, extraExport bool) map[string]interface{} {
1519
t, v := reflect.TypeOf(val), reflect.ValueOf(val)
1620
if v.Kind() == reflect.Ptr {
17-
return structToMap(v.Elem().Interface(), tag)
21+
return structToMap(v.Elem().Interface(), tag, extraExport)
1822
}
1923
if v.Kind() != reflect.Struct {
2024
return nil
@@ -40,17 +44,17 @@ func structToMap(val interface{}, tag string) map[string]interface{} {
4044
if vField.CanInterface() {
4145
iv := vField.Interface()
4246
if st {
43-
iv = structToMap(iv, tag)
47+
iv = structToMap(iv, tag, extraExport)
4448
}
4549
mp[name] = iv
46-
} else {
50+
} else if extraExport {
4751
// 未导出
4852
cp := reflect.New(v.Type()).Elem()
4953
cp.Set(v)
5054
value := cp.Field(i)
5155
iv := reflect.NewAt(value.Type(), unsafe.Pointer(value.UnsafeAddr())).Elem().Interface()
5256
if st {
53-
iv = structToMap(iv, tag)
57+
iv = structToMap(iv, tag, extraExport)
5458
}
5559
mp[name] = iv
5660
}

tools/stm/stm_test.go

+74-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type testS struct {
2020
list2 []int `stm:"list2"`
2121
}
2222

23-
func Test_structToMap(t *testing.T) {
23+
func Test_StructToMapExtraExport(t *testing.T) {
2424
ts := testS{
2525
S1: "s1",
2626
s2: "s2",
@@ -101,7 +101,79 @@ func Test_structToMap(t *testing.T) {
101101
}
102102
for _, tt := range tests {
103103
t.Run(tt.name, func(t *testing.T) {
104-
assert.Equal(t, structToMap(ts, tt.args.tag), tt.want)
104+
assert.Equal(t, StructToMapExtraExport(ts, tt.args.tag), tt.want)
105+
})
106+
}
107+
}
108+
109+
func Test_StructToMap(t *testing.T) {
110+
ts := testS{
111+
S1: "s1",
112+
s2: "s2",
113+
T1: struct {
114+
Name string `stm:"name"`
115+
age int `stm:"age" stm2:"-"`
116+
}{
117+
Name: "name",
118+
age: 18,
119+
},
120+
t2: struct {
121+
Gender bool `stm:"gender"`
122+
}{
123+
Gender: true,
124+
},
125+
List1: []string{"1", "2", "3"},
126+
list2: []int{1, 2, 3},
127+
}
128+
type args struct {
129+
tag string
130+
}
131+
tests := []struct {
132+
name string
133+
args args
134+
want map[string]interface{}
135+
}{
136+
{
137+
name: "",
138+
args: args{
139+
tag: "",
140+
},
141+
want: map[string]interface{}{
142+
"S1": "s1",
143+
"T1": map[string]interface{}{
144+
"Name": "name",
145+
},
146+
"List1": []string{"1", "2", "3"},
147+
},
148+
},
149+
{
150+
name: "",
151+
args: args{
152+
tag: "stm",
153+
},
154+
want: map[string]interface{}{
155+
"S_1": "s1",
156+
"T_1": map[string]interface{}{
157+
"name": "name",
158+
},
159+
"list1": []string{"1", "2", "3"},
160+
},
161+
},
162+
{
163+
name: "",
164+
args: args{
165+
tag: "stm2",
166+
},
167+
want: map[string]interface{}{
168+
"T1": map[string]interface{}{
169+
"Name": "name",
170+
},
171+
},
172+
},
173+
}
174+
for _, tt := range tests {
175+
t.Run(tt.name, func(t *testing.T) {
176+
assert.Equal(t, StructToMap(ts, tt.args.tag), tt.want)
105177
})
106178
}
107179
}

0 commit comments

Comments
 (0)