Skip to content

Commit

Permalink
增加自定导出说明文档
Browse files Browse the repository at this point in the history
  • Loading branch information
wingcd committed Jul 11, 2024
1 parent ac0d873 commit c37d854
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ __Step.4 运行程序__
- [字段规则](./doc/field_types.md#规则)
- [导出配置](./doc/config.md)
- [导出支持](./doc/export_types.md)
- [自定义导出](./doc/custom_export.md)
- [读取支持](./doc/reader.md)
- [CSHARP/JS/TS](./doc/reader.md#CSHARP)
- [GOLANG](./doc/reader.md#GOLANG)
Expand Down
17 changes: 16 additions & 1 deletion conf.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,19 @@ exports:
id: 11
type: "golang"
path: "./gen/code/message.go"
package: "Message"
package: "Message"
-
id: 12
type: "json"
path: "./gen/data/json/"
excludes: "5"
-
id: 13
type: "custom"
path: "./gen/data/json/CustomTest/all.csv"
template: "./template/data-gen.lua"
-
id: 14
type: "custom"
path: "./gen/data/json/CustomTest/all.d.ts"
template: "./template/dts-gen.lua"
123 changes: 123 additions & 0 deletions doc/custom_export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#### 自定义导出
导出类型为`type:custom`,且导出脚本配置为`template:对应的lua脚本`时,程序将执行对应的lua脚本生成数据:
* 项目示例导出csv脚本为:[csv_export](../template/data-gen.lua)
* 项目示例导出csv对应dts脚本为:[dts_export](../template/dts-gen.lua)

脚本中可以通过打印json查看所有数据结构:
```lua
print(GXE.json_encode(GXE))
```

lua中可以使用的入口为GXE,一下为数据表简单描述:
```golang
type BuildInfo struct {
Imports []string
Output string
Template string
}

type commonFileDesc struct {
Version string
GoProtoVersion string
HasMessage bool
}

// 定义表类型(同类型分组)
type DefineTableInfo struct {
StartID int64
// 类型(enum/struct)
Category string
// 类型名
TypeName string
// 表名
DefinedTable string
// 类型子项
Items []*DefineTableItem
}

type StructInfo struct {
// 描述(注释)
Desc string
// 字段名
FieldName string
// 首字母大写字段名
TitleFieldName string
// 表中定义的原始类型
RawValueType string
// 转换后的标准类型
StandardValueType string
// protobuf类型
PBValueType string
// 转换后的值类型
ValueType string
// 二进制编码方式
EncodeType string
// 基础值是否枚举
IsEnum bool
// 基础值是否结构
IsStruct bool
// 基础值是否数组
IsArray bool
// 编号(1开始)
Index int
// 数组分隔符,默认为全局配置符号
ArraySplitChar string
// 此字段是否可转换对象
Convertable bool
// 换换后的类型是否需要缓存
Cachable bool
IsVoid bool
// 是否消息类型
IsMessage bool
// 别名(可转换对象显示类型)
Alias string
// 字段限制规则
Rule int
}

// 定义表数据项(按行)
type DefineTableItem struct {
StructInfo
//
Value string
}

// 数据表
type DataTable struct {
Id int
// 类型名
TypeName string
// 表头
Headers []*DataTableHeader
// 表文件名
DefinedTable string
// 数据
Data [][]string
// 表类型
TableType ETableType
// 是否数组
IsArray bool
// 是否需要增加子项
NeedAddItems bool
}

type customFileDesc struct {
commonFileDesc

Namespace string
Info *BuildInfo
// 单个文件导出时使用
Table *model.DataTable

Enums []*model.DefineTableInfo
Consts []*model.DefineTableInfo
Tables []*model.DataTable
}

// 导出到lua的数据结构GXE
type LuaExportInfo struct {
version string
info *BuildInfo
fileDesc *customFileDesc
}
```
4 changes: 3 additions & 1 deletion doc/export_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@

- [x] [](#message)支持消息导出(无需单独导出类型,按相关语言导出即可)[案例](../gen/message.go)

- [x] [](#channel)支持列级别的渠道数据配置,通过配置或者命令行导出不同渠道数据
- [x] [](#channel)支持列级别的渠道数据配置,通过配置或者命令行导出不同渠道数据

- [x] [](#custom)支持自定义导出脚本,通过编写lua生成导出代码、数据
3 changes: 2 additions & 1 deletion generator/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ type customFileDesc struct {
Info *BuildInfo
Enum *model.DefineTableInfo
Const *model.DefineTableInfo
Table *model.DataTable
// 单个文件导出时使用
Table *model.DataTable

Enums []*model.DefineTableInfo
Consts []*model.DefineTableInfo
Expand Down
71 changes: 71 additions & 0 deletions template/data-gen.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
ValueTypes = {
["int"] = "i",
["int32"] = "i",
["uint"] = "i",
["uint32"] = "i",
["float"] = "f",
["float32"] = "f",
["string"] = "s",
["bool"] = "b",
["int[]"] = "ia",
["uint[]"] = "ia",
["float[]"] = "fa",
["float32[]"] = "fa",
["string[]"] = "sa",
["bool[]"] = "ba",
}

function genTableDesc(t)
line = t.TypeName
for i,v in pairs(t.Headers) do
structInfo = v.StructInfo
if nil == ValueTypes[structInfo.RawValueType] then
print("Error: " .. structInfo.RawValueType)
goto continue
end

line = line .. ":" .. structInfo.FieldName .. "-" .. ValueTypes[structInfo.RawValueType]
::continue::
end
return line .. "\n"
end

function genTableData(t)
ret = "##########\n"
ret = ret .. t.TypeName .. "\n"

if t.TypeName == "novice" then
-- print(GXE.json_encode(t.Data))
end

for i,v in pairs(t.Data) do
line = ""
idx = 0
for _,v2 in pairs(v) do
if idx == 0 then
line = string.format("%s", v2)
else
line = line .. "," .. string.format("%s", v2)
end
idx = idx + 1
end
ret = ret .. line .. "\n"
end
return ret
end

function generate()
ret = ""
for i,v in pairs(GXE.fileDesc.Tables) do
if not v.IsArray then
ret = ret .. genTableDesc(v)
end
end

for i,v in pairs(GXE.fileDesc.Tables) do
if not v.IsArray then
ret = ret .. genTableData(v)
end
end
return ret
end
57 changes: 57 additions & 0 deletions template/dts-gen.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
ValueTypes = {
["int"] = "number",
["int32"] = "number",
["uint"] = "number",
["uint32"] = "number",
["float"] = "number",
["float32"] = "number",
["string"] = "string",
["bool"] = "boolean",
["int[]"] = "number[]",
["uint[]"] = "number[]",
["float[]"] = "number[]",
["float32[]"] = "number[]",
["string[]"] = "string[]",
["bool[]"] = "boolean[]",
}

function genItem(t)
line = " interface " .. t.TypeName .. "Item {\n"
for i,v in pairs(t.Headers) do
structInfo = v.StructInfo
if nil == ValueTypes[structInfo.RawValueType] then
print("Error: " .. structInfo.RawValueType)
goto continue
end

line = line .. " /** " .. structInfo.Desc .. " */\n"
line = line .. " readonly " .. structInfo.FieldName .. ": " .. ValueTypes[structInfo.RawValueType] .. ";\n"
::continue::
end
return line .. " }\n\n"
end

function genTable(t)
line = " let " .. t.TypeName .. ": {[key: number]: " .. t.TypeName .. "Item };\n"
return line
end

function generate()
ret = "//由工具自动生成的代码,请勿手动修改!\n"
ret = ret .. "declare namespace Configs {\n"

for i,v in pairs(GXE.fileDesc.Tables) do
if not v.IsArray then
ret = ret .. genItem(v)
end
end

for i,v in pairs(GXE.fileDesc.Tables) do
if not v.IsArray then
ret = ret .. genTable(v)
end
end

ret = ret .. "}\n"
return ret
end

0 comments on commit c37d854

Please sign in to comment.