forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypetab.go
184 lines (123 loc) · 3.54 KB
/
typetab.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package model
import (
"encoding/json"
"fmt"
"github.com/ahmetb/go-linq"
)
type TypeData struct {
Define *TypeDefine
Tab *DataTable // 类型引用的表
Row int // 类型引用的原始数据(DataTable)中的行
}
type TypeTable struct {
fields []*TypeData
}
func (self *TypeTable) ToJSON(all bool) []byte {
data, _ := json.MarshalIndent(self.AllFields(all), "", "\t")
return data
}
func (self *TypeTable) Print(all bool) {
fmt.Println(string(self.ToJSON(all)))
}
// refData,类型表对应源表的位置信息
func (self *TypeTable) AddField(tf *TypeDefine, data *DataTable, row int) {
if self.FieldByName(tf.ObjectType, tf.FieldName) != nil {
panic("Duplicate table field: " + tf.FieldName)
}
self.fields = append(self.fields, &TypeData{
Tab: data,
Define: tf,
Row: row,
})
}
func (self *TypeTable) Raw() []*TypeData {
return self.fields
}
func (self *TypeTable) AllFields(all bool) (ret []*TypeDefine) {
linq.From(self.fields).WhereT(func(td *TypeData) bool {
if !all && td.Define.IsBuiltin {
return false
}
return true
}).SelectT(func(td *TypeData) interface{} {
return td.Define
}).ToSlice(&ret)
return
}
// 类型是枚举
func (self *TypeTable) IsEnumKind(objectType string) bool {
return linq.From(self.rawEnumNames(true)).WhereT(func(name string) bool {
return name == objectType
}).Count() == 1
}
// 匹配枚举值
func (self *TypeTable) ResolveEnumValue(objectType, value string) (ret string) {
linq.From(self.fields).WhereT(func(td *TypeData) bool {
return td.Define.ObjectType == objectType &&
(td.Define.Name == value || td.Define.FieldName == value)
}).ForEachT(func(td *TypeData) {
ret = td.Define.Value
})
return
}
func (self *TypeTable) EnumNames() (ret []string) {
return self.rawEnumNames(BuiltinSymbolsVisible)
}
func (self *TypeTable) StructNames() (ret []string) {
return self.rawStructNames(BuiltinSymbolsVisible)
}
// 获取所有的结构体名
func (self *TypeTable) rawStructNames(all bool) (ret []string) {
linq.From(self.fields).WhereT(func(td *TypeData) bool {
tf := td.Define
if !all && tf.IsBuiltin {
return false
}
return tf.Kind == TypeUsage_HeaderStruct
}).SelectT(func(td *TypeData) string {
return td.Define.ObjectType
}).Distinct().ToSlice(&ret)
return
}
// 获取所有的枚举名
func (self *TypeTable) rawEnumNames(all bool) (ret []string) {
linq.From(self.fields).WhereT(func(td *TypeData) bool {
tf := td.Define
if !all && tf.IsBuiltin {
return false
}
return tf.Kind == TypeUsage_Enum
}).SelectT(func(td *TypeData) string {
return td.Define.ObjectType
}).Distinct().ToSlice(&ret)
return
}
// 对象的所有字段
func (self *TypeTable) AllFieldByName(objectType string) (ret []*TypeDefine) {
linq.From(self.fields).WhereT(func(td *TypeData) bool {
return td.Define.ObjectType == objectType
}).SelectT(func(td *TypeData) *TypeDefine {
return td.Define
}).ToSlice(&ret)
return
}
// 数据表中表头对应类型表
func (self *TypeTable) FieldByName(objectType, name string) (ret *TypeDefine) {
linq.From(self.fields).WhereT(func(td *TypeData) bool {
tf := td.Define
return tf.ObjectType == objectType &&
(tf.Name == name || tf.FieldName == name)
}).ForEachT(func(td *TypeData) {
ret = td.Define
})
return
}
func (self *TypeTable) ObjectExists(objectType string) bool {
return linq.From(self.fields).WhereT(func(td *TypeData) bool {
return td.Define.ObjectType == objectType
}).Count() > 0
}
func NewSymbolTable() *TypeTable {
return new(TypeTable)
}
var BuiltinSymbolsVisible bool