Skip to content

Commit 0a1c678

Browse files
committed
fix(codegen): GoDefine 结构体类型会加上类型名
1 parent 92abc2b commit 0a1c678

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

codegen/object.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ import (
2020
// m 需要特殊定义的类型;
2121
// unexported 是否导出小写字段;
2222
func GoDefine(t reflect.Type, m map[reflect.Type]string, unexported bool) string {
23+
for t.Kind() == reflect.Pointer {
24+
t = t.Elem()
25+
}
26+
2327
buf := &errwrap.Buffer{}
2428
goDefine(buf, 0, t, m, unexported, false)
29+
s := buf.String()
30+
31+
if len(s) > 0 && s[0] == '{' { // 结构可能由于 m 的关系返回一个非结构体的类型定义,所以只能由开头是否为 { 判断是否为结构体。
32+
return "type " + t.Name() + " struct " + s
33+
}
2534
return buf.String()
2635
}
2736

codegen/object_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ type object5 struct {
4343
func TestGoDefine(t *testing.T) {
4444
a := assert.New(t, false)
4545

46-
wont := "{\n\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\tArray\t[5]int\n\tSlice\t[]string\n\tByte\tuint8\n}"
46+
wont := "type object1 struct {\n\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\tArray\t[5]int\n\tSlice\t[]string\n\tByte\tuint8\n}"
4747
a.Equal(GoDefine(reflect.TypeFor[object1](), nil, false), wont)
4848

49-
wont = "{\n\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\tObject\t{\n\t\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\t\tArray\t[5]int\n\t\tSlice\t[]string\n\t\tByte\tuint8\n\t}\t`json:\"object\"`\n}"
49+
wont = "type object2 struct {\n\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\tObject\t{\n\t\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\t\tArray\t[5]int\n\t\tSlice\t[]string\n\t\tByte\tuint8\n\t}\t`json:\"object\"`\n}"
5050
a.Equal(GoDefine(reflect.TypeFor[*object2](), nil, false), wont)
5151

5252
a.Equal(GoDefine(reflect.TypeFor[int](), nil, false), "int")
@@ -60,12 +60,15 @@ func TestGoDefine(t *testing.T) {
6060
a.Equal(GoDefine(reflect.TypeFor[time.Time](), m, false), "string")
6161
a.Equal(GoDefine(reflect.TypeFor[*time.Time](), m, false), "string")
6262

63-
a.Equal(GoDefine(reflect.TypeFor[*object3](), m, false), "{\n\tT\tstring\t`json:\"t\"`\n}")
64-
a.Equal(GoDefine(reflect.TypeFor[*object3](), m, true), "{\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n}")
63+
a.Equal(GoDefine(reflect.TypeFor[*object3](), m, false), "type object3 struct {\n\tT\tstring\t`json:\"t\"`\n}")
64+
a.Equal(GoDefine(reflect.TypeFor[*object3](), m, true), "type object3 struct {\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n}")
6565

66-
a.Equal(GoDefine(reflect.TypeFor[*object4](), m, true), "{\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n}")
67-
a.Equal(GoDefine(reflect.TypeFor[*object4](), m, false), "{\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n}")
66+
a.Equal(GoDefine(reflect.TypeFor[*object4](), m, true), "type object4 struct {\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n}")
67+
a.Equal(GoDefine(reflect.TypeFor[*object4](), m, false), "type object4 struct {\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n}")
6868

69-
a.Equal(GoDefine(reflect.TypeFor[*object5](), m, true), "{\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n\tStr\tstring\n}")
70-
a.Equal(GoDefine(reflect.TypeFor[*object5](), m, false), "{\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n\tStr\tstring\n}")
69+
a.Equal(GoDefine(reflect.TypeFor[*object5](), m, true), "type object5 struct {\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n\tStr\tstring\n}")
70+
a.Equal(GoDefine(reflect.TypeFor[*object5](), m, false), "type object5 struct {\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n\tStr\tstring\n}")
71+
72+
a.Equal(GoDefine(reflect.TypeFor[time.Time](), m, false), "string")
73+
a.Equal(GoDefine(reflect.TypeFor[time.Time](), nil, false), "type Time struct {\n}")
7174
}

0 commit comments

Comments
 (0)