WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit a77ce83

Browse files
committed
feat(codegen): GoDefine 可以生成对象字段的注释
1 parent cc08cbc commit a77ce83

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

codegen/object.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ import (
1717
// 对于结构类型会自动展开。
1818
//
1919
// t 需要转换的类型;
20-
// m 需要特殊定义的类型;
20+
// m 需要特殊定义的类型,可以为空;
21+
// c 为对象的字段生成注释,可以为空;
2122
// unexported 是否导出小写字段;
22-
func GoDefine(t reflect.Type, m map[reflect.Type]string, unexported bool) string {
23+
func GoDefine(t reflect.Type, m map[reflect.Type]string, c func(*reflect.StructField) string, unexported bool) string {
2324
for t.Kind() == reflect.Pointer {
2425
t = t.Elem()
2526
}
2627

2728
buf := &errwrap.Buffer{}
28-
goDefine(buf, 0, t, m, unexported, false)
29+
goDefine(buf, 0, t, m, c, unexported, false)
2930
s := buf.String()
3031

3132
if strings.HasPrefix(s, "struct {") { // 结构可能由于 m 的关系返回一个非结构体的类型定义,所以只能由开头是否为 struct { 判断是否为结构体。
@@ -34,7 +35,7 @@ func GoDefine(t reflect.Type, m map[reflect.Type]string, unexported bool) string
3435
return buf.String()
3536
}
3637

37-
func goDefine(buf *errwrap.Buffer, indent int, t reflect.Type, m map[reflect.Type]string, unexported, anonymous bool) {
38+
func goDefine(buf *errwrap.Buffer, indent int, t reflect.Type, m map[reflect.Type]string, c func(*reflect.StructField) string, unexported, anonymous bool) {
3839
if len(m) > 0 {
3940
if s, found := m[t]; found {
4041
buf.WString(s)
@@ -46,13 +47,13 @@ func goDefine(buf *errwrap.Buffer, indent int, t reflect.Type, m map[reflect.Typ
4647
case reflect.Func, reflect.Chan: // 忽略
4748
case reflect.Pointer:
4849
buf.WByte('*')
49-
goDefine(buf, indent, t.Elem(), m, unexported, anonymous)
50+
goDefine(buf, indent, t.Elem(), m, c, unexported, anonymous)
5051
case reflect.Slice:
5152
buf.WString("[]")
52-
goDefine(buf, indent, t.Elem(), m, unexported, anonymous)
53+
goDefine(buf, indent, t.Elem(), m, c, unexported, anonymous)
5354
case reflect.Array:
5455
buf.WByte('[').WString(strconv.Itoa(t.Len())).WByte(']')
55-
goDefine(buf, indent, t.Elem(), m, unexported, anonymous)
56+
goDefine(buf, indent, t.Elem(), m, c, unexported, anonymous)
5657
case reflect.Struct:
5758
if !anonymous {
5859
if t.NumField() == 0 {
@@ -72,7 +73,7 @@ func goDefine(buf *errwrap.Buffer, indent int, t reflect.Type, m map[reflect.Typ
7273
for tt.Kind() == reflect.Pointer { // 匿名字段需要去掉指针类型
7374
tt = tt.Elem()
7475
}
75-
goDefine(buf, indent, tt, m, unexported, true)
76+
goDefine(buf, indent, tt, m, c, unexported, true)
7677
continue
7778
}
7879

@@ -85,12 +86,18 @@ func goDefine(buf *errwrap.Buffer, indent int, t reflect.Type, m map[reflect.Typ
8586
}
8687

8788
buf.WString(strings.Repeat("\t", indent)).WString(f.Name).WByte('\t')
88-
goDefine(buf, indent, f.Type, m, unexported, false)
89+
goDefine(buf, indent, f.Type, m, c, unexported, false)
8990

9091
if f.Tag != "" {
9192
buf.WByte('\t').WByte('`').WString(string(f.Tag)).WByte('`')
9293
}
9394

95+
if c != nil {
96+
if comment := c(&f); comment != "" {
97+
buf.WString("\t// ").WString(comment)
98+
}
99+
}
100+
94101
buf.WByte('\n')
95102
}
96103

codegen/object_test.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,38 @@ type object6 struct {
4848
func TestGoDefine(t *testing.T) {
4949
a := assert.New(t, false)
5050

51+
c := func(f *reflect.StructField) string {
52+
return f.Name
53+
}
54+
5155
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}"
52-
a.Equal(GoDefine(reflect.TypeFor[object1](), nil, false), wont)
56+
a.Equal(GoDefine(reflect.TypeFor[object1](), nil, nil, false), wont)
5357

5458
wont = "type object2 struct {\n\tInt\tint\t`json:\"int\" yaml:\"int\"`\n\tObject\t*struct {\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}"
55-
a.Equal(GoDefine(reflect.TypeFor[*object2](), nil, false), wont)
59+
a.Equal(GoDefine(reflect.TypeFor[*object2](), nil, nil, false), wont)
5660

57-
a.Equal(GoDefine(reflect.TypeFor[int](), nil, false), "int")
61+
a.Equal(GoDefine(reflect.TypeFor[int](), nil, nil, false), "int")
5862

59-
a.Equal(GoDefine(reflect.TypeFor[string](), nil, false), "string")
63+
a.Equal(GoDefine(reflect.TypeFor[string](), nil, nil, false), "string")
6064

61-
a.Equal(GoDefine(reflect.TypeFor[func()](), nil, false), "")
65+
a.Equal(GoDefine(reflect.TypeFor[func()](), nil, nil, false), "")
6266

6367
m := map[reflect.Type]string{reflect.TypeFor[time.Time](): "string"}
6468

65-
a.Equal(GoDefine(reflect.TypeFor[time.Time](), m, false), "string")
66-
a.Equal(GoDefine(reflect.TypeFor[*time.Time](), m, false), "string")
69+
a.Equal(GoDefine(reflect.TypeFor[time.Time](), m, nil, false), "string")
70+
a.Equal(GoDefine(reflect.TypeFor[*time.Time](), m, nil, false), "string")
6771

68-
a.Equal(GoDefine(reflect.TypeFor[*object3](), m, false), "type object3 struct {\n\tT\tstring\t`json:\"t\"`\n}")
69-
a.Equal(GoDefine(reflect.TypeFor[*object3](), m, true), "type object3 struct {\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n}")
72+
a.Equal(GoDefine(reflect.TypeFor[*object3](), m, nil, false), "type object3 struct {\n\tT\tstring\t`json:\"t\"`\n}")
73+
a.Equal(GoDefine(reflect.TypeFor[*object3](), m, nil, true), "type object3 struct {\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n}")
7074

71-
a.Equal(GoDefine(reflect.TypeFor[*object4](), m, true), "type object4 struct {\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n}")
72-
a.Equal(GoDefine(reflect.TypeFor[*object4](), m, false), "type object4 struct {\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n}")
75+
a.Equal(GoDefine(reflect.TypeFor[*object4](), m, nil, true), "type object4 struct {\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n}")
76+
a.Equal(GoDefine(reflect.TypeFor[*object4](), m, nil, false), "type object4 struct {\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n}")
7377

74-
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}")
75-
a.Equal(GoDefine(reflect.TypeFor[*object5](), m, false), "type object5 struct {\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n\tStr\tstring\n}")
78+
a.Equal(GoDefine(reflect.TypeFor[*object5](), m, nil, true), "type object5 struct {\n\tint\tint\n\tT\tstring\t`json:\"t\"`\n\tInt\tint\n\tStr\tstring\n}")
79+
a.Equal(GoDefine(reflect.TypeFor[*object5](), m, c, false), "type object5 struct {\n\tT\tstring\t`json:\"t\"`\t// T\n\tInt\tint\t// Int\n\tStr\tstring\t// Str\n}")
7680

77-
a.Equal(GoDefine(reflect.TypeFor[time.Time](), m, false), "string")
78-
a.Equal(GoDefine(reflect.TypeFor[time.Time](), nil, false), "type Time struct {\n}")
81+
a.Equal(GoDefine(reflect.TypeFor[time.Time](), m, nil, false), "string")
82+
a.Equal(GoDefine(reflect.TypeFor[time.Time](), nil, nil, false), "type Time struct {\n}")
7983

80-
a.Equal(GoDefine(reflect.TypeFor[object6](), m, false), "type object6 struct {\n\tXMLName\tstruct {}\t`json:\"root\"`\n\tStr\t[]*struct {\n\t\tT\tstring\t`json:\"t\"`\n\t}\n}")
84+
a.Equal(GoDefine(reflect.TypeFor[object6](), m, nil, false), "type object6 struct {\n\tXMLName\tstruct {}\t`json:\"root\"`\n\tStr\t[]*struct {\n\t\tT\tstring\t`json:\"t\"`\n\t}\n}")
8185
}

0 commit comments

Comments
 (0)