Skip to content

Commit

Permalink
更新文档,支持字段别名和字段变体
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Sep 18, 2024
1 parent 93c0686 commit ad5da27
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 31 deletions.
14 changes: 9 additions & 5 deletions docs/manual/commandtools.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
dotnet <path_of_luban.dll> [args]
args:
-s, --schemaCollector schema collector name
--conf Required. luban conf file
Expand All @@ -30,6 +28,8 @@ args:
-e, --excludeTag exclude tag
--variant field variants
-o, --outputTable output table
--timeZone time zone
Expand All @@ -40,12 +40,15 @@ args:
-x, --xargs args like -x a=1 -x b=2
-l, --logConfig (Default: nlog.xml) nlog config file
-w, --watchDir watch dir and regererate when dir changes
-v, --verbose verbose
--help Display this help screen.
--version Display version information.
```

|参数|必选|默认值|描述|
Expand All @@ -57,8 +60,9 @@ args:
|-d, --dataTarget|||生成的数据目标。可以有0-n个。如 `-d bin -d json`|
|-f, --forceLoadTableDatas||false|即使没有指定任何dataTarget也要强行加载配置数据,适用于在配置表提交前检查配置合法性|
|-p, --pipeline||default|生成管线。默认为内置的DefaultPipeline|
|-i, --includeTag|||包含该tag的记录会被输出到数据目标|
|-e, --excludeTag|||包含该tag的记录不会被输出到数据目标|
|-i, --includeTag|||tag为空或者为该tag的记录会被输出到数据目标,其他tag数据会被忽略。 --includeTag与--excludeTag不能同时存在|
|-e, --excludeTag|||包含该tag的记录不会被输出到数据目标。 --includeTag与--excludeTag不能同时存在|
|--variant|||指定使用的字段变体,格式为 `--variant {variantKey}={variantName}`,其中`{variantKey}``{beanFullName}.{fieldName}`(如 `test.TestVariant.value`)。 可以有多个`--variant`用于为不同的字段指定变体。详细文档见[变量变体](./variants)|
|-o, --outputTable|||指定要生成的table,可以有多个,例如`-o item.tbItem -o bag.TbBag`。如果未指定此参数,则按照group规则计算导出的table列表|
|--timeZone|||指定当前时区,默认取本地时区。此参数会影响datetime类型。该参数为linux或win下的时区名,例如 `Asia/Shanghai``China Standard Time`|
|--customTemplateDir|||自定义template搜索路径,优先级搜索此路径,再搜索默认的Templates路径|
Expand Down
4 changes: 4 additions & 0 deletions docs/manual/l10n.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- 时间本地化。即将配置中的datetime类型数据,按照指定地区转换为UTC秒数
- text类型。专用的表达本地化字符串key的类型,可以校验key的有效性
- 静态本地化。导出时将本地化key字符串转换为对应的实际语言值,避免运行时再转换的麻烦
- [字段变体](./variants)。为字段的每个本地化目标配置一个变体字段,导出时只导出指定变体的数据

## 基础概念

Expand Down Expand Up @@ -93,6 +94,9 @@ text语义上对应本地化字符串的key,Luban在数据校验器中会检
-x l10n.convertTextKeyToValue=1
```

## 字段变体

详见[字段变体](./variants)

## 收集配置表中所有text的列表

Expand Down
2 changes: 2 additions & 0 deletions docs/manual/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ Field的即bean的成员字段,定义如下:
|字段|类型|可空|描述|
|-|-|-|-|
|name|string||字段名|
|alias|string||字段别名。当从数据源解析字段数据时,如果按字段名找不到,则按别名查找|
|type|string||字段类型,详见[类型系统](./types)|
|comment|string||注释|
|tags|map,string,string|自定义tag对|
|NotNameValidation|bool||不检查字段名合法性|
|groups|list,string||分组|
|variants|list,string||字段变体,详见[字段变体](./variants)|

groups详细说明请阅读公共属性小节的文档。

Expand Down
99 changes: 99 additions & 0 deletions docs/manual/variants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# 字段变体(Variants)

有时候同一个字段可能有多个配置。一个非常常见的场景是制作本地化数据时,不同地区的某个初始道具有不同的值。

一个粗糙的做法类似这样:

|##var|id|item_id|item_id_zh|item_id_en|
|-|-|-|-|-|
|##type|int|int|int|int|
||1|1001|2001|3001|
||2|1002|2002|3002|

虽然这样确实可以工作,但有几个问题:

- 对于某个地区(如zh)来说,它并不需要其他地区的item_id,这个会增大了配置数据,也增加了无谓的内存开销
- 程序员需要根据当前地区,选择从 item_id_zh 还是 item_id_en读取数据,这带来不便

字段变体较好地解决了此问题。无论是哪个地区的item_id_{xxx},最终都会导出为一个公共的item_id字段,并且不包含其他用不到item_id_{yyy}。

## 定义

当前只有字段支持变体,所有可以定义字段的地方都可以定义变体信息。

### xml定义

```xml
<bean name="TestVariant">
<var name="id" type="int"/>
<var name="value" type="int" variants="zh,en,fr,jp"/>
</bean>
```

