forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
115 lines (87 loc) · 2.05 KB
/
type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package printer
import (
"encoding/json"
"github.com/BobWong/tabtoy/v2/i18n"
"github.com/BobWong/tabtoy/v2/model"
)
type typePrinter struct {
}
// 一个列字段
type typeFieldModel struct {
Name string
Type string
Kind string
IsRepeated bool
Meta map[string]interface{}
Comment string
Value int
}
// 一张表的类型信息
type typeStructModel struct {
Name string
Fields []*typeFieldModel
}
// 整个文件类型信息
type typeFileModel struct {
Tool string
Version string
Structs []*typeStructModel
Enums []*typeStructModel
}
func (self *typePrinter) Run(g *Globals) *Stream {
bf := NewStream()
var fm typeFileModel
fm.Tool = "github.com/davyxu/tabtoy"
fm.Version = g.Version
// 遍历所有类型
for _, d := range g.FileDescriptor.Descriptors {
// 这给被限制输出
if !d.File.MatchTag(".type") {
log.Infof("%s: %s", i18n.String(i18n.Printer_IgnoredByOutputTag), d.Name)
continue
}
structM := &typeStructModel{
Name: d.Name,
}
// 遍历字段
for _, fd := range d.Fields {
// 对CombineStruct的XXDefine对应的字段
if d.Usage == model.DescriptorUsage_CombineStruct {
// 这个字段被限制输出
if fd.Complex != nil && !fd.Complex.File.MatchTag(".type") {
continue
}
}
field := &typeFieldModel{
Name: fd.Name,
Type: fd.TypeString(),
Kind: fd.KindString(),
IsRepeated: fd.IsRepeated,
Comment: fd.Comment,
Meta: fd.Meta.Raw(),
}
switch d.Kind {
case model.DescriptorKind_Struct:
field.Value = 0
case model.DescriptorKind_Enum:
field.Value = int(fd.EnumValue)
}
structM.Fields = append(structM.Fields, field)
}
switch d.Kind {
case model.DescriptorKind_Struct:
fm.Structs = append(fm.Structs, structM)
case model.DescriptorKind_Enum:
fm.Enums = append(fm.Enums, structM)
}
}
data, err := json.MarshalIndent(&fm, "", " ")
if err != nil {
log.Errorln(err)
}
bf.WriteBytes(data)
return bf
}
func init() {
RegisterPrinter("type", &typePrinter{})
}