Skip to content

Commit 39703a7

Browse files
committed
docs: add more FAQ about KCL
Signed-off-by: peefy <xpf6677@163.com>
1 parent 4b11df7 commit 39703a7

File tree

2 files changed

+104
-0
lines changed
  • i18n/zh-CN/docusaurus-plugin-content-docs

2 files changed

+104
-0
lines changed

i18n/zh-CN/docusaurus-plugin-content-docs/current/user_docs/support/faq-kcl.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,3 +2467,55 @@ kcl main.k -n
24672467
```yaml
24682468
a: 1
24692469
```
2470+
2471+
## 56. 如何定义一个属性可以包含一个或多个不同定义 Schema?
2472+
2473+
在 KCL 中,我们可以使用联合类型来实现这一点。例如:
2474+
2475+
```python
2476+
schema Config:
2477+
route: EndPoint | Gateway
2478+
2479+
schema EndPoint:
2480+
attr: str
2481+
2482+
schema Gateway:
2483+
attr: str
2484+
```
2485+
2486+
## 57. 如何在 KCL 中转换字典和 Schema?
2487+
2488+
在 KCL 中,字典是一个动态数据,没有 Schema 的检查约束。我们可以将字典转换为 Schema 以获得约束条件。我们可以直接将字典数据分配给 Schema 类型数据,KCL 运行时会自动完成类型转换并执行类型检查。
2489+
2490+
```python
2491+
schema Person:
2492+
name: str
2493+
age: int
2494+
check:
2495+
age > 20
2496+
2497+
config = {
2498+
name = "Alice"
2499+
age = 25
2500+
}
2501+
2502+
alice: Person = config
2503+
```
2504+
2505+
## 58. 请解释在 KCL 字符串和字符串插值中 'r' 前缀的关系和用法。
2506+
2507+
在 KCL 中,我们可以使用 `${..}` 进行字符串插值。但在某些情况下,我们不希望进行转义。因此,我们可以通过在字符串文字前添加 'r' 或 'R' 前缀来创建原始字符串。下面是一个 KCL 代码示例:
2508+
2509+
```python
2510+
worldString = "world"
2511+
s = "Hello ${worldString}"
2512+
raw_s = r"Hello ${worldString}"
2513+
```
2514+
2515+
输出结果如下:
2516+
2517+
```yaml
2518+
worldString: world
2519+
s: Hello world
2520+
raw_s: Hello ${worldString}
2521+
```

i18n/zh-CN/docusaurus-plugin-content-docs/version-0.8.0/user_docs/support/faq-kcl.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,3 +2467,55 @@ kcl main.k -n
24672467
```yaml
24682468
a: 1
24692469
```
2470+
2471+
## 56. 如何定义一个属性可以包含一个或多个不同定义 Schema?
2472+
2473+
在 KCL 中,我们可以使用联合类型来实现这一点。例如:
2474+
2475+
```python
2476+
schema Config:
2477+
route: EndPoint | Gateway
2478+
2479+
schema EndPoint:
2480+
attr: str
2481+
2482+
schema Gateway:
2483+
attr: str
2484+
```
2485+
2486+
## 57. 如何在 KCL 中转换字典和 Schema?
2487+
2488+
在 KCL 中,字典是一个动态数据,没有 Schema 的检查约束。我们可以将字典转换为 Schema 以获得约束条件。我们可以直接将字典数据分配给 Schema 类型数据,KCL 运行时会自动完成类型转换并执行类型检查。
2489+
2490+
```python
2491+
schema Person:
2492+
name: str
2493+
age: int
2494+
check:
2495+
age > 20
2496+
2497+
config = {
2498+
name = "Alice"
2499+
age = 25
2500+
}
2501+
2502+
alice: Person = config
2503+
```
2504+
2505+
## 58. 请解释在 KCL 字符串和字符串插值中 'r' 前缀的关系和用法。
2506+
2507+
在 KCL 中,我们可以使用 `${..}` 进行字符串插值。但在某些情况下,我们不希望进行转义。因此,我们可以通过在字符串文字前添加 'r' 或 'R' 前缀来创建原始字符串。下面是一个 KCL 代码示例:
2508+
2509+
```python
2510+
worldString = "world"
2511+
s = "Hello ${worldString}"
2512+
raw_s = r"Hello ${worldString}"
2513+
```
2514+
2515+
输出结果如下:
2516+
2517+
```yaml
2518+
worldString: world
2519+
s: Hello world
2520+
raw_s: Hello ${worldString}
2521+
```

0 commit comments

Comments
 (0)