Skip to content

Commit 7644286

Browse files
committed
chore: sync latest version to v0.10 doc and format all documents
Signed-off-by: peefy <[email protected]>
1 parent af14d01 commit 7644286

File tree

11 files changed

+164
-11
lines changed

11 files changed

+164
-11
lines changed

blog/2024-09-05-newsletter/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ assert datetime.validate("2024-08-26", "%Y-%m-%d")
9898

9999
- kcl-openapi has been optimized and adjusted for code and document structure.
100100
- kcl-openapi has added more test cases and optimized the code structure.
101-
- Package management tools have fixed the bug that compiling multiple *.k files failed.
101+
- Package management tools have fixed the bug that compiling multiple \*.k files failed.
102102
- Package management tools supported adding sub-packages in Git repositories as third-party libraries.
103103
- Package management tools have added some test cases.
104104
- krm-kcl function fixed some errors in some tests and documents.

docs/reference/lang/codelab/schema.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ mixin PersistentVolumeClaimMixin for PVCProtocol:
605605
```
606606
607607
> Note: for the `k8s.api.core.v1` import to work, we need to initialize a module and add the `k8s` module as a dependency:
608+
>
608609
> ```bash
609610
> kcl mod init
610611
> kcl mod add k8s

docs/reference/lang/tour.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2344,7 +2344,6 @@ Built-in functions and members of schema
23442344
- instances(full_pkg: bool = False)
23452345
Return the list of existing instances of a schema in the main package. When the `full_pkg` is set `True`, return all schema instances in the whole program.
23462346

2347-
23482347
```python
23492348
schema Person:
23502349
name: str

i18n/zh-CN/docusaurus-plugin-content-blog/2024-09-05-newsletter/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ assert datetime.validate("2024-08-26", "%Y-%m-%d")
100100

101101
- kcl-openapi 对代码结构和文档结构进行了优化和调整。
102102
- kcl-playground 添加了更多的测试用例,对工程结构体进行了优化和升级。
103-
- 包管理工具修复了编译多个 *.k 文件失败的 bug。
103+
- 包管理工具修复了编译多个 \*.k 文件失败的 bug。
104104
- 包管理工具支持添加 Git 仓库中子包作为三方库。
105105
- 包管理工具新增部分测试用例。
106106
- krm-kcl function 修复了部分测试和文档中的错误。

i18n/zh-CN/docusaurus-plugin-content-docs/version-0.10/reference/lang/spec/schema.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,8 @@ Suppose there are schemas a, b, and c, where c inherits b and b inherits a. Sche
816816
817817
Built-in function and members of schema
818818
819-
- instances()
820-
Return the list of existing instances of a schema.
819+
- instances(full_pkg: bool = False)
820+
Return the list of existing instances of a schema in the main package. When the `full_pkg` is set `True`, return all schema instances in the whole program.
821821
822822
### Irrelevant Order Calculation
823823

i18n/zh-CN/docusaurus-plugin-content-docs/version-0.10/reference/lang/tour.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,8 +2291,8 @@ schema Person:
22912291

22922292
内置函数和 schema 成员
22932293

2294-
- instances()
2295-
返回 schema 的现有实例列表。
2294+
- instances(full_pkg: bool = False)
2295+
返回 schema 的现有实例列表,当 `full_pkg` 设置为 `False` 时,仅返回 main 中的 schema 实例,当 `full_pkg` 设置为 `True` 时,返回整个项目对应 schema 的所有实例
22962296

22972297
```python
22982298
schema Person:

