forked from davyxu/tabtoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_gen.go
139 lines (107 loc) · 2.8 KB
/
table_gen.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
// Generated by github.com/davyxu/tabtoy
// DO NOT EDIT!!
// Version: 2.9.1
package main
type TableEnumValue struct {
Name string
Index int32
}
type ActorType int32
const (
ActorType_None = 0 //
ActorType_Pharah = 1 // 法鸡
ActorType_Junkrat = 2 // 狂鼠
ActorType_Genji = 3 // 源氏
ActorType_Mercy = 4 // 天使
)
var (
ActorTypeEnumValues = []TableEnumValue{
{Name: "None", Index: 0}, //
{Name: "Pharah", Index: 1}, // 法鸡
{Name: "Junkrat", Index: 2}, // 狂鼠
{Name: "Genji", Index: 3}, // 源氏
{Name: "Mercy", Index: 4}, // 天使
}
ActorTypeMapperValueByName = map[string]int32{}
ActorTypeMapperNameByValue = map[int32]string{}
)
func (self ActorType) String() string {
name, _ := ActorTypeMapperNameByValue[int32(self)]
return name
}
type ExampleData struct {
ID int32 `tb_name:"任务ID"`
Name string `tb_name:"名称"`
Rate float32 `tb_name:"比例"`
Type ActorType `tb_name:"类型"`
Skill []int32 `tb_name:"技能列表"`
}
type ExampleKV struct {
ServerIP string `tb_name:"服务器IP"`
ServerPort uint16 `tb_name:"服务器端口"`
GroupID []int32 `tb_name:"分组"`
}
// Combine struct
type Table struct {
ExampleData []*ExampleData // table: ExampleData
ExampleKV []*ExampleKV // table: ExampleKV
// Indices
ExampleDataByID map[int32]*ExampleData `json:"-"` // table: ExampleData
// Handlers
postHandlers []func(*Table) error `json:"-"`
preHandlers []func(*Table) error `json:"-"`
}
// table: ExampleKV
func (self *Table) GetKeyValue_ExampleKV() *ExampleKV {
return self.ExampleKV[0]
}
// 注册加载后回调(用于构建数据)
func (self *Table) RegisterPostEntry(h func(*Table) error) {
if h == nil {
panic("empty postload handler")
}
self.postHandlers = append(self.postHandlers, h)
}
// 注册加载前回调(用于清除数据)
func (self *Table) RegisterPreEntry(h func(*Table) error) {
if h == nil {
panic("empty preload handler")
}
self.preHandlers = append(self.preHandlers, h)
}
// 调用PreHander,清除索引和数据
func (self *Table) ResetData() error {
for _, h := range self.preHandlers {
if err := h(self); err != nil {
return err
}
}
self.ExampleData = self.ExampleData[0:0]
self.ExampleKV = self.ExampleKV[0:0]
self.ExampleDataByID = map[int32]*ExampleData{}
return nil
}
// 构建索引,调用PostHander
func (self *Table) BuildData() error {
for _, v := range self.ExampleData {
self.ExampleDataByID[v.ID] = v
}
for _, h := range self.postHandlers {
if err := h(self); err != nil {
return err
}
}
return nil
}
// 初始化表实例
func NewTable() *Table {
self := &Table{}
self.ResetData()
return self
}
func init() {
for _, v := range ActorTypeEnumValues {
ActorTypeMapperValueByName[v.Name] = v.Index
ActorTypeMapperNameByValue[v.Index] = v.Name
}
}