### \_\_beans\_\_.xlsx中定义

![variant_beans](/img/variant_beans.jpg)

### 在数据表的标题行定义

对于需要定义变体的字段,为每个变体定义一个`{fieldName}@{variant}`的变量。有几个规则:

- 变体标题头所在的列必须在变量名列之后,即 `value@en`必须在`value`列之后
- 不需要为变体变量定义`type``group`之类信息,变体变量直接使用原始变量相应的值,即`value@en``type`会取`value``type`

![variant_table_header](/img/variant_table_header.jpg)


## 配置变体数据

### excel格式族


![variant_table_header](/img/variant_table_header.jpg)

### json格式

```json
{
"id":1,
"value@en": 1001
}

```

### xml格式

```xml
<data>
<id>1</id>
<value variant="en">1001</value>
</data>
```

### yaml格式

```yml
id: 1
value@en: 1001
```
### lua格式
```lua
return {
id=1,
["value@en"] = 1001,
}
```

## 导出数据

一般来说,既然定义了变量变体,导出数据时应该为该变量指定当前应该使用的变体。命令参数`--variant {variantKey}={variantName}`用于为字段指定当前使用的变体。

`{variantKey}``{beanFullName}.{fieldName}`, `{variantName}`为变体名。 例如`--variant TestVariant.value=en`表示导出数据时value字段取`value_en`列对应的值。

在为某个变量指定了当前使用的变体后,如果对应的字段不存在,则取默认字段的值。同样地,以变体en为例,如果`value_en`列不存在,则取`value`列的值,如果`value`列也不存在,则抛出字段找不到的错误。

如果某个字段定义了变体,但没有在命令行中使用`--variant`指定该字段使用的变体名,则读取不含变量的原始变量的值。此时Luban会打印一行警告日志。


Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,45 @@ Thanks to the cross-platform capabilities of .net, Luban can run on mainstream W
dotnet <path_of_luban.dll> [args]
args:
-s, --schemaCollector schema collector name
-s, --schemaCollector schema collector name
--conf Required. luban conf file
--conf Required. luban conf file
-t, --target Required. target name
-t, --target Required. target name
-c, --codeTarget code target name
-c, --codeTarget code target name
-d, --dataTarget data target name
-d, --dataTarget data target name
-p, --pipeline pipeline name
-p, --pipeline pipeline name
-f, --forceLoadTableDatas force load table datas when not any dataTarget
-f, --forceLoadTableDatas force load table datas when not any dataTarget
-i, --includeTag include tag
-i, --includeTag include tag
-e, --excludeTag exclude tag
-e, --excludeTag exclude tag
--variant field variants
-o, --outputTable output table
-o, --outputTable output table
--timeZone time zone
--timeZone time zone
--customTemplateDir custom template dirs
--customTemplateDir custom template dirs
--validationFailAsError validation fail as error
--validationFailAsError validation fail as error
-x, --xargs args like -x a=1 -x b=2
-x, --xargs args like -x a=1 -x b=2
-v, --verbose verbose
-l, --logConfig (Default: nlog.xml) nlog config file
--help Display this help screen.
-w, --watchDir watch dir and regererate when dir changes
--version Display version information.
-v, --verbose verbose
--help Display this help screen.
--version Display version information.
```

Expand All @@ -57,8 +62,9 @@ args:
|-d, --dataTarget|No||The generated data target. There can be 0-n. Such as `-d bin -d json`|
|-f, --forceLoadTableDatas|No|false|Forcibly load the configuration data even if no dataTarget is specified, suitable for checking the legality of the configuration before submitting the configuration table|
|-p, --pipeline|No|default|Generate pipeline. Defaults to the built-in DefaultPipeline |
|-i, --includeTag|No||Records containing this tag will be output to the data target|
|-e, --excludeTag|No||Records containing this tag will not be output to the data target|
|-i, --includeTag|No||Records with empty tag or this tag will be output to the data target, and other tag data will be ignored. --includeTag and --excludeTag cannot exist at the same time|
|-e, --excludeTag|No||Records containing this tag will not be output to the data target. --includeTag and --excludeTag cannot exist at the same time|
|--variant|No||Specify the field variant to be used, the format is `--variant {variantKey}={variantName}`, where `{variantKey}` is `{beanFullName}.{fieldName}` (such as `test.TestVariant.value`). There can be multiple `--variant` to specify variants for different fields. For detailed documentation, see [variant variants](./variants)|
|-o, --outputTable|No||Specify the table to be generated, there can be multiple, for example `-o item.tbItem -o bag.TbBag`. If this parameter is not specified, the exported table list is calculated according to group rules |
|--timeZone|No||Specify the current time zone, which defaults to the local time zone. This parameter affects datetime types. This parameter is the time zone name under linux or win, such as `Asia/Shanghai` or `China Standard Time`|
|--customTemplateDir|No||Customize the template search path, search this path first, and then search the default Templates path|
Expand Down
4 changes: 4 additions & 0 deletions i18n/en/docusaurus-plugin-content-docs/current/manual/l10n.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The following types of localization are supported:
- Time localization. Convert the datetime type data in the configuration into UTC seconds according to the specified region
- text type. A dedicated type that expresses localized string keys and can verify the validity of the keys.
- Static localization. Convert the localized key string to the corresponding actual language value when exporting to avoid the trouble of re-conversion at runtime
- [Field variants](./variants). Configure a variant field for each localization target of the field, and only export the data of the specified variant when exporting

## Basic concepts

Expand Down Expand Up @@ -93,6 +94,9 @@ Only these parameters will enable static localization. Examples are as follows:
-x l10n.convertTextKeyToValue=1
```

