Skip to content

Commit 983be43

Browse files
committed
增加: lua格式的字段索引功能
1 parent 39c10eb commit 983be43

14 files changed

+164
-50
lines changed

README.md

+54-22
Original file line numberDiff line numberDiff line change
@@ -148,29 +148,9 @@ Actor {ID: 104 Name: "邋遢大王" Struct {HP: 10 AttackRate: 1} BuffID: 0 Buff
148148
149149
```
150150

151-
# 导出格式
152-
默认导出pbt, 通过参数可以导出lua, json格式
153-
* --fmt=pbt
154-
默认导出pbt
155-
* --fmt=lua
156-
导出以return开头的lua文件, 通过require的返回值回去表格table
157-
兼容pbc格式, 枚举值以值名字字符串导出, 64位以字符串方式导出
158-
* --fmt=json
159-
标准json, 与proto定义的结构导出json相同
160-
161-
# Protobuf Text格式(pbt)
162-
Google Protobuf 官方支持的格式
163-
可通过官方protobuf库读取,写入这种格式
164-
与json区别在于:
165-
json的字段名必须是带双引号, 且数组需要用[]圈住, 多重字段尾部必须加逗号
166-
* 格式
167-
key1: value1 key2: value2
168-
冒号组合key,value, 空格分隔字段
169-
170-
\#作为注释
171-
172151
# Proto文件规则
173152
proto文件格式范例参考test/test.proto
153+
174154
需要配合github.com/davyxu/pbmeta的protobuf插件protoc-gen-meta导出proto文件的meta信息
175155

176156
在proto的字段后方的注释中以[tabtoy]开头的注释将被理解为meta信息, 用于描述字段导出功能修饰
@@ -184,8 +164,49 @@ proto文件格式范例参考test/test.proto
184164
https://github.com/davyxu/tabtoy/blob/master/test/test.proto
185165

186166

167+
# 导出格式
168+
默认导出pbt, 通过参数可以导出lua, json格式
169+
170+
## Protobuf 文本格式(*.pbt)
171+
参数: --fmt=pbt
172+
173+
Google Protobuf 官方支持的格式
174+
175+
可通过官方protobuf库读取,写入这种格式
176+
177+
与json区别在于:
178+
179+
json的字段名必须是带双引号, 且数组需要用[]圈住, 多重字段尾部必须加逗号
180+
181+
* 格式
182+
key1: value1 key2: value2
183+
冒号组合key,value, 空格分隔字段
184+
185+
\#作为注释
186+
187+
默认导出pbt
188+
189+
190+
## Lua格式(*.lua)
191+
参数: --fmt=lua
192+
导出以return开头的lua文件, 通过require的返回值回去表格table
193+
兼容pbc格式, 枚举值以值名字字符串导出, 64位以字符串方式导出
194+
195+
### lua导出字段的索引
196+
197+
为了方便lua导出文件的使用, 可以创建字段索引, 步骤如下, 参考test/test.proto:
198+
199+
* 为ActorDefine消息的ID字段和Name字段增加描述LuaMapper: true
200+
201+
## json格式(*.json)
202+
参数: --fmt=json
203+
标准json, 与proto定义的结构导出json相同
204+
205+
206+
187207

188-
# 电子表格文件头格式
208+
209+
# 电子表格格式及写法
189210

190211
## 导出头
191212

@@ -242,6 +263,17 @@ Proto字段列, 必须放在第二行
242263

243264
支持在Struct中的字段添加Alias别名以使用别名方式的字段或中文字段描述单元格
244265

266+
范例步骤: 请参考test/test.proto
267+
268+
* 为Prop消息的需要的字段增加描述 AttackRate 为Alias:"攻击速率" ExType为Alias:"额外类型" 等
269+
270+
* 确认StrStruct字段拥有描述String2Struct: true
271+
272+
* 在StrStruct的单元格里填写 血量: 3 攻击速率: 1 额外类型:超能
273+
274+
* 导出测试
275+
276+
245277
## 字段重复性检查
246278

247279
格式: RepeatCheck: true

filter/val2struct.go

-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ func Value2Struct(meta *tool.FieldMeta, structValue string, fd *pbmeta.FieldDesc
142142

143143
p.NextToken()
144144

145-
log.Debugln("end of term")
146145
}
147146

148147
return

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func main() {
4343

4444
// 版本
4545
if *paramVersion {
46-
fmt.Println("tabtoy 1.1.1")
46+
fmt.Println("tabtoy 1.2.0")
4747
return
4848
}
4949

mode_xls2pbt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func setFieldValue(ri *scanner.RecordInfo, fieldName, value string) bool {
175175

176176
type sheetData struct {
177177
name string
178-
msg *data.DynamicMessage
178+
msg *data.DynamicMessage // 对应XXFile
179179
}
180180

181181
func getOutputExt() string {

printer/lua.go

+63-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,73 @@ func (self *luaWriter) WriteFieldSpliter() {
7373
self.printer.WriteString(", ")
7474
}
7575

76+
// msg类型=XXFile
7677
func (self *luaWriter) PrintMessage(msg *data.DynamicMessage) {
7778

78-
self.printer.WriteString("return {\n\n")
79+
self.printer.WriteString("local data = {\n\n")
80+
7981
rawWriteMessage(self.printer, self, msg, 0)
8082

81-
self.printer.WriteString("\n\n}")
83+
self.printer.WriteString("\n\n}\n")
84+
85+
/*
86+
87+
data.ActorByID = {}
88+
for _, rec in pairs( data.Actor ) do
89+
90+
data.ActorByID[rec.ID] = rec
91+
92+
end
93+
94+
*/
95+
96+
// 输出lua索引
97+
fdset, lineFieldName := findMapperField(msg)
98+
99+
for _, fd := range fdset {
100+
101+
mapperVarName := fmt.Sprintf("data.%sBy%s", lineFieldName, fd.Name())
102+
103+
self.printer.WriteString("\n-- " + fd.Name() + "\n")
104+
self.printer.WriteString(mapperVarName + " = {}\n")
105+
self.printer.WriteString("for _, rec in pairs(data." + lineFieldName + ") do\n")
106+
self.printer.WriteString("\t" + mapperVarName + "[rec." + fd.Name() + "] = rec\n")
107+
self.printer.WriteString("end\n")
108+
}
109+
110+
self.printer.WriteString("\nreturn data")
111+
}
112+
113+
func findMapperField(msg *data.DynamicMessage) (fdset []*pbmeta.FieldDescriptor, lineFieldName string) {
114+
115+
var lineMsgDesc *pbmeta.Descriptor
116+
// 找到行描述符
117+
for i := 0; i < msg.Desc.FieldCount(); i++ {
118+
fd := msg.Desc.Field(i)
119+
120+
if fd.IsRepeated() {
121+
lineMsgDesc = fd.MessageDesc()
122+
lineFieldName = fd.Name()
123+
break
124+
}
125+
}
126+
127+
// 在结构中寻找需要导出的lua字段
128+
for i := 0; i < lineMsgDesc.FieldCount(); i++ {
129+
fd := lineMsgDesc.Field(i)
130+
meta := data.GetFieldMeta(fd)
131+
if meta == nil {
132+
continue
133+
}
134+
135+
if !meta.LuaMapper {
136+
continue
137+
}
138+
139+
fdset = append(fdset, fd)
140+
}
141+
142+
return
82143
}
83144

84145
func NewLuaWriter(printer *bytes.Buffer) IWriter {

proto/tool.proto

+2
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ message FieldMeta
2525
bool String2Struct = 5; // 字符串转为结构体
2626

2727
string DefaultValue = 6; // 默认值, 比pb2的默认值有更高的优先度
28+
29+
bool LuaMapper = 7; // 为字段创建lua记录索引
2830
}

proto/tool/tool.pb.go

+17-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scanner/sheet.go

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/davyxu/pbmeta"
77
pbprotos "github.com/davyxu/pbmeta/proto"
88
"github.com/davyxu/tabtoy/data"
9-
"github.com/davyxu/tabtoy/proto/filter"
109
"github.com/davyxu/tabtoy/proto/tool"
1110
"github.com/tealeg/xlsx"
1211
)

test/Actor.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"Actor" : [
44
{"ID": 100, "Name": "黑猫警长", "Struct" : {"HP": 100, "AttackRate": 0.6}, "BuffID" : [0, 0], "SkillID" : [4, 6, 7], "StrStruct" : []},
55
{"ID": 101, "Name": "葫芦\n", "Struct" : {"HP": 10, "AttackRate": 0.8}, "BuffID" : [3, 1], "Type": 21, "SkillID" : [1], "StrStruct" : []},
6-
{"ID": 102, "Name": "\"\"", "Struct" : {"HP": 10, "AttackRate": 0.7}, "BuffID" : [0, 0], "SkillID" : [0], "StrStruct" : [{"HP": 2}, {"AttackRate": 0.5}, {"HP": 3, "AttackRate": 1}]},
6+
{"ID": 102, "Name": "\"\"", "Struct" : {"HP": 10, "AttackRate": 0.7}, "BuffID" : [0, 0], "SkillID" : [0], "StrStruct" : [{"HP": 2}, {"AttackRate": 0.5}, {"HP": 3, "AttackRate": 1, "ExType": 21}]},
77
{"ID": 103, "Name": "\n", "Struct" : {"HP": 205}, "BuffID" : [0, 0], "SkillID" : [0], "StrStruct" : []},
88
{"ID": 104, "Name": "邋遢大王", "Struct" : {"HP": 10, "AttackRate": 1}, "BuffID" : [0, 0], "SkillID" : [0], "StrStruct" : []}
99
]

test/Actor.lua

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
-- Generated by github.com/davyxu/tabtoy
2-
return {
2+
local data = {
33

44
Actor = {
55
{ID = "100", Name = "黑猫警长", Struct = {HP = 100, AttackRate = 0.6}, BuffID = 0, BuffID = 0, SkillID = 4, SkillID = 6, SkillID = 7, StrStruct = {{}, {}, {}}},
66
{ID = "101", Name = "葫芦\n", Struct = {HP = 10, AttackRate = 0.8}, BuffID = 3, BuffID = 1, Type = "Power", SkillID = 1, StrStruct = {{}, {}, {}}},
7-
{ID = "102", Name = "\"\"", Struct = {HP = 10, AttackRate = 0.7}, BuffID = 0, BuffID = 0, SkillID = 0, StrStruct = {{HP = 2}, {AttackRate = 0.5}, {HP = 3, AttackRate = 1}}},
7+
{ID = "102", Name = "\"\"", Struct = {HP = 10, AttackRate = 0.7}, BuffID = 0, BuffID = 0, SkillID = 0, StrStruct = {{HP = 2}, {AttackRate = 0.5}, {HP = 3, AttackRate = 1, ExType = "Power"}}},
88
{ID = "103", Name = "\n", Struct = {HP = 205}, BuffID = 0, BuffID = 0, SkillID = 0, StrStruct = {{}, {}, {}}},
99
{ID = "104", Name = "邋遢大王", Struct = {HP = 10, AttackRate = 1}, BuffID = 0, BuffID = 0, SkillID = 0, StrStruct = {{}, {}, {}}}
1010
}
1111

12-
}
12+
}
13+
14+
-- ID
15+
data.ActorByID = {}
16+
for _, rec in pairs(data.Actor) do
17+
data.ActorByID[rec.ID] = rec
18+
end
19+
20+
-- Name
21+
data.ActorByName = {}
22+
for _, rec in pairs(data.Actor) do
23+
data.ActorByName[rec.Name] = rec
24+
end
25+
26+
return data

test/Actor.pbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by github.com/davyxu/tabtoy
22
Actor {ID: 100 Name: "黑猫警长" Struct {HP: 100 AttackRate: 0.6} BuffID: 0 BuffID: 0 SkillID: 4 SkillID: 6 SkillID: 7 }
33
Actor {ID: 101 Name: "葫芦\n" Struct {HP: 10 AttackRate: 0.8} BuffID: 3 BuffID: 1 Type: Power SkillID: 1 }
4-
Actor {ID: 102 Name: "\"\"" Struct {HP: 10 AttackRate: 0.7} BuffID: 0 BuffID: 0 SkillID: 0 StrStruct {HP: 2} StrStruct {AttackRate: 0.5} StrStruct {HP: 3 AttackRate: 1}}
4+
Actor {ID: 102 Name: "\"\"" Struct {HP: 10 AttackRate: 0.7} BuffID: 0 BuffID: 0 SkillID: 0 StrStruct {HP: 2} StrStruct {AttackRate: 0.5} StrStruct {HP: 3 AttackRate: 1 ExType: Power}}
55
Actor {ID: 103 Name: "\n" Struct {HP: 205} BuffID: 0 BuffID: 0 SkillID: 0 }
66
Actor {ID: 104 Name: "邋遢大王" Struct {HP: 10 AttackRate: 1} BuffID: 0 BuffID: 0 SkillID: 0 }

test/Actor.xlsx

22 Bytes
Binary file not shown.

test/test.lua

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local t = require "Actor"
2+
3+
print(t.ActorByID["103"].ID)
4+
5+
print(t.ActorByName["黑猫警长"].ID)

test/test.proto

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ message Prop
2020
message ActorDefine
2121
{
2222
// 唯一ID
23-
int64 ID = 1; // [tabtoy] RepeatCheck: true #ID重复检查
23+
int64 ID = 1; // [tabtoy] RepeatCheck: true LuaMapper: true #ID重复检查 及lua索引
2424

2525
// 角色名称
26-
string Name = 5;
26+
string Name = 5; // [tabtoy] LuaMapper: true
2727

2828
Prop Struct = 10;
2929

0 commit comments

Comments
 (0)