Skip to content

Repository files navigation

KCL Multiple Language Bindings and SDKs

This repo mainly includes the binding of the low-level API and spec of the KCL language core, and the SDKs of various languages are based on this to encapsulate higher-level APIs.

Bindings

Rust

cargo add --git https://github.com/kcl-lang/lib

Write the Code

use kcl_lang::*;use anyhow::Result;fnmain() -> Result<()>{let api = API::default();let args = &ExecProgramArgs{k_filename_list:vec!["main.k".to_string()],k_code_list:vec!["a = 1".to_string()],
..Default::default()};let exec_result = api.exec_program(args)?;println!("{}", exec_result.yaml_result);Ok(())}

More Rust APIs can be found here. If you want to use the sub crate of KCL Rust core, you can run the following command.

# Take the kcl-runtime as an example.
cargo add --git https://github.com/kcl-lang/kcl kcl-runtime

Go

go get kcl-lang.io/lib

Write the Code

package main
import (
"fmt""kcl-lang.io/lib/go/api""kcl-lang.io/lib/go/native"
)
funcmain() {
client:=native.NewNativeServiceClient()
result, err:=client.ExecProgram(&api.ExecProgramArgs{
KFilenameList: []string{"main.k"},
KCodeList: []string{"a = 1"},
})
iferr!=nil {
t.Fatal(err)
}
fmt.Println(result.YamlResult)
}

Full Go SDK can be found here, which depends on the kcl-lang/lib Go bindings.

Java

Refer to this 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:

<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.

<dependency>
<groupId>com.kcl</groupId>
<artifactId>kcl-lib</artifactId>
<version>0.12.4</version>
</dependency>

Write the code

importcom.kcl.api.API;
importcom.kcl.api.Spec.ExecProgramArgs;
importcom.kcl.api.Spec.ExecProgramResult;
publicclassExecProgramTest {
publicstaticvoidmain(String[] args) throwsException {
APIapi = newAPI();
ExecProgramResultresult = api
.execProgram(ExecProgramArgs.newBuilder().addKFilenameList("path/to/kcl.k").build());
System.out.println(result.getYamlResult());
}
}

.NET

dotnet add package KclLib

Write the code

usingKclLib.API;varapi=newAPI();varexecArgs=newExecProgramArgs();varpath=Path.Combine("test_data","schema.k");execArgs.KFilenameList.Add(path);varresult=api.ExecProgram(execArgs);Console.WriteLine(result.YamlResult);

Python

python3 -m pip install kcl-lib

Write the code

importkcl_lib.apiasapiargs=api.ExecProgramArgs(k_filename_list=["./tests/test_data/schema.k"])
api=api.API()
result=api.exec_program(args)
print(result.yaml_result)

Node.js

npm install kcl-lib

Write the code

import{execProgram,ExecProgramArgs}from'kcl-lib'functionmain(){constresult=execProgram(newExecProgramArgs(['__test__/test_data/schema.k']))console.log(result.yamlResult)}main();

Kotlin

Refer to this 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:

<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.

<dependency>
<groupId>com.kcl</groupId>
<artifactId>kcl-lib</artifactId>
<version>0.12.4</version>
</dependency>

Write the code

importcom.kcl.api.APIimportcom.kcl.api.execProgramArgsval args = execProgramArgs { kFilenameList +="schema.k" }
val api =API()
val result = api.execProgram(args)

Swift

import KclLib
letapi=API()varexecArgs=ExecProgramArgs()
execArgs.kFilenameList.append("schema.k")letresult=try api.execProgram(execArgs)

C++

For CMake, you can use FetchContent to add KCL C++ Lib to your project.

FetchContent_Declare(
kcl-lib
GIT_REPOSITORY https://github.com/kcl-lang/lib.git
GIT_TAG v0.12.4
SOURCE_SUBDIR cpp
)
FetchContent_MakeAvailable(kcl-lib)

Or you can download the source code and add it to your project.

mkdir third_party
cd third_party
git clone https://github.com/kcl-lang/lib.git
add_subdirectory(third_party/lib/cpp)
target_link_libraries(your_target kcl-lib-cpp)

Write the code

#include"kcl_lib.hpp"
#include<iostream>intmain()
{
auto args = kcl_lib::ExecProgramArgs {
.k_filename_list = { "../test_data/schema.k" },
};
auto result = kcl_lib::exec_program(args);
std::cout << result.yaml_result.c_str() << std::endl;
return0;
}

C

See here

WASM

See here

Documents

See here

License

FOSSA Status

About

KCL Multiple Language Bindings including Rust, Go, Python, Java, Kotlin, .NET, Swift, Lua, Node.js, Zig, C, C++, WASM, etc.

Resources

Contributing

Stars

21 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages