diff --git a/.github/workflows/c-test.yaml b/.github/workflows/c-test.yaml new file mode 100644 index 0000000..7d8e039 --- /dev/null +++ b/.github/workflows/c-test.yaml @@ -0,0 +1,56 @@ +name: c-test + +on: + push: + branches: + - main + - master + tags: + - '*' + pull_request: + branches: + - main + paths: + - "c/**" + - ".github/workflows/c-test.yaml" + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + build-and-test: + defaults: + run: + working-directory: "c" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.79 + override: true + components: clippy, rustfmt + - name: Build + run: | + make + make examples + - name: Run Examples + shell: bash + run: | + cd examples + for file in ./* + do + if [[ -f "$file" && -x "$file" ]]; then + echo "Executing: $file" + if ! "$file"; then + echo "Error occurred while executing $file. Exiting script." + exit 1 + fi + fi + done diff --git a/.github/workflows/cpp-test.yaml b/.github/workflows/cpp-test.yaml new file mode 100644 index 0000000..f85fbe7 --- /dev/null +++ b/.github/workflows/cpp-test.yaml @@ -0,0 +1,62 @@ +name: cpp-test + +on: + push: + branches: + - main + - master + tags: + - '*' + pull_request: + branches: + - main + paths: + - "cpp/**" + - ".github/workflows/cpp-test.yaml" + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + build-and-test: + defaults: + run: + working-directory: "cpp" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install libgtest-dev ninja-build + - name: Install Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: 1.79 + override: true + components: clippy, rustfmt + - name: Build + run: | + mkdir build + cd build + cmake .. + make -j8 + - name: Run Examples + shell: bash + run: | + cd build + for file in ./* + do + if [[ -f "$file" && -x "$file" ]]; then + echo "Executing: $file" + if ! "$file"; then + echo "Error occurred while executing $file. Exiting script." + exit 1 + fi + fi + done diff --git a/.github/workflows/dotnet-test.yaml b/.github/workflows/dotnet-test.yaml index 3f0a052..0f37701 100644 --- a/.github/workflows/dotnet-test.yaml +++ b/.github/workflows/dotnet-test.yaml @@ -12,6 +12,7 @@ on: - main paths: - "dotnet/**" + - ".github/workflows/dotnet-test.yaml" workflow_dispatch: jobs: @@ -68,7 +69,7 @@ jobs: - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: 1.77 + toolchain: 1.79 override: true components: clippy, rustfmt - name: Setup linux-aarch_64 rust target @@ -123,7 +124,7 @@ jobs: - name: Install Rust uses: actions-rs/toolchain@v1 with: - toolchain: 1.77 + toolchain: 1.79 override: true components: clippy, rustfmt diff --git a/.github/workflows/go-test.yaml b/.github/workflows/go-test.yaml index 5d3995a..46e4160 100644 --- a/.github/workflows/go-test.yaml +++ b/.github/workflows/go-test.yaml @@ -14,6 +14,7 @@ on: - "go/**" - "go.mod" - "go.sum" + - ".github/workflows/go-test.yaml" workflow_dispatch: permissions: diff --git a/.github/workflows/java-test.yaml b/.github/workflows/java-test.yaml index 1f4b39e..ad9fd82 100644 --- a/.github/workflows/java-test.yaml +++ b/.github/workflows/java-test.yaml @@ -12,6 +12,7 @@ on: - main paths: - "java/**" + - ".github/workflows/java-test.yaml" workflow_dispatch: jobs: @@ -59,7 +60,7 @@ jobs: - name: Install rust nightly toolchain uses: actions-rs/toolchain@v1 with: - toolchain: 1.77 + toolchain: 1.79 override: true components: clippy, rustfmt diff --git a/.github/workflows/nodejs-test.yaml b/.github/workflows/nodejs-test.yaml index 20f380d..945d57b 100644 --- a/.github/workflows/nodejs-test.yaml +++ b/.github/workflows/nodejs-test.yaml @@ -15,6 +15,7 @@ on: - main paths: - "nodejs/**" + - ".github/workflows/nodejs-test.yaml" workflow_dispatch: jobs: diff --git a/.github/workflows/python-test.yaml b/.github/workflows/python-test.yaml index 2f3ba22..92d047a 100644 --- a/.github/workflows/python-test.yaml +++ b/.github/workflows/python-test.yaml @@ -17,6 +17,7 @@ on: - main paths: - "python/**" + - ".github/workflows/python-test.yaml" workflow_dispatch: permissions: diff --git a/.github/workflows/rust-test.yaml b/.github/workflows/rust-test.yaml index 49e3956..5f89d15 100644 --- a/.github/workflows/rust-test.yaml +++ b/.github/workflows/rust-test.yaml @@ -13,6 +13,7 @@ on: paths: - "Cargo.toml" - "src/**" + - ".github/workflows/rust-test.yaml" workflow_dispatch: permissions: @@ -30,7 +31,7 @@ jobs: - name: Install rust nightly toolchain uses: actions-rs/toolchain@v1 with: - toolchain: 1.77 + toolchain: 1.79 override: true components: clippy, rustfmt - name: Rust code test diff --git a/.github/workflows/wasm-test.yaml b/.github/workflows/wasm-test.yaml index 75a9290..3ce091b 100644 --- a/.github/workflows/wasm-test.yaml +++ b/.github/workflows/wasm-test.yaml @@ -11,6 +11,7 @@ on: - main paths: - "wasm/**" + - ".github/workflows/wasm-test.yaml" workflow_dispatch: jobs: diff --git a/README.md b/README.md index 781b4e1..f49bd84 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,61 @@ function main() { main(); ``` +### C++ + +For CMake, you can use FetchContent to add KCL C++ Lib to your project. + +```shell +FetchContent_Declare( + kcl-lib + GIT_REPOSITORY https://github.com/kcl-lang/lib.git + GIT_TAG v0.9.3 + SOURCE_SUBDIR cpp +) +FetchContent_MakeAvailable(kcl-lib) +``` + +Or you can download the source code and add it to your project. + +```shell +mkdir third_party +cd third_party +git clone https://github.com/kcl-lang/lib.git +``` + +```shell +add_subdirectory(third_party/lib/cpp) +``` + +```shell +target_link_libraries(your_target kcl-lib-cpp) +``` + +Write the code + +```cpp +#include "kcl_lib.hpp" +#include + +int main() +{ + auto args = kcl_lib::ExecProgramArgs(); + auto files = rust::Vec(); + files.push_back(rust::String("../test_data/schema.k")); + args.k_filename_list = files; + auto result = kcl_lib::exec_program(args); + std::cout << result.yaml_result.c_str() << std::endl; +} +``` + +### C + +See [here](./c/) + +### WASM + +See [here](./wasm/) + ## Documents See [here](https://www.kcl-lang.io/docs/reference/xlang-api/overview) diff --git a/c/Makefile b/c/Makefile index 2f9a3b9..71e9fc9 100644 --- a/c/Makefile +++ b/c/Makefile @@ -22,7 +22,7 @@ STATIC_LIB = $(LIB_DIR)/libkcl_lib_c.a CCFLAGS=-I./include CXXFLAGS=-I./include -std=c++14 -all: cargo-build $(STATIC_LIB) +all: cargo $(STATIC_LIB) debug: CFLAGS += -g debug: all diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index b6d5bc4..a4ef868 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -64,25 +64,3 @@ if(WIN32) RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR} ) endif() - -# Tests -if (KCL_LIB_ENABLE_TESTING) - enable_testing() - find_package(GTest REQUIRED) - file(GLOB_RECURSE TEST_SOURCE_FILE tests/*.cpp) - add_executable(kcl-lib-cpp_test ${TEST_SOURCE_FILE}) - target_include_directories(kcl-lib-cpp_test PUBLIC ${CPP_INCLUDE_DIR} ${GTEST_INCLUDE_DIRS}) - target_link_libraries(kcl-lib-cpp_test ${GTEST_LDFLAGS} GTest::gtest_main kcl-lib-cpp) - target_compile_options(kcl-lib-cpp_test PRIVATE ${GTEST_CFLAGS}) - - # Platform-specific test configuration - if(WIN32) - target_link_libraries(kcl-lib-cpp_test userenv ws2_32 bcrypt) - endif() - if(APPLE) - target_link_libraries(kcl-lib-cpp_test "-framework CoreFoundation -framework Security") - endif() - - include(GoogleTest) - gtest_discover_tests(kcl-lib-cpp_test) -endif() diff --git a/cpp/Makefile b/cpp/Makefile index 56197c7..1042b30 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -7,3 +7,7 @@ fmt: .PHONY: cargo cargo: cargo build -r + +.PHONY: cpp +cpp: + cmake --build build --parallel diff --git a/cpp/README.md b/cpp/README.md index 83b7ed5..48bf4b6 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -42,12 +42,20 @@ target_link_libraries(your_target kcl-lib-cpp) + C++ Compiler with C++17 Support + Cargo +If you build in macos, you set the environment. + ```shell +# Set cargo build target on macos export MACOSX_DEPLOYMENT_TARGET='10.13' -mkdir build +``` + +Use cmake to build the whole project. + +```shell +mkdir -p build cd build cmake .. -make +make -j8 ``` ## Examples diff --git a/spec/gpyrpc/Makefile b/spec/gpyrpc/Makefile deleted file mode 100644 index 59b1512..0000000 --- a/spec/gpyrpc/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -run: - protoc spec.proto --java_out ../../java/src/main/java - protoc spec.proto --kotlin_out ../../kotlin/src/main/kotlin - protoc spec.proto --pyi_out ../../python/kcl_lib/api - protoc spec.proto --python_out ../../python/kcl_lib/api - protoc spec.proto --go_out ../../go/api - protoc spec.proto --csharp_out ../../dotnet/KclLib/api - protoc spec.proto --cpp_out ../../cpp/src/api diff --git a/spec/gpyrpc/spec.proto b/spec/gpyrpc/spec.proto deleted file mode 100644 index ec56e97..0000000 --- a/spec/gpyrpc/spec.proto +++ /dev/null @@ -1,1469 +0,0 @@ -// Copyright The KCL Authors. All rights reserved. -// -// This file defines the request parameters and return structure of the KCL RPC server. - -syntax = "proto3"; - -package com.kcl.api; - -option csharp_namespace = "KclLib.API"; -option go_package = ".;api"; - -// Message representing an external package for KCL. -// kcl main.k -E pkg_name=pkg_path -message ExternalPkg { - // Name of the package. - string pkg_name = 1; - // Path of the package. - string pkg_path = 2; -} - -// Message representing a key-value argument for KCL. -// kcl main.k -D name=value -message Argument { - // Name of the argument. - string name = 1; - // Value of the argument. - string value = 2; -} - -// ---------------------------------------------------------------------------- -// Error types -// ---------------------------------------------------------------------------- - -// Message representing an error. -message Error { - // Level of the error (e.g., "Error", "Warning"). - string level = 1; - // Error code. (e.g., "E1001") - string code = 2; - // List of error messages. - repeated Message messages = 3; -} - -// Message representing a detailed error message with a position. -message Message { - // The error message text. - string msg = 1; - // The position in the source code where the error occurred. - Position pos = 2; -} - -// ---------------------------------------------------------------------------- -// service request/response -// ---------------------------------------------------------------------------- - -// Service for built-in functionality. -service BuiltinService { - // Sends a ping request. - rpc Ping(Ping_Args) returns (Ping_Result); - // Lists available methods. - rpc ListMethod(ListMethod_Args) returns (ListMethod_Result); -} - -// Service for KCL VM interactions. -service KclvmService { - /// Ping KclvmService, return the same value as the parameter - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "Ping", - /// "params": { - /// "value": "hello" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "value": "hello" - /// }, - /// "id": 1 - /// } - /// ``` - rpc Ping(Ping_Args) returns (Ping_Result); - - /// GetVersion KclvmService, return the kclvm service version information - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "GetVersion", - /// "params": {}, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "version": "0.9.3", - /// "checksum": "c020ab3eb4b9179219d6837a57f5d323", - /// "git_sha": "1a9a72942fffc9f62cb8f1ae4e1d5ca32aa1f399", - /// "version_info": "Version: 0.9.3-c020ab3eb4b9179219d6837a57f5d323\nPlatform: aarch64-apple-darwin\nGitCommit: 1a9a72942fffc9f62cb8f1ae4e1d5ca32aa1f399" - /// }, - /// "id": 1 - /// } - /// ``` - rpc GetVersion(GetVersion_Args) returns (GetVersion_Result); - - /// Parse KCL program with entry files. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "ParseProgram", - /// "params": { - /// "paths": ["./src/testdata/test.k"] - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "ast_json": "{...}", - /// "paths": ["./src/testdata/test.k"], - /// "errors": [] - /// }, - /// "id": 1 - /// } - /// ``` - rpc ParseProgram(ParseProgram_Args) returns (ParseProgram_Result); - - /// Parse KCL single file to Module AST JSON string with import dependencies - /// and parse errors. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "ParseFile", - /// "params": { - /// "path": "./src/testdata/parse/main.k" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "ast_json": "{...}", - /// "deps": ["./dep1", "./dep2"], - /// "errors": [] - /// }, - /// "id": 1 - /// } - /// ``` - rpc ParseFile(ParseFile_Args) returns (ParseFile_Result); - - /// load_package provides users with the ability to parse kcl program and semantic model - /// information including symbols, types, definitions, etc. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "LoadPackage", - /// "params": { - /// "parse_args": { - /// "paths": ["./src/testdata/parse/main.k"] - /// }, - /// "resolve_ast": true - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "program": "{...}", - /// "paths": ["./src/testdata/parse/main.k"], - /// "parse_errors": [], - /// "type_errors": [], - /// "symbols": { ... }, - /// "scopes": { ... }, - /// "node_symbol_map": { ... }, - /// "symbol_node_map": { ... }, - /// "fully_qualified_name_map": { ... }, - /// "pkg_scope_map": { ... } - /// }, - /// "id": 1 - /// } - /// ``` - rpc LoadPackage(LoadPackage_Args) returns (LoadPackage_Result); - - /// list_options provides users with the ability to parse kcl program and get all option information. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "ListOptions", - /// "params": { - /// "paths": ["./src/testdata/option/main.k"] - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "options": [ - /// { "name": "option1", "type": "str", "required": true, "default_value": "", "help": "option 1 help" }, - /// { "name": "option2", "type": "int", "required": false, "default_value": "0", "help": "option 2 help" }, - /// { "name": "option3", "type": "bool", "required": false, "default_value": "false", "help": "option 3 help" } - /// ] - /// }, - /// "id": 1 - /// } - /// ``` - rpc ListOptions(ParseProgram_Args) returns (ListOptions_Result); - - /// list_variables provides users with the ability to parse kcl program and get all variables by specs. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "ListVariables", - /// "params": { - /// "files": ["./src/testdata/variables/main.k"], - /// "specs": ["a"] - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "variables": { - /// "a": { - /// "variables": [ - /// { "value": "1", "type_name": "int", "op_sym": "", "list_items": [], "dict_entries": [] } - /// ] - /// } - /// }, - /// "unsupported_codes": [], - /// "parse_errors": [] - /// }, - /// "id": 1 - /// } - /// ``` - rpc ListVariables(ListVariables_Args) returns (ListVariables_Result); - - /// Execute KCL file with args. **Note that it is not thread safe.** - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "ExecProgram", - /// "params": { - /// "work_dir": "./src/testdata", - /// "k_filename_list": ["test.k"] - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "json_result": "{\"alice\": {\"age\": 18}}", - /// "yaml_result": "alice:\n age: 18", - /// "log_message": "", - /// "err_message": "" - /// }, - /// "id": 1 - /// } - /// - /// // Request with code - /// { - /// "jsonrpc": "2.0", - /// "method": "ExecProgram", - /// "params": { - /// "k_filename_list": ["file.k"], - /// "k_code_list": ["alice = {age = 18}"] - /// }, - /// "id": 2 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "json_result": "{\"alice\": {\"age\": 18}}", - /// "yaml_result": "alice:\n age: 18", - /// "log_message": "", - /// "err_message": "" - /// }, - /// "id": 2 - /// } - /// - /// // Error case - cannot find file - /// { - /// "jsonrpc": "2.0", - /// "method": "ExecProgram", - /// "params": { - /// "k_filename_list": ["invalid_file.k"] - /// }, - /// "id": 3 - /// } - /// - /// // Error Response - /// { - /// "jsonrpc": "2.0", - /// "error": { - /// "code": -32602, - /// "message": "Cannot find the kcl file" - /// }, - /// "id": 3 - /// } - /// - /// // Error case - no input files - /// { - /// "jsonrpc": "2.0", - /// "method": "ExecProgram", - /// "params": { - /// "k_filename_list": [] - /// }, - /// "id": 4 - /// } - /// - /// // Error Response - /// { - /// "jsonrpc": "2.0", - /// "error": { - /// "code": -32602, - /// "message": "No input KCL files or paths" - /// }, - /// "id": 4 - /// } - /// ``` - rpc ExecProgram(ExecProgram_Args) returns (ExecProgram_Result); - - /// Build the KCL program to an artifact. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "BuildProgram", - /// "params": { - /// "exec_args": { - /// "work_dir": "./src/testdata", - /// "k_filename_list": ["test.k"] - /// }, - /// "output": "./build" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "path": "./build/test.k" - /// }, - /// "id": 1 - /// } - /// ``` - rpc BuildProgram(BuildProgram_Args) returns (BuildProgram_Result); - - /// Execute the KCL artifact with args. **Note that it is not thread safe.** - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "ExecArtifact", - /// "params": { - /// "path": "./artifact_path", - /// "exec_args": { - /// "work_dir": "./src/testdata", - /// "k_filename_list": ["test.k"] - /// } - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "json_result": "{\"alice\": {\"age\": 18}}", - /// "yaml_result": "alice:\n age: 18", - /// "log_message": "", - /// "err_message": "" - /// }, - /// "id": 1 - /// } - /// ``` - rpc ExecArtifact(ExecArtifact_Args) returns (ExecProgram_Result); - - /// Override KCL file with args. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "OverrideFile", - /// "params": { - /// "file": "./src/testdata/test.k", - /// "specs": ["alice.age=18"] - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "result": true, - /// "parse_errors": [] - /// }, - /// "id": 1 - /// } - /// ``` - rpc OverrideFile(OverrideFile_Args) returns (OverrideFile_Result); - - /// Get schema type mapping. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "GetSchemaTypeMapping", - /// "params": { - /// "exec_args": { - /// "work_dir": "./src/testdata", - /// "k_filename_list": ["main.k"], - /// "external_pkgs": [ - /// { - /// "pkg_name":"pkg", - /// "pkg_path": "./src/testdata/pkg" - /// } - /// ] - /// }, - /// "schema_name": "Person" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "schema_type_mapping": { - /// "Person": { - /// "type": "schema", - /// "schema_name": "Person", - /// "properties": { - /// "name": { "type": "str" }, - /// "age": { "type": "int" } - /// }, - /// "required": ["name", "age"], - /// "decorators": [] - /// } - /// } - /// }, - /// "id": 1 - /// } - /// ``` - rpc GetSchemaTypeMapping(GetSchemaTypeMapping_Args) returns (GetSchemaTypeMapping_Result); - - /// Format code source. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "FormatCode", - /// "params": { - /// "source": "schema Person {\n name: str\n age: int\n}\nperson = Person {\n name = \"Alice\"\n age = 18\n}\n" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "formatted": "schema Person {\n name: str\n age: int\n}\nperson = Person {\n name = \"Alice\"\n age = 18\n}\n" - /// }, - /// "id": 1 - /// } - /// ``` - rpc FormatCode(FormatCode_Args) returns (FormatCode_Result); - - /// Format KCL file or directory path contains KCL files and returns the changed file paths. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "FormatPath", - /// "params": { - /// "path": "./src/testdata/test.k" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "changed_paths": [] - /// }, - /// "id": 1 - /// } - /// ``` - rpc FormatPath(FormatPath_Args) returns (FormatPath_Result); - - /// Lint files and return error messages including errors and warnings. - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "LintPath", - /// "params": { - /// "paths": ["./src/testdata/test-lint.k"] - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "results": ["Module 'math' imported but unused"] - /// }, - /// "id": 1 - /// } - /// ``` - rpc LintPath(LintPath_Args) returns (LintPath_Result); - - /// Validate code using schema and data strings. - /// - /// **Note that it is not thread safe.** - /// - /// # Examples - /// - /// ```jsonrpc - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "ValidateCode", - /// "params": { - /// "code": "schema Person {\n name: str\n age: int\n check: 0 < age < 120\n}", - /// "data": "{\"name\": \"Alice\", \"age\": 10}" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "success": true, - /// "err_message": "" - /// }, - /// "id": 1 - /// } - /// ``` - rpc ValidateCode(ValidateCode_Args) returns (ValidateCode_Result); - - rpc ListDepFiles(ListDepFiles_Args) returns (ListDepFiles_Result); - /// Build setting file config from args. - /// - /// # Examples - /// - /// - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "LoadSettingsFiles", - /// "params": { - /// "work_dir": "./src/testdata/settings", - /// "files": ["./src/testdata/settings/kcl.yaml"] - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "kcl_cli_configs": { - /// "files": ["./src/testdata/settings/kcl.yaml"], - /// "output": "", - /// "overrides": [], - /// "path_selector": [], - /// "strict_range_check": false, - /// "disable_none": false, - /// "verbose": 0, - /// "debug": false, - /// "sort_keys": false, - /// "show_hidden": false, - /// "include_schema_type_path": false, - /// "fast_eval": false - /// }, - /// "kcl_options": [] - /// }, - /// "id": 1 - /// } - /// ``` - rpc LoadSettingsFiles(LoadSettingsFiles_Args) returns (LoadSettingsFiles_Result); - - /// Rename all the occurrences of the target symbol in the files. This API will rewrite files if they contain symbols to be renamed. - /// Return the file paths that got changed. - /// - /// # Examples - /// - /// - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "Rename", - /// "params": { - /// "package_root": "./src/testdata/rename_doc", - /// "symbol_path": "a", - /// "file_paths": ["./src/testdata/rename_doc/main.k"], - /// "new_name": "a2" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "changed_files": ["./src/testdata/rename_doc/main.k"] - /// }, - /// "id": 1 - /// } - /// ``` - rpc Rename(Rename_Args) returns (Rename_Result); - - /// Rename all the occurrences of the target symbol and return the modified code if any code has been changed. This API won't rewrite files but return the changed code. - /// - /// # Examples - /// - /// - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "RenameCode", - /// "params": { - /// "package_root": "/mock/path", - /// "symbol_path": "a", - /// "source_codes": { - /// "/mock/path/main.k": "a = 1\nb = a" - /// }, - /// "new_name": "a2" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "changed_codes": { - /// "/mock/path/main.k": "a2 = 1\nb = a2" - /// } - /// }, - /// "id": 1 - /// } - /// ``` - rpc RenameCode(RenameCode_Args) returns (RenameCode_Result); - - /// Test KCL packages with test arguments. - /// - /// # Examples - /// - /// - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "Test", - /// "params": { - /// "exec_args": { - /// "work_dir": "./src/testdata/testing/module", - /// "k_filename_list": ["main.k"] - /// }, - /// "pkg_list": ["./src/testdata/testing/module/..."] - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "info": [ - /// {"name": "test_case_1", "error": "", "duration": 1000, "log_message": ""}, - /// {"name": "test_case_2", "error": "some error", "duration": 2000, "log_message": ""} - /// ] - /// }, - /// "id": 1 - /// } - /// ``` - rpc Test(Test_Args) returns (Test_Result); - - /// Download and update dependencies defined in the kcl.mod file. - /// - /// # Examples - /// - /// - /// // Request - /// { - /// "jsonrpc": "2.0", - /// "method": "UpdateDependencies", - /// "params": { - /// "manifest_path": "./src/testdata/update_dependencies" - /// }, - /// "id": 1 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "external_pkgs": [ - /// {"pkg_name": "pkg1", "pkg_path": "./src/testdata/update_dependencies/pkg1"} - /// ] - /// }, - /// "id": 1 - /// } - /// - /// // Request with vendor flag - /// { - /// "jsonrpc": "2.0", - /// "method": "UpdateDependencies", - /// "params": { - /// "manifest_path": "./src/testdata/update_dependencies", - /// "vendor": true - /// }, - /// "id": 2 - /// } - /// - /// // Response - /// { - /// "jsonrpc": "2.0", - /// "result": { - /// "external_pkgs": [ - /// {"pkg_name": "pkg1", "pkg_path": "./src/testdata/update_dependencies/pkg1"} - /// ] - /// }, - /// "id": 2 - /// } - /// ``` - rpc UpdateDependencies(UpdateDependencies_Args) returns (UpdateDependencies_Result); -} - -// Message for ping request arguments. -message Ping_Args { - // Value to be sent in the ping request. - string value = 1; -} - -// Message for ping response. -message Ping_Result { - // Value received in the ping response. - string value = 1; -} - -// Message for version request arguments. Empty message. -message GetVersion_Args { - // empty -} - -// Message for version response. -message GetVersion_Result { - // KCL version. - string version = 1; - // Checksum of the KCL version. - string checksum = 2; - // Git Git SHA of the KCL code repo. - string git_sha = 3; - // Detailed version information as a string. - string version_info = 4; -} - -// Message for list method request arguments. Empty message. -message ListMethod_Args { - // empty -} - -// Message for list method response. -message ListMethod_Result { - // List of available method names. - repeated string method_name_list = 1; -} - -// Message for parse file request arguments. -message ParseFile_Args { - // Path of the file to be parsed. - string path = 1; - // Source code to be parsed. - string source = 2; - // External packages path. - repeated ExternalPkg external_pkgs = 3; -} - -// Message for parse file response. -message ParseFile_Result { - // Abstract Syntax Tree (AST) in JSON format. - string ast_json = 1; - // File dependency paths. - repeated string deps = 2; - // List of parse errors. - repeated Error errors = 3; -} - -// Message for parse program request arguments. -message ParseProgram_Args { - // Paths of the program files to be parsed. - repeated string paths = 1; - // Source codes to be parsed. - repeated string sources = 2; - // External packages path. - repeated ExternalPkg external_pkgs = 3; -} - -// Message for parse program response. -message ParseProgram_Result { - // Abstract Syntax Tree (AST) in JSON format. - string ast_json = 1; - // Returns the files in the order they should be compiled. - repeated string paths = 2; - // List of parse errors. - repeated Error errors = 3; -} - -// Message for load package request arguments. -message LoadPackage_Args { - // Arguments for parsing the program. - ParseProgram_Args parse_args = 1; - // Flag indicating whether to resolve AST. - bool resolve_ast = 2; - // Flag indicating whether to load built-in modules. - bool load_builtin = 3; - // Flag indicating whether to include AST index. - bool with_ast_index = 4; -} - -// Message for load package response. -message LoadPackage_Result { - // Program Abstract Syntax Tree (AST) in JSON format. - string program = 1; - // Returns the files in the order they should be compiled. - repeated string paths = 2; - // List of parse errors. - repeated Error parse_errors = 3; - // List of type errors. - repeated Error type_errors = 4; - // Map of scopes with scope index as key. - map scopes = 5; - // Map of symbols with symbol index as key. - map symbols = 6; - // Map of node-symbol associations with AST index UUID as key. - map node_symbol_map = 7; - // Map of symbol-node associations with symbol index as key. - map symbol_node_map = 8; - // Map of fully qualified names with symbol index as key. - map fully_qualified_name_map = 9; - // Map of package scope with package path as key. - map pkg_scope_map = 10; -} - -// Message for list options response. -message ListOptions_Result { - // List of available options. - repeated OptionHelp options = 2; -} - -// Message representing a help option. -message OptionHelp { - // Name of the option. - string name = 1; - // Type of the option. - string type = 2; - // Flag indicating if the option is required. - bool required = 3; - // Default value of the option. - string default_value = 4; - // Help text for the option. - string help = 5; -} - -// Message representing a symbol in KCL. -message Symbol { - // Type of the symbol. - KclType ty = 1; - // Name of the symbol. - string name = 2; - // Owner of the symbol. - SymbolIndex owner = 3; - // Definition of the symbol. - SymbolIndex def = 4; - // Attributes of the symbol. - repeated SymbolIndex attrs = 5; - // Flag indicating if the symbol is global. - bool is_global = 6; -} - -// Message representing a scope in KCL. -message Scope { - // Type of the scope. - string kind = 1; - // Parent scope. - ScopeIndex parent = 2; - // Owner of the scope. - SymbolIndex owner = 3; - // Children of the scope. - repeated ScopeIndex children = 4; - // Definitions in the scope. - repeated SymbolIndex defs = 5; -} - -// Message representing a symbol index. -message SymbolIndex { - // Index identifier. - uint64 i = 1; - // Global identifier. - uint64 g = 2; - // Type of the symbol or scope. - string kind = 3; -} - -// Message representing a scope index. -message ScopeIndex { - // Index identifier. - uint64 i = 1; - // Global identifier. - uint64 g = 2; - // Type of the scope. - string kind = 3; -} - -// Message for execute program request arguments. -message ExecProgram_Args { - // Working directory. - string work_dir = 1; - // List of KCL filenames. - repeated string k_filename_list = 2; - // List of KCL codes. - repeated string k_code_list = 3; - // Arguments for the program. - repeated Argument args = 4; - // Override configurations. - repeated string overrides = 5; - // Flag to disable YAML result. - bool disable_yaml_result = 6; - // Flag to print override AST. - bool print_override_ast = 7; - // Flag for strict range check. - bool strict_range_check = 8; - // Flag to disable none values. - bool disable_none = 9; - // Verbose level. - int32 verbose = 10; - // Debug level. - int32 debug = 11; - // Flag to sort keys in YAML/JSON results. - bool sort_keys = 12; - // External packages path. - repeated ExternalPkg external_pkgs = 13; - // Flag to include schema type path in results. - bool include_schema_type_path = 14; - // Flag to compile only without execution. - bool compile_only = 15; - // Flag to show hidden attributes. - bool show_hidden = 16; - // Path selectors for results. - repeated string path_selector = 17; - // Flag for fast evaluation. - bool fast_eval = 18; -} - -// Message for execute program response. -message ExecProgram_Result { - // Result in JSON format. - string json_result = 1; - // Result in YAML format. - string yaml_result = 2; - // Log message from execution. - string log_message = 3; - // Error message from execution. - string err_message = 4; -} - -// Message for build program request arguments. -message BuildProgram_Args { - // Arguments for executing the program. - ExecProgram_Args exec_args = 1; - // Output path. - string output = 2; -} - -// Message for build program response. -message BuildProgram_Result { - // Path of the built program. - string path = 1; -} - -// Message for execute artifact request arguments. -message ExecArtifact_Args { - // Path of the artifact. - string path = 1; - // Arguments for executing the program. - ExecProgram_Args exec_args = 2; -} - -// Message for format code request arguments. -message FormatCode_Args { - // Source code to be formatted. - string source = 1; -} - -// Message for format code response. -message FormatCode_Result { - // Formatted code as bytes. - bytes formatted = 1; -} - -// Message for format file path request arguments. -message FormatPath_Args { - // Path of the file to format. - string path = 1; -} - -// Message for format file path response. -message FormatPath_Result { - // List of changed file paths. - repeated string changed_paths = 1; -} - -// Message for lint file path request arguments. -message LintPath_Args { - // Paths of the files to lint. - repeated string paths = 1; -} - -// Message for lint file path response. -message LintPath_Result { - // List of lint results. - repeated string results = 1; -} - -// Message for override file request arguments. -message OverrideFile_Args { - // Path of the file to override. - string file = 1; - // List of override specifications. - repeated string specs = 2; - // List of import paths. - repeated string import_paths = 3; -} - -// Message for override file response. -message OverrideFile_Result { - // Result of the override operation. - bool result = 1; - // List of parse errors encountered. - repeated Error parse_errors = 2; -} - -// Message for list variables options. -message ListVariables_Options { - // Flag to merge program configuration. - bool merge_program = 1; -} - -// Message representing a list of variables. -message VariableList { - // List of variables. - repeated Variable variables = 1; -} - -// Message for list variables request arguments. -message ListVariables_Args { - // Files to be processed. - repeated string files = 1; - // Specifications for variables. - repeated string specs = 2; - // Options for listing variables. - ListVariables_Options options = 3; -} - -// Message for list variables response. -message ListVariables_Result { - // Map of variable lists by file. - map variables = 1; - // List of unsupported codes. - repeated string unsupported_codes = 2; - // List of parse errors encountered. - repeated Error parse_errors = 3; -} - -// Message representing a variable. -message Variable { - // Value of the variable. - string value = 1; - // Type name of the variable. - string type_name = 2; - // Operation symbol associated with the variable. - string op_sym = 3; - // List items if the variable is a list. - repeated Variable list_items = 4; - // Dictionary entries if the variable is a dictionary. - repeated MapEntry dict_entries = 5; -} - -// Message representing a map entry. -message MapEntry { - // Key of the map entry. - string key = 1; - // Value of the map entry. - Variable value = 2; -} - -// Message for get schema type mapping request arguments. -message GetSchemaTypeMapping_Args { - // Arguments for executing the program. - ExecProgram_Args exec_args = 1; - // Name of the schema. - string schema_name = 2; -} - -// Message for get schema type mapping response. -message GetSchemaTypeMapping_Result { - // Map of schema type mappings. - map schema_type_mapping = 1; -} - -// Message for validate code request arguments. -message ValidateCode_Args { - // Path to the data file. - string datafile = 1; - // Data content. - string data = 2; - // Path to the code file. - string file = 3; - // Source code content. - string code = 4; - // Name of the schema. - string schema = 5; - // Name of the attribute. - string attribute_name = 6; - // Format of the validation (e.g., "json", "yaml"). - string format = 7; -} - -// Message for validate code response. -message ValidateCode_Result { - // Flag indicating if validation was successful. - bool success = 1; - // Error message from validation. - string err_message = 2; -} - -// Message representing a position in the source code. -message Position { - // Line number. - int64 line = 1; - // Column number. - int64 column = 2; - // Filename the position refers to. - string filename = 3; -} - -// Message for list dependency files request arguments. -message ListDepFiles_Args { - // Working directory. - string work_dir = 1; - // Flag to use absolute paths. - bool use_abs_path = 2; - // Flag to include all files. - bool include_all = 3; - // Flag to use fast parser. - bool use_fast_parser = 4; -} - -// Message for list dependency files response. -message ListDepFiles_Result { - // Root package path. - string pkgroot = 1; - // Package path. - string pkgpath = 2; - // List of file paths in the package. - repeated string files = 3; -} - -// --------------------------------------------------------------------------------- -// LoadSettingsFiles API -// Input work dir and setting files and return the merged kcl singleton config. -// --------------------------------------------------------------------------------- - -// Message for load settings files request arguments. -message LoadSettingsFiles_Args { - // Working directory. - string work_dir = 1; - // Setting files to load. - repeated string files = 2; -} - -// Message for load settings files response. -message LoadSettingsFiles_Result { - // KCL CLI configuration. - CliConfig kcl_cli_configs = 1; - // List of KCL options as key-value pairs. - repeated KeyValuePair kcl_options = 2; -} - -// Message representing KCL CLI configuration. -message CliConfig { - // List of files. - repeated string files = 1; - // Output path. - string output = 2; - // List of overrides. - repeated string overrides = 3; - // Path selectors. - repeated string path_selector = 4; - // Flag for strict range check. - bool strict_range_check = 5; - // Flag to disable none values. - bool disable_none = 6; - // Verbose level. - int64 verbose = 7; - // Debug flag. - bool debug = 8; - // Flag to sort keys in YAML/JSON results. - bool sort_keys = 9; - // Flag to show hidden attributes. - bool show_hidden = 10; - // Flag to include schema type path in results. - bool include_schema_type_path = 11; - // Flag for fast evaluation. - bool fast_eval = 12; -} - -// Message representing a key-value pair. -message KeyValuePair { - // Key of the pair. - string key = 1; - // Value of the pair. - string value = 2; -} - -// --------------------------------------------------------------------------------- -// Rename API -// Find all the occurrences of the target symbol and rename them. -// This API will rewrite files if they contain symbols to be renamed. -// --------------------------------------------------------------------------------- - -// Message for rename request arguments. -message Rename_Args { - // File path to the package root. - string package_root = 1; - // Path to the target symbol to be renamed. - string symbol_path = 2; - // Paths to the source code files. - repeated string file_paths = 3; - // New name of the symbol. - string new_name = 4; -} - -// Message for rename response. -message Rename_Result { - // List of file paths that got changed. - repeated string changed_files = 1; -} - -// --------------------------------------------------------------------------------- -// RenameCode API -// Find all the occurrences of the target symbol and rename them. -// This API won't rewrite files but return the modified code if any code has been changed. -// --------------------------------------------------------------------------------- - -// Message for rename code request arguments. -message RenameCode_Args { - // File path to the package root. - string package_root = 1; - // Path to the target symbol to be renamed. - string symbol_path = 2; - // Map of source code with filename as key and code as value. - map source_codes = 3; - // New name of the symbol. - string new_name = 4; -} - -// Message for rename code response. -message RenameCode_Result { - // Map of changed code with filename as key and modified code as value. - map changed_codes = 1; -} - -// --------------------------------------------------------------------------------- -// Test API -// Test KCL packages with test arguments. -// --------------------------------------------------------------------------------- - -// Message for test request arguments. -message Test_Args { - // Execution program arguments. - ExecProgram_Args exec_args = 1; - // List of KCL package paths to be tested. - repeated string pkg_list = 2; - // Regular expression for filtering tests to run. - string run_regexp = 3; - // Flag to stop the test run on the first failure. - bool fail_fast = 4; -} - -// Message for test response. -message Test_Result { - // List of test case information. - repeated TestCaseInfo info = 2; -} - -// Message representing information about a single test case. -message TestCaseInfo { - // Name of the test case. - string name = 1; - // Error message if any. - string error = 2; - // Duration of the test case in microseconds. - uint64 duration = 3; - // Log message from the test case. - string log_message = 4; -} - -// --------------------------------------------------------------------------------- -// UpdateDependencies API -// Download and update dependencies defined in the kcl.mod file. -// --------------------------------------------------------------------------------- - -// Message for update dependencies request arguments. -message UpdateDependencies_Args { - // Path to the manifest file. - string manifest_path = 1; - // Flag to vendor dependencies locally. - bool vendor = 2; -} - -// Message for update dependencies response. -message UpdateDependencies_Result { - // List of external packages updated. - repeated ExternalPkg external_pkgs = 3; -} - -// ---------------------------------------------------------------------------- -// KCL Type Structure -// ---------------------------------------------------------------------------- - -// Message representing a KCL type. -message KclType { - // Type name (e.g., schema, dict, list, str, int, float, bool, any, union, number_multiplier). - string type = 1; - // Union types if applicable. - repeated KclType union_types = 2; - // Default value of the type. - string default = 3; - // Name of the schema if applicable. - string schema_name = 4; - // Documentation for the schema. - string schema_doc = 5; - // Properties of the schema as a map with property name as key. - map properties = 6; - // List of required schema properties. - repeated string required = 7; - // Key type if the KclType is a dictionary. - KclType key = 8; - // Item type if the KclType is a list or dictionary. - KclType item = 9; - // Line number where the type is defined. - int32 line = 10; - // List of decorators for the schema. - repeated Decorator decorators = 11; - // Absolute path of the file where the attribute is located. - string filename = 12; - // Path of the package where the attribute is located. - string pkg_path = 13; - // Documentation for the attribute. - string description = 14; - // Map of examples with example name as key. - map examples = 15; - // Base schema if applicable. - KclType base_schema = 16; -} - -// Message representing a decorator in KCL. -message Decorator { - // Name of the decorator. - string name = 1; - // Arguments for the decorator. - repeated string arguments = 2; - // Keyword arguments for the decorator as a map with keyword name as key. - map keywords = 3; -} - -// Message representing an example in KCL. -message Example { - // Short description for the example. - string summary = 1; - // Long description for the example. - string description = 2; - // Embedded literal example. - string value = 3; -} - -// ---------------------------------------------------------------------------- -// END -// ----------------------------------------------------------------------------