diff --git a/README.md b/README.md index d4dde75..e2bb0f4 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/conf.template.yaml b/conf.template.yaml index 7a18d57..60f0c69 100644 --- a/conf.template.yaml +++ b/conf.template.yaml @@ -103,4 +103,19 @@ exports: id: 11 type: "golang" path: "./gen/code/message.go" - package: "Message" \ No newline at end of file + 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" \ No newline at end of file diff --git a/doc/custom_export.md b/doc/custom_export.md new file mode 100644 index 0000000..1312534 --- /dev/null +++ b/doc/custom_export.md @@ -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 +} +``` \ No newline at end of file diff --git a/doc/export_types.md b/doc/export_types.md index 6e599d6..9ec6d6d 100644 --- a/doc/export_types.md +++ b/doc/export_types.md @@ -28,4 +28,6 @@ - [x] [](#message)支持消息导出(无需单独导出类型,按相关语言导出即可)[案例](../gen/message.go) -- [x] [](#channel)支持列级别的渠道数据配置,通过配置或者命令行导出不同渠道数据 \ No newline at end of file +- [x] [](#channel)支持列级别的渠道数据配置,通过配置或者命令行导出不同渠道数据 + +- [x] [](#custom)支持自定义导出脚本,通过编写lua生成导出代码、数据 \ No newline at end of file diff --git a/generator/custom.go b/generator/custom.go index 1c09553..4b7b412 100644 --- a/generator/custom.go +++ b/generator/custom.go @@ -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 diff --git a/template/data-gen.lua b/template/data-gen.lua new file mode 100644 index 0000000..7d93922 --- /dev/null +++ b/template/data-gen.lua @@ -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 diff --git a/template/dts-gen.lua b/template/dts-gen.lua new file mode 100644 index 0000000..116d0df --- /dev/null +++ b/template/dts-gen.lua @@ -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