Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

表是带 source 的行集合。表 schema 声明表模式、source 位置、字段和可选索引。

Modes

ModeShapeTypical Use
map通过一个字段作为 key 的行集合。道具、任务、等级、buff。
list没有 keyed lookup 的有序行集合。掉落项、权重池、有序步骤。
singleton单行。全局配置、调参常量。
[[tables]]
name = "Item"
mode = "map"
key = "id"

[[tables.fields]]
name = "id"
type = "i32"

对于 map 表,key 指定表的主键字段。Sora 会用它做行唯一性校验、生成 lookup API、生成 Excel 模板提示,并校验 ref<Table.key>

Source

[tables.source]
format = "xlsx"
file = "Core.xlsx"
sheet = "Item"

当项目或命令提供默认 source format 时,format 可以省略。导出和校验时,file 会基于命令的 --data-root 解析。

内置 source format 包括 xlsxcsvtomljsonyaml。JSON 和 YAML 表文件是 row object 数组:

[
  { "id": 1001, "name": "Iron Sword" },
  { "id": 1002, "name": "Health Potion" }
]

对 JSON 和 YAML,file 也可以指向目录。这种情况下,Sora 会递归读取每个匹配的 .json.yaml.yml 文件,每个文件作为一条 row object,并按路径排序。

Indexes

索引是表上的额外查询入口。它和 mode = "map"key 不一样:

概念用途
table key表的主键。map 表必须靠它保证每行唯一,并生成主要的 get(id) 查询。
[[tables.indexes]]额外查询方式。比如按名字查、按类型分组、按关卡查掉落。

例如 Item 表的主键是 id,运行时代码通常会按 id 取一个道具:

[[tables]]
name = "Item"
mode = "map"
key = "id"

[[tables.fields]]
name = "id"
type = "i32"

[[tables.fields]]
name = "name"
type = "string"

[[tables.fields]]
name = "item_type"
type = "enum<ItemType>"

如果还希望按 name 查道具,可以加一个 unique index:

[[tables.indexes]]
name = "by_name"
fields = ["name"]
unique = true

对应数据可以长这样:

idnameitem_type
1001Iron SwordWeapon
1002Wood ShieldArmor

unique = true 表示 name 不能重复。生成代码在支持该目标时会提供类似 get_by_name("Iron Sword") 的 helper,返回单行或空值。

如果希望按分类拿到多行,就用非 unique index:

[[tables.indexes]]
name = "by_item_type"
fields = ["item_type"]
unique = false

对应数据:

idnameitem_type
1001Iron SwordWeapon
1002Bronze AxeWeapon
2001Wood ShieldArmor

unique = false 表示同一个 key 可以匹配多行。生成代码在支持该目标时会提供类似 get_by_item_type(ItemType::Weapon) 的 helper,返回匹配行列表。

fields 是列表,因此 unique index 也可以表达组合唯一性:

[[tables.indexes]]
name = "by_world_stage"
fields = ["world", "stage"]
unique = true

这会要求 (world, stage) 组合不能重复。例如 (1, 1) 只能出现一次,(1, 2) 可以再出现一次。当前生成 lookup helper 主要支持非 singleton 表上的单字段 index;组合 index 更适合先用于数据校验。

Validation

加载 source 数据后,Sora 会校验表行:

  • 非 optional 字段必须存在,除非有 default;
  • map 表的 key 字段必须唯一;
  • enum value 必须有效;
  • reference 必须指向已有行;
  • numeric range 和 length range 必须通过;
  • parser 输出必须匹配声明的字段类型。