i18n/zh-CN/docusaurus-plugin-content-docs/version-0.10/reference/plugin/overview.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,79 @@ import kcl_plugin.my_plugin
173173
174174
result = my_plugin.add(1, 1)
175175
```
176+
177+
## 使用 Rust 编写插件
178+
179+
### 0. 先决条件
180+
181+
使用 KCL Rust 插件需要在您的 PATH 中具有 Rust 1.79+,并安装 KCL Rust SDK。
182+
183+
```shell
184+
cargo add anyhow
185+
cargo add kclvm-parser --git https://github.com/kcl-lang/kcl
186+
cargo add kclvm-loader --git https://github.com/kcl-lang/kcl
187+
cargo add kclvm-evaluator --git https://github.com/kcl-lang/kcl
188+
cargo add kclvm-runtime --git https://github.com/kcl-lang/kcl
189+
```
190+
191+
### 1. 你好插件
192+
193+
编写以下 Rust 代码并添加名为 `my_plugin` 的插件。
194+
195+
```rust
196+
use anyhow::{anyhow, Result};
197+
use kclvm_evaluator::Evaluator;
198+
use kclvm_loader::{load_packages, LoadPackageOptions};
199+
use kclvm_parser::LoadProgramOptions;
200+
use kclvm_runtime::{Context, IndexMap, PluginFunction, ValueRef};
201+
use std::{cell::RefCell, rc::Rc, sync::Arc};
202+
203+
fn my_plugin_sum(_: &Context, args: &ValueRef, _: &ValueRef) -> Result<ValueRef> {
204+
let a = args
205+
.arg_i_int(0, Some(0))
206+
.ok_or(anyhow!("expect int value for the first param"))?;
207+
let b = args
208+
.arg_i_int(1, Some(0))
209+
.ok_or(anyhow!("expect int value for the second param"))?;
210+
Ok((a + b).into())
211+
}
212+
213+
fn context_with_plugin() -> Rc<RefCell<Context>> {
214+
let mut plugin_functions: IndexMap<String, PluginFunction> = Default::default();
215+
let func = Arc::new(my_plugin_sum);
216+
plugin_functions.insert("my_plugin.add".to_string(), func);
217+
let mut ctx = Context::new();
218+
ctx.plugin_functions = plugin_functions;
219+
Rc::new(RefCell::new(ctx))
220+
}
221+
222+
fn main() -> Result<()> {
223+
let src = r#"
224+
import kcl_plugin.my_plugin
225+
226+
sum = my_plugin.add(1, 1)
227+
"#;
228+
let p = load_packages(&LoadPackageOptions {
229+
paths: vec!["test.k".to_string()],
230+
load_opts: Some(LoadProgramOptions {
231+
load_plugins: true,
232+
k_code_list: vec![src.to_string()],
233+
..Default::default()
234+
}),
235+
load_builtin: false,
236+
..Default::default()
237+
})?;
238+
let evaluator = Evaluator::new_with_runtime_ctx(&p.program, context_with_plugin());
239+
let result = evaluator.run()?;
240+
println!("yaml result {}", result.1);
241+
Ok(())
242+
}
243+
```
244+
245+
test.k 的内容为:
246+
247+
```python
248+
import kcl_plugin.my_plugin
249+
250+
result = my_plugin.add(1, 1)
251+
```

versioned_docs/version-0.10/reference/lang/codelab/schema.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ mixin PersistentVolumeClaimMixin for PVCProtocol:
605605
```
606606
607607
> Note: for the `k8s.api.core.v1` import to work, we need to initialize a module and add the `k8s` module as a dependency:
608+
>
608609
> ```bash
609610
> kcl mod init
610611
> kcl mod add k8s

versioned_docs/version-0.10/reference/lang/spec/schema.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,8 @@ Suppose there are schemas a, b, and c, where c inherits b and b inherits a. Sche
816816
817817
Built-in function and members of schema
818818
819-
- instances()
820-
Return the list of existing instances of a schema.
819+
- instances(full_pkg: bool = False)
820+
Return the list of existing instances of a schema in the main package. When the `full_pkg` is set `True`, return all schema instances in the whole program.
821821
822822
### Irrelevant Order Calculation
823823

versioned_docs/version-0.10/reference/lang/tour.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,8 +2341,8 @@ Note that the current version of KCL does not yet support user-defined decorator
23412341

23422342
Built-in functions and members of schema
23432343

2344-
- instances()
2345-
Return the list of existing instances of a schema.
2344+
- instances(full_pkg: bool = False)
2345+
Return the list of existing instances of a schema in the main package. When the `full_pkg` is set `True`, return all schema instances in the whole program.
23462346

23472347
```python
23482348
schema Person:

versioned_docs/version-0.10/reference/plugin/overview.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,79 @@ import kcl_plugin.my_plugin
174174
175175
result = my_plugin.add(1, 1)
176176
```
177+
178+
## Use Rust to Write Plugins
179+
180+
### 0. Prerequisites
181+
182+
Using the KCL Rust plugin requires the presence of `Rust 1.79+` in your `PATH` and install the KCL Rust SDK.
183+
184+
```shell
185+
cargo add anyhow
186+
cargo add kclvm-parser --git https://github.com/kcl-lang/kcl
187+
cargo add kclvm-loader --git https://github.com/kcl-lang/kcl
188+
cargo add kclvm-evaluator --git https://github.com/kcl-lang/kcl
189+
cargo add kclvm-runtime --git https://github.com/kcl-lang/kcl
190+
```
191+
192+
### 1. Hello Plugin
193+
194+
Write the following Rust code and add the the plugin named `my_plugin`.
195+
196+
```rust
197+
use anyhow::{anyhow, Result};
198+
use kclvm_evaluator::Evaluator;
199+
use kclvm_loader::{load_packages, LoadPackageOptions};
200+
use kclvm_parser::LoadProgramOptions;
201+
use kclvm_runtime::{Context, IndexMap, PluginFunction, ValueRef};
202+
use std::{cell::RefCell, rc::Rc, sync::Arc};
203+
204+
fn my_plugin_sum(_: &Context, args: &ValueRef, _: &ValueRef) -> Result<ValueRef> {
205+
let a = args
206+
.arg_i_int(0, Some(0))
207+
.ok_or(anyhow!("expect int value for the first param"))?;
208+
let b = args
209+
.arg_i_int(1, Some(0))
210+
.ok_or(anyhow!("expect int value for the second param"))?;
211+
Ok((a + b).into())
212+
}
213+
214+
fn context_with_plugin() -> Rc<RefCell<Context>> {
215+
let mut plugin_functions: IndexMap<String, PluginFunction> = Default::default();
216+
let func = Arc::new(my_plugin_sum);
217+
plugin_functions.insert("my_plugin.add".to_string(), func);
218+
let mut ctx = Context::new();
219+
ctx.plugin_functions = plugin_functions;
220+
Rc::new(RefCell::new(ctx))
221+
}
222+
223+
fn main() -> Result<()> {
224+
let src = r#"
225+
import kcl_plugin.my_plugin
226+
227+
sum = my_plugin.add(1, 1)
228+
"#;
229+
let p = load_packages(&LoadPackageOptions {
230+
paths: vec!["test.k".to_string()],
231+
load_opts: Some(LoadProgramOptions {
232+
load_plugins: true,
233+
k_code_list: vec![src.to_string()],
234+
..Default::default()
235+
}),
236+
load_builtin: false,
237+
..Default::default()
238+
})?;
239+
let evaluator = Evaluator::new_with_runtime_ctx(&p.program, context_with_plugin());
240+
let result = evaluator.run()?;
241+
println!("yaml result {}", result.1);
242+
Ok(())
243+
}
244+
```
245+
246+
The content of `test.k` are:
247+
248+
```python
249+
import kcl_plugin.my_plugin
250+
251+
result = my_plugin.add(1, 1)
252+
```

0 commit comments

Comments
 (0)