Skip to content

Commit 44e7f0d

Browse files
authored
Merge pull request #481 from kcl-lang/faq-merge-external-dynamic-config
docs: add external dynamic config merge and json_merge_patch module faq documents
2 parents c819b5b + f9d28a0 commit 44e7f0d

File tree

7 files changed

+61
-5
lines changed

7 files changed

+61
-5
lines changed

docs/user_docs/support/faq-kcl.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,8 @@ configNew:
17201720
key2: value2
17211721
```
17221722

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

17251727
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
@@ -1736,6 +1738,19 @@ We can use the `=` attribute operator to modify it to the following form
17361738
data = {k: 1} | {k = 2} # Ok: the value 2 will override the value 1 through the `=` operator
17371739
```
17381740

1741+
### Use the `json_merge_patch` module to merge configuration
1742+
1743+
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.
1744+
1745+
```python
1746+
_vals1 = yaml.decode(file.read("..."))
1747+
_vals2 = option("...")
1748+
1749+
_vals = _vals1 | _vals2
1750+
```
1751+
1752+
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.
1753+
17391754
## 38. How to traverse multiple elements at the same time in the for comprehension?
17401755

17411756
In KCL, we can use for comprehension to traverse multiple elements
@@ -2442,7 +2457,7 @@ data:
24422457
dataIsUnique: true
24432458
```
24442459

2445-
## 55. How do i omit attributes in the output for variables with "None" value?
2460+
## 55. How to omit attributes in the output for variables with "None" value?
24462461

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

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

+13
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,19 @@ data = {k: 1} | {k: 2} # Error: conflicting values on the attribute 'k' between
17481748
data = {k: 1} | {k = 2} # Ok: the value 2 will override the value 1 through the `=` operator
17491749
```
17501750

1751+
### 使用 `json_merge_patch` 库合并配置
1752+
1753+
如果我们对外部读取的配置有合并诉求,比如下面的代码显示的那样,则可以使用 `json_merge_patch` 库来操作,因为外部配置默认的属性运算符为 `:`, 可能会遇到合并冲突错误
1754+
1755+
```python
1756+
_vals1 = yaml.decode(file.read("..."))
1757+
_vals2 = option("...")
1758+
1759+
_vals = _vals1 | _vals2
1760+
```
1761+
1762+
`json_merge_patch` 库使用的方式详见[这里](https://github.com/kcl-lang/modules/tree/main/json_merge_patch)
1763+
17511764
## 38. KCL 中如何同时遍历多个元素
17521765

17531766
KCL 中可以使用 for 推导表达式遍历多个元素

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

+13
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,19 @@ data = {k: 1} | {k: 2} # Error: conflicting values on the attribute 'k' between
17481748
data = {k: 1} | {k = 2} # Ok: the value 2 will override the value 1 through the `=` operator
17491749
```
17501750

1751+
### 使用 `json_merge_patch` 库合并配置
1752+
1753+
如果我们对外部读取的配置有合并诉求,比如下面的代码显示的那样,则可以使用 `json_merge_patch` 库来操作,因为外部配置默认的属性运算符为 `:`, 可能会遇到合并冲突错误
1754+
1755+
```python
1756+
_vals1 = yaml.decode(file.read("..."))
1757+
_vals2 = option("...")
1758+
1759+
_vals = _vals1 | _vals2
1760+
```
1761+
1762+
`json_merge_patch` 库使用的方式详见[这里](https://github.com/kcl-lang/modules/tree/main/json_merge_patch)
1763+
17511764
## 38. KCL 中如何同时遍历多个元素
17521765

17531766
KCL 中可以使用 for 推导表达式遍历多个元素

versioned_docs/version-0.10/user_docs/support/faq-kcl.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,8 @@ configNew:
17201720
key2: value2
17211721
```
17221722

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

17251727
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
@@ -1736,6 +1738,19 @@ We can use the `=` attribute operator to modify it to the following form
17361738
data = {k: 1} | {k = 2} # Ok: the value 2 will override the value 1 through the `=` operator
17371739
```
17381740

1741+
### Use the `json_merge_patch` module to merge configuration
1742+
1743+
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.
1744+
1745+
```python
1746+
_vals1 = yaml.decode(file.read("..."))
1747+
_vals2 = option("...")
1748+
1749+
_vals = _vals1 | _vals2
1750+
```
1751+
1752+
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.
1753+
17391754
## 38. How to traverse multiple elements at the same time in the for comprehension?
17401755

17411756
In KCL, we can use for comprehension to traverse multiple elements
@@ -2442,7 +2457,7 @@ data:
24422457
dataIsUnique: true
24432458
```
24442459

2445-
## 55. How do i omit attributes in the output for variables with "None" value?
2460+
## 55. How to omit attributes in the output for variables with "None" value?
24462461

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

versioned_docs/version-0.7/user_docs/support/faq-kcl.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,7 @@ data:
24402440
dataIsUnique: true
24412441
```
24422442

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

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

versioned_docs/version-0.8/user_docs/support/faq-kcl.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,7 @@ data:
24402440
dataIsUnique: true
24412441
```
24422442

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

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

versioned_docs/version-0.9/user_docs/support/faq-kcl.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2442,7 +2442,7 @@ data:
24422442
dataIsUnique: true
24432443
```
24442444

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

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

0 commit comments

Comments
 (0)