Skip to content

Commit 802ba22

Browse files
authored
Merge pull request #347 from Peefy/refactor-multiple-language-docs
docs: refactor multiple language documents
2 parents 9763816 + bf0e08a commit 802ba22

File tree

14 files changed

+223
-874
lines changed

14 files changed

+223
-874
lines changed

docs/reference/xlang-api/java-api.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,50 @@ sidebar_position: 5
44

55
# Java API
66

7-
At present, the KCL Java SDK is still in the early preview version. The KCL team will continue to update and provide more functions in the future. For more information, see [https://github.com/kcl-lang/kcl-java](https://github.com/kcl-lang/kcl-java)
7+
## Installation
8+
9+
Refer to [this](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#authenticating-to-github-packages
10+
) to configure your Maven; set up your GitHub account and Token in the `settings.xml`.
11+
12+
### Maven
13+
14+
In your project's pom.xml, configure our repository as follows:
15+
16+
```xml
17+
<repositories>
18+
<repository>
19+
<id>github</id>
20+
<url>https://maven.pkg.github.com/kcl-lang/*</url>
21+
<snapshots>
22+
<enabled>true</enabled>
23+
</snapshots>
24+
</repository>
25+
</repositories>
26+
```
27+
28+
This way you'll be able to import the above dependency to use the SDK.
29+
30+
```xml
31+
<dependency>
32+
<groupId>com.kcl</groupId>
33+
<artifactId>kcl-lib</artifactId>
34+
<version>0.8.5</version>
35+
</dependency>
36+
```
37+
38+
## Quick Start
39+
40+
```java
41+
import com.kcl.api.API;
42+
import com.kcl.api.Spec.ExecProgram_Args;
43+
import com.kcl.api.Spec.ExecProgram_Result;
44+
45+
public class ExecProgramTest {
46+
public static void main(String[] args) throws Exception {
47+
API api = new API();
48+
ExecProgram_Result result = api
49+
.execProgram(ExecProgram_Args.newBuilder().addKFilenameList("path/to/kcl.k").build());
50+
System.out.println(result.getYamlResult());
51+
}
52+
}
53+
```

docs/reference/xlang-api/overview.md

Lines changed: 3 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -4,179 +4,11 @@ sidebar_position: 1
44

55
# Introduction
66

7-
The KCL language provides general programming language interfaces such as C/Rust/Go/Python/Java, and the related languages are under development.
7+
The KCL language provides general programming language APIs.
88

9-
## C/Rust API
9+
## C/Rust APIs
1010

11-
The core of KCL is developed in Rust, and the C language API is exported externally for packaging and integration in high-level languages such as Go/Python/Java.
12-
13-
## Go API
14-
15-
Go API is a C-API provided by CGO wrapping KCL, while providing deeper customization features to meet the needs of upper-level tools.
16-
17-
### Abstract Model
18-
19-
The abstract model of the KCL Go API is as follows:
20-
21-
```
22-
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
23-
│ kcl files │ │ KCL-Go-API │ │ KCLResultList │
24-
│ ┌───────────┐ │ │ │ │ │
25-
│ │ 1.k │ │ │ │ │ │
26-
│ └───────────┘ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
27-
│ ┌───────────┐ │ │ ┌───────────┐ │ │ │ KCLResult │──┼────────▶│x.Get("a.b.c") │
28-
│ │ 2.k │ │ │ │ Run(path) │ │ │ └───────────┘ │ └───────────────┘
29-
│ └───────────┘ │────┐ │ └───────────┘ │ │ │
30-
│ ┌───────────┐ │ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
31-
│ │ 3.k │ │ │ │ │ │ │ KCLResult │──┼────────▶│x.Get("k", &v) │
32-
│ └───────────┘ │ │ │ │ │ └───────────┘ │ └───────────────┘
33-
│ ┌───────────┐ │ ├───▶│ ┌───────────┐ │──────────▶│ │
34-
│ │setting.yml│ │ │ │ │RunFiles() │ │ │ ┌───────────┐ │ ┌───────────────┐
35-
│ └───────────┘ │ │ │ └───────────┘ │ │ │ KCLResult │──┼────────▶│x.JSONString() │
36-
└─────────────────┘ │ │ │ │ └───────────┘ │ └───────────────┘
37-
│ │ │ │ │
38-
┌─────────────────┐ │ │ │ │ ┌───────────┐ │ ┌───────────────┐
39-
│ Options │ │ │ ┌───────────┐ │ │ │ KCLResult │──┼────────▶│x.YAMLString() │
40-
│WithOptions │ │ │ │MustRun() │ │ │ └───────────┘ │ └───────────────┘
41-
│WithOverrides │────┘ │ └───────────┘ │ │ │
42-
│WithWorkDir │ │ │ │ │
43-
│WithDisableNone │ │ │ │ │
44-
└─────────────────┘ └─────────────────┘ └─────────────────┘
45-
```
46-
47-
The input file contains the KCL file and the `setting.yml` configuration file, and `Options` can be used to specify additional parameters and information such as working directory. The "KCL-Go-API" part is the provided KCL execution function. The execution function executes the KCL program according to the input file and additional parameters, and finally outputs the result of `KCLResultList`. `KCLResultList` is a list of `KCLResult`, each `KCLResult` corresponding to a generated configuration file or `map[string]interface{}`.
48-
49-
### Example
50-
51-
```go
52-
package main
53-
54-
import (
55-
"fmt"
56-
57-
kcl "kcl-lang.io/kcl-go"
58-
)
59-
60-
61-
func main() {
62-
const k_code = `
63-
name = "kcl"
64-
age = 1
65-
66-
schema Person:
67-
name: str = "kcl"
68-
age: int = 1
69-
70-
x0 = Person{}
71-
x1 = Person{age:101}
72-
`
73-
74-
result := kcl.MustRun("hello.k", kcl.WithCode(k_code)).First()
75-
fmt.Println(result.GetRawYAMLString())
76-
77-
fmt.Println("----")
78-
fmt.Println("x0.name:", result.Get("x0.name"))
79-
fmt.Println("x1.age:", result.Get("x1.age"))
80-
81-
fmt.Println("----")
82-
83-
var person struct {
84-
Name string
85-
Age int
86-
}
87-
fmt.Printf("person: %+v\n", result.Get("x1", &person))
88-
}
89-
```
90-
91-
Output result:
92-
93-
```yaml
94-
age: 1
95-
name: kcl
96-
x0:
97-
age: 1
98-
name: kcl
99-
x1:
100-
age: 101
101-
name: kcl
102-
103-
----
104-
x0.name: kcl
105-
x1.age: 101
106-
----
107-
person: &{Name:kcl Age:101}
108-
```
109-
110-
## Python API
111-
112-
Using the Python SDK requires that you have a local Python version higher than 3.7.3 and a local pip package management tool. You can use the following command to install and obtain helpful information.
113-
114-
```bash
115-
python3 -m pip install kclvm --user && python3 -m kclvm --help
116-
```
117-
118-
### Command Line Tool
119-
120-
Prepare a KCL file named `main.k`
121-
122-
```python
123-
name = "kcl"
124-
age = 1
125-
126-
schema Person:
127-
name: str = "kcl"
128-
age: int = 1
129-
130-
x0 = Person {}
131-
x1 = Person {
132-
age = 101
133-
}
134-
```
135-
136-
Execute the following command and get the output:
137-
138-
```shell
139-
$ python3 -m kclvm hello.k
140-
name: kcl
141-
age: 1
142-
x0:
143-
name: kcl
144-
age: 1
145-
x1:
146-
name: kcl
147-
age: 101
148-
```
149-
150-
### API
151-
152-
In addition, we can also execute KCL files through Python code.
153-
154-
Prepare a KCL file named `main.py`
155-
156-
```python
157-
import kclvm.program.exec as kclvm_exec
158-
import kclvm.vm.planner as planner
159-
160-
print(planner.plan(kclvm_exec.Run(["hello.k"]).filter_by_path_selector()))
161-
```
162-
163-
Execute the following command and get the output:
164-
165-
```shell
166-
$ python3 main.py
167-
name: kcl
168-
age: 1
169-
x0:
170-
name: kcl
171-
age: 1
172-
x1:
173-
name: kcl
174-
age: 101
175-
```
176-
177-
You can see that the same output can be obtained through command line tools and APIs.
178-
179-
At present, the KCL Python SDK is still in the early preview version. The KCL team will continue to update and provide more functions in the future. For more information, see [https://github.com/kcl-lang/kcl-py](https://github.com/kcl-lang/kcl-py)
11+
The core of KCL is developed in Rust, and the C language API is exported externally for packaging and integration in other high-level languages such as Go, Python, etc.
18012

18113
## REST-API
18214

docs/reference/xlang-api/python-api.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,19 @@ sidebar_position: 4
44

55
# Python API
66

7-
At present, the KCL Python SDK is still in the early preview version. The KCL team will continue to update and provide more functions in the future. For more information, see [https://github.com/kcl-lang/kcl-py](https://github.com/kcl-lang/kcl-py)
7+
## Installation
8+
9+
```shell
10+
python3 -m pip install kcl-lib
11+
```
12+
13+
## Quick Start
14+
15+
```typescript
16+
import kcl_lib.api as api
17+
18+
args = api.ExecProgram_Args(k_filename_list=["path/to/kcl.k"])
19+
api = api.API()
20+
result = api.exec_program(args)
21+
print(result.yaml_result)
22+
```

docs/reference/xlang-api/rest-api.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ service KclvmService {
179179
rpc ParseProgram(ParseProgram_Args) returns(ParseProgram_Result);
180180
rpc LoadPackage(LoadPackage_Args) returns(LoadPackage_Result);
181181
rpc ListOptions(ParseProgram_Args) returns(ListOptions_Result);
182+
rpc ListVariables(ListVariables_Args) returns(ListVariables_Result);
182183
183184
rpc FormatCode(FormatCode_Args) returns(FormatCode_Result);
184185
rpc FormatPath(FormatPath_Args) returns(FormatPath_Result);
@@ -303,7 +304,7 @@ message ExecProgram_Args {
303304
304305
repeated string k_filename_list = 2;
305306
repeated string k_code_list = 3;
306-
307+
307308
repeated CmdArgSpec args = 4;
308309
repeated CmdOverrideSpec overrides = 5;
309310
@@ -339,6 +340,9 @@ message ExecProgram_Args {
339340
340341
// -S --path_selector
341342
repeated string path_selector = 17;
343+
344+
// -K --fast_eval
345+
bool fast_eval = 18;
342346
}
343347
344348
message ExecProgram_Result {
@@ -403,6 +407,20 @@ message OverrideFile_Result {
403407
bool result = 1;
404408
}
405409
410+
message ListVariables_Args {
411+
string file = 1;
412+
repeated string specs = 2;
413+
}
414+
415+
message ListVariables_Result {
416+
map<string, Variable> variables = 1;
417+
repeated string unsupported_codes = 2;
418+
}
419+
420+
message Variable {
421+
string value = 1;
422+
}
423+
406424
message GetFullSchemaType_Args {
407425
ExecProgram_Args exec_args = 1;
408426
string schema_name = 2;
@@ -487,6 +505,7 @@ message CliConfig {
487505
bool sort_keys = 9;
488506
bool show_hidden = 10;
489507
bool include_schema_type_path = 11;
508+
bool fast_eval = 12;
490509
}
491510
492511
message KeyValuePair {

i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/xlang-api/java-api.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,23 @@ sidebar_position: 5
44

55
# Java API
66

7-
目前 KCL Java SDK 还处于早期预览版本,后续 KCL 团队会持续更新并提供更丰富的功能,更多信息请参阅:[https://github.com/kcl-lang/kcl-java](https://github.com/kcl-lang/kcl-java)
7+
## 添加依赖
8+
9+
```shell
10+
npm install kcl-lib
11+
```
12+
13+
### Maven
14+
15+
## 快速开始
16+
17+
```typescript
18+
import { execProgram, ExecProgramArgs } from "kcl-lib";
19+
20+
function main() {
21+
const result = execProgram(new ExecProgramArgs(["path/to/kcl.k"]));
22+
console.log(result.yamlResult);
23+
}
24+
25+
main();
26+
```

0 commit comments

Comments
 (0)