Skip to content

Commit

Permalink
Merge pull request #481 from kcl-lang/faq-merge-external-dynamic-config
Browse files Browse the repository at this point in the history
docs: add external dynamic config merge and json_merge_patch module faq documents
  • Loading branch information
Peefy authored Nov 6, 2024
2 parents c819b5b + f9d28a0 commit 44e7f0d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 deletions.
17 changes: 16 additions & 1 deletion docs/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,8 @@ configNew:
key2: value2
```



### The solution to the conflicting values on the attribute 'attr' between {value1} and {value2} error in KCL

When an error like conflicting values on the attribute 'attr' between {value1} and {value2} occurs in KCL, it is usually a problem with the use of the merge attribute operator `:`, indicating that when the `value1` and `value2` configurations are merged, the attribute A conflict error occurred at `attr`. In general, modify the attr attribute of value2 to other attribute operators, use `=` to indicate overwrite, and use `+=` to indicate addition
Expand All @@ -1736,6 +1738,19 @@ We can use the `=` attribute operator to modify it to the following form
data = {k: 1} | {k = 2} # Ok: the value 2 will override the value 1 through the `=` operator
```

### Use the `json_merge_patch` module to merge configuration

If we want to merge external configurations, as shown in the following code, we can use the `json_marge_patch` module, because the default attribute operator for external configurations is `: `, which may encounter merge conflict errors.

```python
_vals1 = yaml.decode(file.read("..."))
_vals2 = option("...")

_vals = _vals1 | _vals2
```

Please refer to [here](https://github.com/kcl-lang/modules/tree/main/json_merge_patch) for more information on how to use the `json_merge_patch` module.

## 38. How to traverse multiple elements at the same time in the for comprehension?

In KCL, we can use for comprehension to traverse multiple elements
Expand Down Expand Up @@ -2442,7 +2457,7 @@ data:
dataIsUnique: true
```

## 55. How do i omit attributes in the output for variables with "None" value?
## 55. How to omit attributes in the output for variables with "None" value?

In KCL, there is a builtin disableNone feature `-n` that does not print variables with null value.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,19 @@ data = {k: 1} | {k: 2} # Error: conflicting values on the attribute 'k' between
data = {k: 1} | {k = 2} # Ok: the value 2 will override the value 1 through the `=` operator
```

### 使用 `json_merge_patch` 库合并配置

如果我们对外部读取的配置有合并诉求,比如下面的代码显示的那样,则可以使用 `json_merge_patch` 库来操作,因为外部配置默认的属性运算符为 `:`, 可能会遇到合并冲突错误

```python
_vals1 = yaml.decode(file.read("..."))
_vals2 = option("...")

_vals = _vals1 | _vals2
```

`json_merge_patch` 库使用的方式详见[这里](https://github.com/kcl-lang/modules/tree/main/json_merge_patch)

## 38. KCL 中如何同时遍历多个元素

KCL 中可以使用 for 推导表达式遍历多个元素
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,19 @@ data = {k: 1} | {k: 2} # Error: conflicting values on the attribute 'k' between
data = {k: 1} | {k = 2} # Ok: the value 2 will override the value 1 through the `=` operator
```

### 使用 `json_merge_patch` 库合并配置

如果我们对外部读取的配置有合并诉求,比如下面的代码显示的那样,则可以使用 `json_merge_patch` 库来操作,因为外部配置默认的属性运算符为 `:`, 可能会遇到合并冲突错误

```python
_vals1 = yaml.decode(file.read("..."))
_vals2 = option("...")

_vals = _vals1 | _vals2
```

`json_merge_patch` 库使用的方式详见[这里](https://github.com/kcl-lang/modules/tree/main/json_merge_patch)

## 38. KCL 中如何同时遍历多个元素

KCL 中可以使用 for 推导表达式遍历多个元素
Expand Down
17 changes: 16 additions & 1 deletion versioned_docs/version-0.10/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,8 @@ configNew:
key2: value2
```



### The solution to the conflicting values on the attribute 'attr' between {value1} and {value2} error in KCL

When an error like conflicting values on the attribute 'attr' between {value1} and {value2} occurs in KCL, it is usually a problem with the use of the merge attribute operator `:`, indicating that when the `value1` and `value2` configurations are merged, the attribute A conflict error occurred at `attr`. In general, modify the attr attribute of value2 to other attribute operators, use `=` to indicate overwrite, and use `+=` to indicate addition
Expand All @@ -1736,6 +1738,19 @@ We can use the `=` attribute operator to modify it to the following form
data = {k: 1} | {k = 2} # Ok: the value 2 will override the value 1 through the `=` operator
```

### Use the `json_merge_patch` module to merge configuration

If we want to merge external configurations, as shown in the following code, we can use the `json_marge_patch` module, because the default attribute operator for external configurations is `: `, which may encounter merge conflict errors.

```python
_vals1 = yaml.decode(file.read("..."))
_vals2 = option("...")

_vals = _vals1 | _vals2
```

Please refer to [here](https://github.com/kcl-lang/modules/tree/main/json_merge_patch) for more information on how to use the `json_merge_patch` module.

## 38. How to traverse multiple elements at the same time in the for comprehension?

In KCL, we can use for comprehension to traverse multiple elements
Expand Down Expand Up @@ -2442,7 +2457,7 @@ data:
dataIsUnique: true
```

## 55. How do i omit attributes in the output for variables with "None" value?
## 55. How to omit attributes in the output for variables with "None" value?

In KCL, there is a builtin disableNone feature `-n` that does not print variables with null value.

Expand Down
2 changes: 1 addition & 1 deletion versioned_docs/version-0.7/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,7 @@ data:
dataIsUnique: true
```

## 55. How do i omit attributes in the output for variables with "None" value?
## 55. How to omit attributes in the output for variables with "None" value?

In KCL, there is a builtin disableNone feature `-n` that does not print variables with null value.

Expand Down
2 changes: 1 addition & 1 deletion versioned_docs/version-0.8/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,7 @@ data:
dataIsUnique: true
```

## 55. How do i omit attributes in the output for variables with "None" value?
## 55. How to omit attributes in the output for variables with "None" value?

In KCL, there is a builtin disableNone feature `-n` that does not print variables with null value.

Expand Down
2 changes: 1 addition & 1 deletion versioned_docs/version-0.9/user_docs/support/faq-kcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -2442,7 +2442,7 @@ data:
dataIsUnique: true
```

## 55. How do i omit attributes in the output for variables with "None" value?
## 55. How to omit attributes in the output for variables with "None" value?

In KCL, there is a builtin disableNone feature `-n` that does not print variables with null value.

Expand Down

0 comments on commit 44e7f0d

Please sign in to comment.