Skip to content

docs: update 0.10.0-alpha API documents #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions docs/reference/xlang-api/kotlin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,35 @@ sidebar_position: 8

# Kotlin API

The official [Kotlin KCL package](https://github.com/kcl-lang/lib/tree/main/kotlin) has not been released yet. Issues and PRs are welcome!
## Installation

Of course, you can use the KCL Java package to call the KCL API in Kotlin.
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) to configure your Maven; set up your GitHub account and Token in the `settings.xml`.

### Maven

In your project's pom.xml, configure our repository as follows:

```xml
<repositories>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/kcl-lang/*</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
```

This way you'll be able to import the above dependency to use the SDK.

```xml
<dependency>
<groupId>com.kcl</groupId>
<artifactId>kcl-lib-kotlin</artifactId>
<version>0.10.0-alpha.2-SNAPSHOT</version>
</dependency>
```

## Quick Start

Expand All @@ -18,3 +44,58 @@ val args = execProgramArgs { kFilenameList += "schema.k" }
val api = API()
val result = api.execProgram(args)
```

## API Reference

### execProgram

Execute KCL file with arguments and return the JSON/YAML result.

<details><summary>Example</summary>
<p>

The content of `schema.k` is

```python
schema AppConfig:
replicas: int

app: AppConfig {
replicas: 2
}
```

Kotlin Code

```kotlin
import com.kcl.api.API
import com.kcl.api.execProgramArgs

val args = execProgramArgs { kFilenameList += "schema.k" }
val api = API()
val result = api.execProgram(args)
```

</p>
</details>

### validateCode

Validate code using schema and JSON/YAML data strings.

<details><summary>Example</summary>
<p>

Kotlin Code

```kotlin
val args = validateCodeArgs {
code = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n" + " 0 < age < 120\n"
data = "{\"name\": \"Alice\", \"age\": 10}"
}
val apiInstance = API();
val result = apiInstance.validateCode(args);
```

</p>
</details>
2 changes: 1 addition & 1 deletion docs/reference/xlang-api/python-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ assert list(result.symbols.values())[0].ty.schema_name == "AppConfig"
</p>
</details>

### list_variable
### list_variables

list_variables provides users with the ability to parse KCL program and get all variables by specs.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,35 @@ sidebar_position: 8

# Kotlin API

[Kotlin KCL API](https://github.com/kcl-lang/lib/tree/main/kotlin) 正在开发当中,尚未正式发布。欢迎提交 Issues 或者 PRs 参与共建!
## 添加依赖

当然,您也可以在 Kotlin 中直接调用 KCL Java API。
参考[此处](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#authenticating-to-github-packages)来配置您的 Maven;在 settings.xml 中设置您的 GitHub 账户和 Token。

### Maven

在您项目的 pom.xml 中,按如下配置 Maven 仓库:

```xml
<repositories>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/kcl-lang/*</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
```

通过这种方式,您将能够导入上述依赖以使用 Kotlin SDK。

```xml
<dependency>
<groupId>com.kcl</groupId>
<artifactId>kcl-lib-kotlin</artifactId>
<version>0.10.0-alpha.2-SNAPSHOT</version>
</dependency>
```

## 快速开始

Expand All @@ -18,3 +44,58 @@ val args = execProgramArgs { kFilenameList += "schema.k" }
val api = API()
val result = api.execProgram(args)
```

## API Reference

### execProgram

Execute KCL file with arguments and return the JSON/YAML result.

<details><summary>Example</summary>
<p>

The content of `schema.k` is

```python
schema AppConfig:
replicas: int

app: AppConfig {
replicas: 2
}
```

Kotlin Code

```kotlin
import com.kcl.api.API
import com.kcl.api.execProgramArgs

val args = execProgramArgs { kFilenameList += "schema.k" }
val api = API()
val result = api.execProgram(args)
```

</p>
</details>

### validateCode

Validate code using schema and JSON/YAML data strings.

<details><summary>Example</summary>
<p>

Kotlin Code

```kotlin
val args = validateCodeArgs {
code = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n" + " 0 < age < 120\n"
data = "{\"name\": \"Alice\", \"age\": 10}"
}
val apiInstance = API();
val result = apiInstance.validateCode(args);
```

</p>
</details>
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ assert list(result.symbols.values())[0].ty.schema_name == "AppConfig"
</p>
</details>

### list_variable
### list_variables

list_variables provides users with the ability to parse KCL program and get all variables by specs.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ assert list(result.symbols.values())[0].ty.schema_name == "AppConfig"
</p>
</details>

### list_variable
### list_variables

list_variables provides users with the ability to parse KCL program and get all variables by specs.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ assert list(result.symbols.values())[0].ty.schema_name == "AppConfig"
</p>
</details>

### list_variable
### list_variables

list_variables provides users with the ability to parse KCL program and get all variables by specs.

Expand Down
Loading