zong-zhe / lib

embed kclvm artifact for go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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;

fn main() -> 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 kclvm-runtime as an example.
cargo add --git https://github.com/kcl-lang/kcl kclvm-runtime

Go

go get kcl-lang.io/lib

Write the Code

package main

import (
	"kcl-lang.io/lib"
)

func main() {
    path = "path/to/install/lib"
    _ := lib.InstallKclvm(path)
}

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.9.1-SNAPSHOT</version>
</dependency>

Write the code

import com.kcl.api.API;
import com.kcl.api.Spec.ExecProgram_Args;
import com.kcl.api.Spec.ExecProgram_Result;

public class ExecProgramTest {
    public static void main(String[] args) throws Exception {
        API api = new API();
        ExecProgram_Result result = api
                .execProgram(ExecProgram_Args.newBuilder().addKFilenameList("path/to/kcl.k").build());
        System.out.println(result.getYamlResult());
    }
}

.NET

This library is currently under development here

namespace KclLib.Tests;

using KclLib.API;

public class KclLibAPITest
{
    public static void Main()
    {
        var execArgs = new ExecProgram_Args();
        execArgs.KFilenameList.Add("path/to/kcl.k");
        var result = new API().ExecProgram(execArgs);
        Console.WriteLine(result.YamlResult);
    }
}

Python

python3 -m pip install kcl-lib

Write the code

import kcl_lib.api as api

args = api.ExecProgram_Args(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'

function main() {
  const result = execProgram(new ExecProgramArgs(['__test__/test_data/schema.k']))
  console.log(result.yamlResult)
}

main();

Documents

See here

License

FOSSA Status

About

embed kclvm artifact for go


Languages

Language:C# 77.3%Language:Java 14.3%Language:Rust 3.8%Language:Python 1.9%Language:JavaScript 1.3%Language:Go 0.7%Language:TypeScript 0.6%Language:Makefile 0.1%Language:Dockerfile 0.1%Language:AMPL 0.0%