## Field variants

See [Field variants](./variants) for details.

## Collect a list of all texts in the configuration table

Expand Down
17 changes: 10 additions & 7 deletions i18n/en/docusaurus-plugin-content-docs/current/manual/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ For groups, tags, and typeMappers, please read the detailed introduction in the

EnumItem is defined as follows:

| field | type | nullable | description |
| ------- | ----------------- | -------- | --------------------- |
| name | string | No | enumeration item name |
| alias | string | Yes | alias |
| value | string | Yes | enumeration value |
| comment | string | Yes | comment |
| tags | map,string,string | Yes | custom tag pair |
|Field|Type|Nullable|Description|
|-|-|-|-|
|name|string|No|Field name|
|alias|string|Yes|Field alias. When parsing field data from the data source, if the field name cannot be found, it is searched by alias|
|type|string|No|Field type, see [Type system](./types) for details|
|comment|string|Yes|Comment|
|tags|map,string,string|Custom tag pair|
|NotNameValidation|bool|No|Do not check the validity of field names|
|groups|list,string|Yes|Grouping|
|variants|list,string|Yes|Field variants, see [Field variants](./variants) for details|

If value is empty, it will automatically increment from the previous enumeration value, and if it is the first enumeration value, the value will be 0. value can be a decimal integer or a hexadecimal integer such as 0x10.
value can also be a combination or combination of other enumeration values, such as `A|B`.
Expand Down
97 changes: 97 additions & 0 deletions i18n/en/docusaurus-plugin-content-docs/current/manual/variants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Field Variants

Sometimes the same field may have multiple configurations. A very common scenario is that when making localized data, different regions have different values ​​for a certain initial path.

A rough approach is like this:

|##var|id|item_id|item_id_zh|item_id_en|
|-|-|-|-|-|
|##type|int|int|int|int|
||1|1001|2001|3001|
||2|1002|2002|3002|

Although this does work, there are several problems:

- For a certain region (such as zh), it does not need item_id in other regions, which will increase the configuration data and increase unnecessary memory overhead

- The programmer needs to choose to read data from item_id_zh or item_id_en according to the current region, which is inconvenient

Field variants solve this problem better. Regardless of the region, item_id_{xxx} will eventually be exported as a common item_id field, and will not include other unused item_id_{yyy}.

## Definition

Currently, only fields support variants. Variant information can be defined in all places where fields can be defined.

### XML definition

```xml
<bean name="TestVariant">
<var name="id" type="int"/>
<var name="value" type="int" variants="zh,en,fr,jp"/>
</bean>
```

### Define in \_\_beans\_\_.xlsx

![variant_beans](/img/variant_beans.jpg)

### Define in the header row of the data table

For fields that need to define variants, define a variable of `{fieldName}@{variant}` for each variant. There are several rules:

- The column where the variant header is located must be after the variable name column, that is, `value@en` must be after the `value` column

- There is no need to define information such as `type` and `group` for variant variables. Variant variables directly use the corresponding values ​​of the original variables, that is, the `type` of `value@en` will take the `type` of `value`.

![variant_table_header](/img/variant_table_header.jpg)

## Configure variant data

### excel format family

![variant_table_header](/img/variant_table_header.jpg)

### json format

```json
{
"id":1,
"value@en": 1001
}

```

### xml format

```xml
<data>
<id>1</id>
<value variant="en">1001</value>
</data>
```

### yaml format

```yml
id: 1
value@en: 1001
```
### lua format
```lua
return {
id=1,
["value@en"] = 1001,
}
```

## Exporting data

Generally speaking, since a variable variant is defined, the variant that should be used should be specified for the variable when exporting data. The command parameter `--variant {variantKey}={variantName}` is used to specify the currently used variant for the field.

`{variantKey}` is `{beanFullName}.{fieldName}`, `{variantName}` is the variant name. For example, `--variant TestVariant.value=en` means that when exporting data, the value field takes the value corresponding to the `value_en` column.

After specifying the currently used variant for a variable, if the corresponding field does not exist, the value of the default field is taken. Similarly, taking variant en as an example, if the `value_en` column does not exist, the value of the `value` column is taken. If the `value` column does not exist either, an error that the field cannot be found is thrown.

If a field has a variant defined, but the variant name used by the field is not specified using `--variant` in the command line, the value of the original variable without the variant is read. At this time, Luban will print a warning log.
Loading

0 comments on commit ad5da27

Please sign in to comment.