From 3879a393e8f20fd1f35075390d4e109c25ec8163 Mon Sep 17 00:00:00 2001 From: peefy Date: Mon, 15 Jul 2024 15:45:10 +0800 Subject: [PATCH] feat: add all Java API tests and reference docs Signed-off-by: peefy --- java/README.md | 609 ++++++++++++++++++ java/src/main/java/com/kcl/api/API.java | 401 +++++++++++- java/src/main/java/com/kcl/api/Service.java | 6 + .../test/java/com/kcl/ExecProgramTest.java | 22 + java/src/test/java/com/kcl/FormatTest.java | 35 + .../test/java/com/kcl/GetSchemaTypeTest.java | 29 + .../src/test/java/com/kcl/GetVersionTest.java | 20 + java/src/test/java/com/kcl/LintTest.java | 22 + .../test/java/com/kcl/ListOptionsTest.java | 21 + .../java/com/kcl/LoadSettingsFileTest.java | 22 + java/src/test/java/com/kcl/ParseTest.java | 20 + java/src/test/java/com/kcl/RenameTest.java | 40 ++ java/src/test/java/com/kcl/TestingTest.java | 18 + .../java/com/kcl/UpdateDependenciesTest.java | 18 + java/src/test/java/com/kcl/ValidateTest.java | 25 + java/src/test_data/format_path/test.k | 1 + java/src/test_data/get_schema_ty/aaa/kcl.mod | 5 + java/src/test_data/get_schema_ty/aaa/main.k | 10 + java/src/test_data/get_schema_ty/bbb/kcl.mod | 5 + java/src/test_data/get_schema_ty/bbb/main.k | 2 + java/src/test_data/get_schema_ty/ccc/kcl.mod | 5 + java/src/test_data/get_schema_ty/ccc/main.k | 2 + java/src/test_data/lint_path/test-lint.k | 3 + java/src/test_data/option/main.k | 5 + java/src/test_data/parse/kcl.mod | 0 java/src/test_data/parse/main.k | 5 + java/src/test_data/parse/pkg1/pkg.k | 1 + java/src/test_data/parse/pkg2/pkg.k | 1 + java/src/test_data/rename/main.bak | 2 + java/src/test_data/rename/main.k | 2 + java/src/test_data/settings/kcl.yaml | 5 + java/src/test_data/testing/module/kcl.mod | 3 + java/src/test_data/testing/module/pkg/func.k | 3 + .../test_data/testing/module/pkg/func_test.k | 7 + java/src/test_data/update_dependencies/main.k | 4 + java/src/test_data/variables/main.k | 3 + python/README.md | 2 +- 37 files changed, 1368 insertions(+), 16 deletions(-) create mode 100644 java/src/test/java/com/kcl/ExecProgramTest.java create mode 100644 java/src/test/java/com/kcl/FormatTest.java create mode 100644 java/src/test/java/com/kcl/GetSchemaTypeTest.java create mode 100644 java/src/test/java/com/kcl/GetVersionTest.java create mode 100644 java/src/test/java/com/kcl/LintTest.java create mode 100644 java/src/test/java/com/kcl/ListOptionsTest.java create mode 100644 java/src/test/java/com/kcl/LoadSettingsFileTest.java create mode 100644 java/src/test/java/com/kcl/ParseTest.java create mode 100644 java/src/test/java/com/kcl/RenameTest.java create mode 100644 java/src/test/java/com/kcl/TestingTest.java create mode 100644 java/src/test/java/com/kcl/ValidateTest.java create mode 100644 java/src/test_data/format_path/test.k create mode 100644 java/src/test_data/get_schema_ty/aaa/kcl.mod create mode 100644 java/src/test_data/get_schema_ty/aaa/main.k create mode 100644 java/src/test_data/get_schema_ty/bbb/kcl.mod create mode 100644 java/src/test_data/get_schema_ty/bbb/main.k create mode 100644 java/src/test_data/get_schema_ty/ccc/kcl.mod create mode 100644 java/src/test_data/get_schema_ty/ccc/main.k create mode 100644 java/src/test_data/lint_path/test-lint.k create mode 100644 java/src/test_data/option/main.k create mode 100644 java/src/test_data/parse/kcl.mod create mode 100644 java/src/test_data/parse/main.k create mode 100644 java/src/test_data/parse/pkg1/pkg.k create mode 100644 java/src/test_data/parse/pkg2/pkg.k create mode 100644 java/src/test_data/rename/main.bak create mode 100644 java/src/test_data/rename/main.k create mode 100644 java/src/test_data/settings/kcl.yaml create mode 100644 java/src/test_data/testing/module/kcl.mod create mode 100644 java/src/test_data/testing/module/pkg/func.k create mode 100644 java/src/test_data/testing/module/pkg/func_test.k create mode 100644 java/src/test_data/update_dependencies/main.k create mode 100644 java/src/test_data/variables/main.k diff --git a/java/README.md b/java/README.md index 9ca9ba0..cb3cb07 100644 --- a/java/README.md +++ b/java/README.md @@ -73,3 +73,612 @@ make test ```shell make fmt ``` + +## API Reference + +### execProgram + +Execute KCL file with arguments and return the JSON/YAML result. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Java Code + +```java +import com.kcl.api.*; + +ExecProgram_Args args = ExecProgram_Args.newBuilder().addKFilenameList("schema.k").build(); +API apiInstance = new API(); +ExecProgram_Result result = apiInstance.execProgram(args); +``` + +

+
+ +### parseProgram + +Parse KCL program with entry files and return the AST JSON string. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Java Code + +```java +import com.kcl.api.*; +import com.kcl.ast.*; +import com.kcl.util.JsonUtil; + +API api = new API(); +ParseProgram_Result result = api.parseProgram( + ParseProgram_Args.newBuilder().addPaths("schema.k").build() +); +System.out.println(result.getAstJson()); +Program program = JsonUtil.deserializeProgram(result.getAstJson()); +``` + +

+
+ +### parseFile + +Parse KCL single file to Module AST JSON string with import dependencies and parse errors. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Java Code + +```java +import com.kcl.api.*; + +ParseFile_Args args = ParseFile_Args.newBuilder().setPath("schema.k").build(); +API apiInstance = new API(); +ParseFile_Result result = apiInstance.parseFile(args); +``` + +

+
+ +### parseProgram + +Parse KCL program with entry files and return the AST JSON string. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Java Code + +```java +import com.kcl.api.*; +import com.kcl.ast.*; +import com.kcl.util.JsonUtil; + +API api = new API(); +ParseProgram_Result result = api.parseProgram( + ParseProgram_Args.newBuilder().addPaths("path/to/kcl.k").build() +); +System.out.println(result.getAstJson()); +Program program = JsonUtil.deserializeProgram(result.getAstJson()); +``` + +

+
+ +### loadPackage + +loadPackage provides users with the ability to parse KCL program and semantic model information including symbols, types, definitions, etc. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Java Code + +```python +import kcl_lib.api as api + +args = api.LoadPackage_Args( + parse_args=api.ParseProgram_Args(paths=["schema.k"]), resolve_ast=True +) +api = api.API() +result = api.load_package(args) +assert list(result.symbols.values())[0].ty.schema_name == "AppConfig" +``` + +

+
+ +### listVariables + +listVariables provides users with the ability to parse KCL program and get all variables by specs. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Java Code + +```java +import com.kcl.api.*; + +API api = new API(); +ListVariables_Result result = api.listVariables( + ListVariables_Args.newBuilder().setResolveAst(true).setParseArgs( + ParseProgram_Args.newBuilder().addPaths("/path/to/kcl.k").build()) + .build()); +result.getSymbolsMap().values().forEach(s -> System.out.println(s)); +``` + +

+
+ +### listOptions + +listOptions provides users with the ability to parse KCL program and get all option information. + +
Example +

+ +The content of `options.k` is + +```python +a = option("key1") +b = option("key2", required=True) +c = { + metadata.key = option("metadata-key") +} +``` + +Java Code + +```java +import com.kcl.api.*; + +ParseProgram_Args args = ParseProgram_Args.newBuilder().addPaths("./src/test_data/option/main.k").build(); +API apiInstance = new API(); +ListOptions_Result result = apiInstance.listOptions(args); +``` + +

+
+ +### getSchemaTypeMapping + +Get schema type mapping defined in the program. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Java Code + +```java +import com.kcl.api.*; + +ExecProgram_Args execArgs = ExecProgram_Args.newBuilder().addKFilenameList("schema.k").build(); +GetSchemaTypeMapping_Args args = GetSchemaTypeMapping_Args.newBuilder().setExecArgs(execArgs).build(); +API apiInstance = new API(); +GetSchemaTypeMapping_Result result = apiInstance.getSchemaTypeMapping(args); +KclType appSchemaType = result.getSchemaTypeMappingMap().get("app"); +String replicasType = appSchemaType.getPropertiesOrThrow("replicas").getType(); +``` + +

+
+ +### overrideFile + +Override KCL file with arguments. See [https://www.kcl-lang.io/docs/user_docs/guides/automation](https://www.kcl-lang.io/docs/user_docs/guides/automation) for more override spec guide. + +
Example +

+ +The content of `main.k` is + +```python +a = 1 + +b = { + "a": 1 + "b": 2 +} +``` + +Java Code + +```java +import com.kcl.api.*; + +API api = new API(); +String spec = "a=2"; +OverrideFile_Result result = api.overrideFile(OverrideFile_Args.newBuilder() + .setFile("./src/test_data/override_file/main.k").addSpecs(spec).build()); +``` + +

+
+ +### formatCode + +Format the code source. + +
Example +

+ +Java Code + +```java +import com.kcl.api.*; + +String sourceCode = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n" + + " 0 < age < 120\n"; +FormatCode_Args args = FormatCode_Args.newBuilder().setSource(sourceCode).build(); +API apiInstance = new API(); +FormatCode_Result result = apiInstance.formatCode(args); +String expectedFormattedCode = "schema Person:\n" + " name: str\n" + " age: int\n\n" + " check:\n" + + " 0 < age < 120\n\n"; +``` + +

+
+ +### formatPath + +Format KCL file or directory path contains KCL files and returns the changed file paths. + +
Example +

+ +The content of `format_path.k` is + +```python +schema Person: + name: str + age: int + + check: + 0 < age < 120 +``` + +Java Code + +```java +import com.kcl.api.*; + +FormatPath_Args args = FormatPath_Args.newBuilder().setPath("format_path.k").build(); +API apiInstance = new API(); +FormatPath_Result result = apiInstance.formatPath(args); +Assert.assertTrue(result.getChangedPathsList().isEmpty()); +``` + +

+
+ +### lintPath + +Lint files and return error messages including errors and warnings. + +
Example +

+ +The content of `lint_path.k` is + +```python +import math + +a = 1 +``` + +Java Code + +```java +import com.kcl.api.*; + +LintPath_Args args = LintPath_Args.newBuilder().addPaths("lint_path.k").build(); +API apiInstance = new API(); +LintPath_Result result = apiInstance.lintPath(args); +boolean foundWarning = result.getResultsList().stream() + .anyMatch(warning -> warning.contains("Module 'math' imported but unused")); +``` + +

+
+ +### validateCode + +Validate code using schema and JSON/YAML data strings. + +
Example +

+ +Java Code + +```java +import com.kcl.api.*; + +String code = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n" + + " 0 < age < 120\n"; +String data = "{\"name\": \"Alice\", \"age\": 10}"; +ValidateCode_Args args = ValidateCode_Args.newBuilder().setCode(code).setData(data).setFormat("json").build(); +API apiInstance = new API(); +ValidateCode_Result result = apiInstance.validateCode(args); +``` + +

+
+ +### rename + +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. + +
Example +

+ +The content of `main.k` is + +```python +a = 1 +b = a +``` + +Java Code + +```java +import com.kcl.api.*; + +Rename_Args args = Rename_Args.newBuilder().setPackageRoot(".").setSymbolPath("a") + .addFilePaths("main.k").setNewName("a2").build(); +API apiInstance = new API(); +Rename_Result result = apiInstance.rename(args); +``` + +

+
+ +### renameCode + +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. + +
Example +

+ +Java Code + +```java +import com.kcl.api.*; + +API api = new API(); +RenameCode_Args args = RenameCode_Args.newBuilder().setPackageRoot("/mock/path").setSymbolPath("a") + .putSourceCodes("/mock/path/main.k", "a = 1\nb = a").setNewName("a2").build(); +RenameCode_Result result = api.renameCode(args); +``` + +

+
+ +### test + +Test KCL packages with test arguments. + +
Example +

+ +Java Code + +```java +import com.kcl.api.*; + +API apiInstance = new API(); +Test_Args args = Test_Args.newBuilder().addPkgList("/path/to/test/package").build(); +Test_Result result = apiInstance.test(args); +``` + +

+
+ +### loadSettingsFiles + +Load the setting file config defined in `kcl.yaml` + +
Example +

+ +The content of `kcl.yaml` is + +```yaml +kcl_cli_configs: + strict_range_check: true +kcl_options: + - key: key + value: value +``` + +Java Code + +```java +import com.kcl.api.*; + +API api = new API(); +LoadSettingsFiles_Args args = LoadSettingsFiles_Args.newBuilder().addFiles("kcl.yaml") + .build(); +LoadSettingsFiles_Result result = api.loadSettingsFiles(args); +``` + +

+
+ +### updateDependencies + +Download and update dependencies defined in the `kcl.mod` file and return the external package name and location list. + +
Example +

+ +The content of `module/kcl.mod` is + +```yaml +[package] +name = "mod_update" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.0" } +flask = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests", commit = "ade147b" } +``` + +Java Code + +```java +import com.kcl.api.*; + +API api = new API(); + +UpdateDependencies_Result result = api.updateDependencies( + UpdateDependencies_Args.newBuilder().setManifestPath("module").build()); +``` + +

+
+ +Call `execProgram` with external dependencies + +
Example +

+ +The content of `module/kcl.mod` is + +```yaml +[package] +name = "mod_update" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.0" } +flask = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests", commit = "ade147b" } +``` + +The content of `module/main.k` is + +```python +import helloworld +import flask + +a = helloworld.The_first_kcl_program +``` + +Java Code + +```java +import com.kcl.api.*; + +API api = new API(); + +UpdateDependencies_Result result = api.updateDependencies( + UpdateDependencies_Args.newBuilder().setManifestPath("./src/test_data/update_dependencies").build()); + +ExecProgram_Args execArgs = ExecProgram_Args.newBuilder(). addAllExternalPkgs(result.getExternalPkgsList()) + .addKFilenameList("./src/test_data/update_dependencies/main.k").build(); + +ExecProgram_Result execResult = api.execProgram(execArgs); +``` + +

+
+ +### getVersion + +Return the KCL service version information. + +
Example +

+ +Java Code + +```java +import com.kcl.api.*; + +API api = new API(); +GetVersion_Args version_args = GetVersion_Args.newBuilder().build(); +GetVersion_Result result = api.getVersion(version_args); +``` + +

+
diff --git a/java/src/main/java/com/kcl/api/API.java b/java/src/main/java/com/kcl/api/API.java index 500ec3f..a0bae6e 100644 --- a/java/src/main/java/com/kcl/api/API.java +++ b/java/src/main/java/com/kcl/api/API.java @@ -19,7 +19,7 @@ public class API implements Service { loadLibrary(); } - public static void loadLibrary() { + private static void loadLibrary() { // Load the dynamic library (the .dll, .so, or .dylib file) try { doLoadLibrary(); @@ -81,7 +81,7 @@ public API() { } /** - * Parses a program given a set of file paths and returns the result. * + * Parses a program given a set of file paths and returns the result. * *

* Example usage: @@ -114,13 +114,37 @@ public ParseProgram_Result parseProgram(ParseProgram_Args args) throws Exception return ParseProgram_Result.parseFrom(call("KclvmService.ParseProgram", args.toByteArray())); } + /** + * Parses a single file and returns its AST and errors. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     * 
+     * ParseFile_Args args = ParseFile_Args.newBuilder().setPath("./src/test_data/parse/main.k").build();
+     * API apiInstance = new API();
+     * ParseFile_Result result = apiInstance.parseFile(args);
+     * }
+     * 
+ * + * @param args + * the arguments specifying the file path to be parsed. + * + * @return the result of parsing the file including the AST in JSON format and any errors. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public ParseFile_Result parseFile(ParseFile_Args args) throws Exception { return ParseFile_Result.parseFrom(call("KclvmService.ParseFile", args.toByteArray())); } /** - * Loads KCL package and returns the AST, symbol, type, definition information. * + * Loads a KCL package and returns AST, symbol, type, and definition information. * *

* Example usage: @@ -132,7 +156,7 @@ public ParseFile_Result parseFile(ParseFile_Args args) throws Exception { * API api = new API(); * LoadPackage_Result result = api.loadPackage( * LoadPackage_Args.newBuilder().setResolveAst(true).setParseArgs( - * ParseProgram_Args.newBuilder().addPaths("/path/to/kcl.k").build()) + * ParseProgram_Args.newBuilder().addPaths("/path/to/kcl.k").build()) * .build()); * result.getSymbolsMap().values().forEach(s -> System.out.println(s)); * } @@ -141,8 +165,7 @@ public ParseFile_Result parseFile(ParseFile_Args args) throws Exception { * @param args * the arguments specifying the file paths to be parsed and resolved. * - * @return the result of parsing the program and parse errors, type errors, including the AST in JSON format and - * symbol, type and definition information. + * @return the result including AST, symbol, type, and definition information. * * @throws Exception * if an error occurs during the remote procedure call. @@ -153,7 +176,7 @@ public LoadPackage_Result loadPackage(LoadPackage_Args args) throws Exception { } /** - * List variables and return the code text* + * Lists variables in the KCL program and returns detailed information. * *

* Example usage: @@ -172,10 +195,9 @@ public LoadPackage_Result loadPackage(LoadPackage_Args args) throws Exception { * * * @param args - * the arguments specifying the file paths to be parsed and resolved. + * the arguments specifying the file paths and other details. * - * @return the result of parsing the program and parse errors, type errors, including the AST in JSON format and - * symbol, type and definition information. + * @return the result including the variables found and their information. * * @throws Exception * if an error occurs during the remote procedure call. @@ -186,7 +208,36 @@ public ListVariables_Result listVariables(ListVariables_Args args) throws Except } /** - * Loads KCL package with the internal cache and returns the AST, symbol, type, definition information. * + * listOptions provides users with the ability to parse KCL program and get all option information. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * ParseProgram_Args args = ParseProgram_Args.newBuilder().addPaths("./src/test_data/option/main.k").build();
+     * API apiInstance = new API();
+     * ListOptions_Result result = apiInstance.listOptions(args);
+     * }
+     * 
+ * + * @param args + * the arguments specifying the file paths and other details. + * + * @return the result including the variables found and their information. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ + @Override + public ListOptions_Result listOptions(ParseProgram_Args args) throws Exception { + return ListOptions_Result.parseFrom(call("KclvmService.ListOptions", args.toByteArray())); + } + + /** + * Loads a KCL package using the internal cache and returns various details. * *

* Example usage: @@ -198,17 +249,16 @@ public ListVariables_Result listVariables(ListVariables_Args args) throws Except * API api = new API(); * LoadPackage_Result result = api.loadPackageWithCache( * LoadPackage_Args.newBuilder().setResolveAst(true).setParseArgs( - * ParseProgram_Args.newBuilder().addPaths("/path/to/kcl.k").build()) + * ParseProgram_Args.newBuilder().addPaths("/path/to/kcl.k").build()) * .build()); * result.getSymbolsMap().values().forEach(s -> System.out.println(s)); * } * * * @param args - * the arguments specifying the file paths to be parsed and resolved. + * the arguments specifying the file paths and resolution details. * - * @return the result of parsing the program and parse errors, type errors, including the AST in JSON format and - * symbol, type and definition information. + * @return the result including AST, symbol, type, and definition information. * * @throws Exception * if an error occurs during the remote procedure call. @@ -218,65 +268,386 @@ public LoadPackage_Result loadPackageWithCache(LoadPackage_Args args) throws Exc return LoadPackage_Result.parseFrom(callLoadPackageWithCache(args.toByteArray())); } + /** + * Executes a KCL program with the given arguments. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * ExecProgram_Args args = ExecProgram_Args.newBuilder().addKFilenameList("schema.k").build();
+     * API apiInstance = new API();
+     * ExecProgram_Result result = apiInstance.execProgram(args);
+     * }
+     * 
+ * + * @param args + * the arguments for executing the program. + * + * @return the result of executing the program. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ public ExecProgram_Result execProgram(ExecProgram_Args args) throws Exception { return ExecProgram_Result.parseFrom(call("KclvmService.ExecProgram", args.toByteArray())); } + /** + * Overrides a KCL file with new content. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * API api = new API();
+     * String spec = "a=2";
+     * OverrideFile_Result result = api.overrideFile(OverrideFile_Args.newBuilder()
+     *     .setFile("./src/test_data/override_file/main.k").addSpecs(spec).build());
+     * }
+     * 
+ * + * @param args + * the arguments specifying the file and the new content. + * + * @return the result of overriding the file. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public OverrideFile_Result overrideFile(OverrideFile_Args args) throws Exception { return OverrideFile_Result.parseFrom(call("KclvmService.OverrideFile", args.toByteArray())); } + /** + * Gets schema type mappings from a KCL program. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     * 
+     * ExecProgram_Args execArgs = ExecProgram_Args.newBuilder().addKFilenameList("schema.k").build();
+     * GetSchemaTypeMapping_Args args = GetSchemaTypeMapping_Args.newBuilder().setExecArgs(execArgs).build();
+     * API apiInstance = new API();
+     * GetSchemaTypeMapping_Result result = apiInstance.getSchemaTypeMapping(args);
+     * KclType appSchemaType = result.getSchemaTypeMappingMap().get("app");
+     * String replicasType = appSchemaType.getPropertiesOrThrow("replicas").getType();
+     * }
+     * 
+ * + * @param args + * the arguments specifying the program and schema. + * + * @return the schema type mappings in the program. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public GetSchemaTypeMapping_Result getSchemaTypeMapping(GetSchemaTypeMapping_Args args) throws Exception { return GetSchemaTypeMapping_Result.parseFrom(call("KclvmService.GetSchemaTypeMapping", args.toByteArray())); } + /** + * Formats KCL code according to the language standards. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * String sourceCode = "schema Person:\n" + "    name:   str\n" + "    age:    int\n" + "    check:\n"
+     *         + "        0 <   age <   120\n";
+     * FormatCode_Args args = FormatCode_Args.newBuilder().setSource(sourceCode).build();
+     * API apiInstance = new API();
+     * FormatCode_Result result = apiInstance.formatCode(args);
+     * String expectedFormattedCode = "schema Person:\n" + "    name: str\n" + "    age: int\n\n" + "    check:\n"
+     *         + "        0 < age < 120\n\n";
+     * }
+     * 
+ * + * @param args + * the arguments specifying the code to be formatted. + * + * @return the formatted code. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public FormatCode_Result formatCode(FormatCode_Args args) throws Exception { return FormatCode_Result.parseFrom(call("KclvmService.FormatCode", args.toByteArray())); } + /** + * Formats a specified path containing KCL files. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * FormatPath_Args args = FormatPath_Args.newBuilder().setPath("test.k").build();
+     * API apiInstance = new API();
+     * FormatPath_Result result = apiInstance.formatPath(args);
+     * Assert.assertTrue(result.getChangedPathsList().isEmpty());
+     * }
+     * 
+ * + * @param args + * the arguments specifying the path to be formatted. + * + * @return the paths of files that were changed during the format process. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public FormatPath_Result formatPath(FormatPath_Args args) throws Exception { return FormatPath_Result.parseFrom(call("KclvmService.FormatPath", args.toByteArray())); } + /** + * Lints a specified path containing KCL files. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * LintPath_Args args = LintPath_Args.newBuilder().addPaths("test.k").build();
+     * API apiInstance = new API();
+     * LintPath_Result result = apiInstance.lintPath(args);
+     * boolean foundWarning = result.getResultsList().stream()
+     *         .anyMatch(warning -> warning.contains("Module 'math' imported but unused"));
+     * }
+     * 
+ * + * @param args + * the arguments specifying the path to be linted. + * + * @return the lint results including warnings and errors. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public LintPath_Result lintPath(LintPath_Args args) throws Exception { return LintPath_Result.parseFrom(call("KclvmService.LintPath", args.toByteArray())); } + /** + * Validates KCL code using given schema and data strings. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * String code = "schema Person:\n" + "    name: str\n" + "    age: int\n" + "    check:\n"
+     *         + "        0 < age < 120\n";
+     * String data = "{\"name\": \"Alice\", \"age\": 10}";
+     * ValidateCode_Args args = ValidateCode_Args.newBuilder().setCode(code).setData(data).setFormat("json").build();
+     * API apiInstance = new API();
+     * ValidateCode_Result result = apiInstance.validateCode(args);
+     * }
+     * 
+ * + * @param args + * the arguments specifying the code, schema, and data for validation. + * + * @return the validation result. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public ValidateCode_Result validateCode(ValidateCode_Args args) throws Exception { return ValidateCode_Result.parseFrom(call("KclvmService.ValidateCode", args.toByteArray())); } + /** + * Loads setting files and returns the result. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * API api = new API();
+     * LoadSettingsFiles_Args args = LoadSettingsFiles_Args.newBuilder().addFiles("kcl.yaml")
+     *         .build();
+     * LoadSettingsFiles_Result result = api.loadSettingsFiles(args);
+     * }
+     * 
+ * + * @param args + * the arguments specifying the settings to be loaded. + * + * @return the loaded settings result. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public LoadSettingsFiles_Result loadSettingsFiles(LoadSettingsFiles_Args args) throws Exception { return LoadSettingsFiles_Result.parseFrom(call("KclvmService.LoadSettingsFiles", args.toByteArray())); } + /** + * Renames symbols in the given KCL files. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * Rename_Args args = Rename_Args.newBuilder().setPackageRoot("./src/test_data/rename").setSymbolPath("a")
+     *         .addFilePaths("./src/test_data/rename/main.k").setNewName("a2").build();
+     * API apiInstance = new API();
+     * Rename_Result result = apiInstance.rename(args);
+     * }
+     * 
+ * + * @param args + * the arguments specifying the symbols to be renamed. + * + * @return the result of the rename operation. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public Rename_Result rename(Rename_Args args) throws Exception { return Rename_Result.parseFrom(call("KclvmService.Rename", args.toByteArray())); } + /** + * Renames symbols in the given KCL code and returns the modified code. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * API api = new API();
+     * RenameCode_Args args = RenameCode_Args.newBuilder().setPackageRoot("/mock/path").setSymbolPath("a")
+     *         .putSourceCodes("/mock/path/main.k", "a = 1\nb = a").setNewName("a2").build();
+     * RenameCode_Result result = api.renameCode(args);
+     * }
+     * 
+ * + * @param args + * the arguments specifying the symbols to be renamed in the code. + * + * @return the result of the rename operation including the modified code. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public RenameCode_Result renameCode(RenameCode_Args args) throws Exception { return RenameCode_Result.parseFrom(call("KclvmService.RenameCode", args.toByteArray())); } + /** + * Tests KCL packages with the given test arguments. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * API apiInstance = new API();
+     * Test_Args args = Test_Args.newBuilder().addPkgList("./src/test_data/testing/...").build();
+     * Test_Result result = apiInstance.test(args);
+     * }
+     * 
+ * + * @param args + * the arguments specifying the test details. + * + * @return the test result including the test logs and errors. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public Test_Result test(Test_Args args) throws Exception { return Test_Result.parseFrom(call("KclvmService.Test", args.toByteArray())); } + /** + * Updates dependencies defined in the kcl.mod file. + * + *

+ * Example usage: + * + *

+     * {@code
+     * import com.kcl.api.*;
+     *
+     * API api = new API();
+     * UpdateDependencies_Result result = api.updateDependencies(
+     *         UpdateDependencies_Args.newBuilder().setManifestPath("/path/to/module").build());
+     * }
+     * 
+ * + * @param args + * the arguments specifying the dependencies to be updated. + * + * @return the result of updating dependencies. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ @Override public UpdateDependencies_Result updateDependencies(UpdateDependencies_Args args) throws Exception { return UpdateDependencies_Result.parseFrom(call("KclvmService.UpdateDependencies", args.toByteArray())); } + /** + * Gets the version information of the KCL service. + * + * @param args + * the arguments specifying the version request. + * + * @return the version result including version number and other details. + * + * @throws Exception + * if an error occurs during the remote procedure call. + */ + @Override + public GetVersion_Result getVersion(GetVersion_Args args) throws Exception { + return GetVersion_Result.parseFrom(call("KclvmService.GetVersion", args.toByteArray())); + } + private byte[] call(String name, byte[] args) throws Exception { byte[] result = callNative(name.getBytes(), args); if (result != null && startsWith(result, ERROR_PREFIX)) { diff --git a/java/src/main/java/com/kcl/api/Service.java b/java/src/main/java/com/kcl/api/Service.java index ef968ae..698d538 100644 --- a/java/src/main/java/com/kcl/api/Service.java +++ b/java/src/main/java/com/kcl/api/Service.java @@ -24,6 +24,9 @@ public interface Service { // List all the variables in the KCL file ListVariables_Result listVariables(ListVariables_Args args) throws Exception; + // List all the option functions in the KCL file + ListOptions_Result listOptions(ParseProgram_Args args) throws Exception; + // Service for getting the full schema type list GetSchemaTypeMapping_Result getSchemaTypeMapping(GetSchemaTypeMapping_Args args) throws Exception; @@ -53,4 +56,7 @@ public interface Service { // Service for the dependency updating UpdateDependencies_Result updateDependencies(UpdateDependencies_Args args) throws Exception; + + // Service for the KCL service version information. + GetVersion_Result getVersion(GetVersion_Args args) throws Exception; } diff --git a/java/src/test/java/com/kcl/ExecProgramTest.java b/java/src/test/java/com/kcl/ExecProgramTest.java new file mode 100644 index 0000000..44e6abe --- /dev/null +++ b/java/src/test/java/com/kcl/ExecProgramTest.java @@ -0,0 +1,22 @@ +package com.kcl; + +import com.kcl.api.API; +import com.kcl.api.Spec.ExecProgram_Args; +import com.kcl.api.Spec.ExecProgram_Result; + +import org.junit.Assert; +import org.junit.Test; + +public class ExecProgramTest { + + private static final String TEST_FILE = "./src/test_data/schema.k"; + + @Test + public void testGetSchemaTypeApi() throws Exception { + ExecProgram_Args args = ExecProgram_Args.newBuilder().addKFilenameList(TEST_FILE).build(); + + API apiInstance = new API(); + ExecProgram_Result result = apiInstance.execProgram(args); + Assert.assertEquals(result.getYamlResult(), "app:\n" + " replicas: 2"); + } +} diff --git a/java/src/test/java/com/kcl/FormatTest.java b/java/src/test/java/com/kcl/FormatTest.java new file mode 100644 index 0000000..6757eed --- /dev/null +++ b/java/src/test/java/com/kcl/FormatTest.java @@ -0,0 +1,35 @@ +package com.kcl; + +import com.kcl.api.API; +import com.kcl.api.Spec.FormatCode_Args; +import com.kcl.api.Spec.FormatCode_Result; +import com.kcl.api.Spec.FormatPath_Args; +import com.kcl.api.Spec.FormatPath_Result; +import org.junit.Assert; +import org.junit.Test; + +public class FormatTest { + @Test + public void testFormatPathApi() throws Exception { + final String TEST_PATH = "./src/test_data/format_path/test.k"; + FormatPath_Args args = FormatPath_Args.newBuilder().setPath(TEST_PATH).build(); + API apiInstance = new API(); + apiInstance.formatPath(args); + } + + @Test + public void testFormatCodeApi() throws Exception { + String sourceCode = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n" + + " 0 < age < 120\n"; + + FormatCode_Args args = FormatCode_Args.newBuilder().setSource(sourceCode).build(); + + API apiInstance = new API(); + FormatCode_Result result = apiInstance.formatCode(args); + + String expectedFormattedCode = "schema Person:\n" + " name: str\n" + " age: int\n\n" + " check:\n" + + " 0 < age < 120\n\n"; + + Assert.assertEquals(expectedFormattedCode, result.getFormatted().toStringUtf8()); + } +} diff --git a/java/src/test/java/com/kcl/GetSchemaTypeTest.java b/java/src/test/java/com/kcl/GetSchemaTypeTest.java new file mode 100644 index 0000000..36fc31c --- /dev/null +++ b/java/src/test/java/com/kcl/GetSchemaTypeTest.java @@ -0,0 +1,29 @@ +package com.kcl; + +import com.kcl.api.API; +import com.kcl.api.Spec.ExecProgram_Args; +import com.kcl.api.Spec.GetSchemaTypeMapping_Args; +import com.kcl.api.Spec.GetSchemaTypeMapping_Result; +import com.kcl.api.Spec.KclType; + +import org.junit.Assert; +import org.junit.Test; + +public class GetSchemaTypeTest { + + private static final String TEST_FILE = "./src/test_data/schema.k"; + + @Test + public void testGetSchemaTypeApi() throws Exception { + ExecProgram_Args execArgs = ExecProgram_Args.newBuilder().addKFilenameList(TEST_FILE).build(); + + GetSchemaTypeMapping_Args args = GetSchemaTypeMapping_Args.newBuilder().setExecArgs(execArgs).build(); + + API apiInstance = new API(); + GetSchemaTypeMapping_Result result = apiInstance.getSchemaTypeMapping(args); + + KclType appSchemaType = result.getSchemaTypeMappingMap().get("app"); + String replicasType = appSchemaType.getPropertiesOrThrow("replicas").getType(); + Assert.assertEquals("int", replicasType); + } +} diff --git a/java/src/test/java/com/kcl/GetVersionTest.java b/java/src/test/java/com/kcl/GetVersionTest.java new file mode 100644 index 0000000..548b035 --- /dev/null +++ b/java/src/test/java/com/kcl/GetVersionTest.java @@ -0,0 +1,20 @@ +package com.kcl; + +import org.junit.Assert; +import org.junit.Test; + +import com.kcl.api.API; +import com.kcl.api.Spec.GetVersion_Args; +import com.kcl.api.Spec.GetVersion_Result; + +public class GetVersionTest { + @Test + public void testGetVersion() throws Exception { + API api = new API(); + GetVersion_Args version_args = GetVersion_Args.newBuilder().build(); + GetVersion_Result result = api.getVersion(version_args); + String versionInfo = result.getVersionInfo(); + Assert.assertTrue(versionInfo, versionInfo.contains("Version")); + Assert.assertTrue(versionInfo, versionInfo.contains("GitCommit")); + } +} diff --git a/java/src/test/java/com/kcl/LintTest.java b/java/src/test/java/com/kcl/LintTest.java new file mode 100644 index 0000000..f5abc20 --- /dev/null +++ b/java/src/test/java/com/kcl/LintTest.java @@ -0,0 +1,22 @@ +package com.kcl; + +import com.kcl.api.API; +import com.kcl.api.Spec.LintPath_Args; +import com.kcl.api.Spec.LintPath_Result; +import org.junit.Assert; +import org.junit.Test; + +public class LintTest { + @Test + public void testLintPathApi() throws Exception { + final String TEST_PATH = "./src/test_data/lint_path/test-lint.k"; + + LintPath_Args args = LintPath_Args.newBuilder().addPaths(TEST_PATH).build(); + + API apiInstance = new API(); + LintPath_Result result = apiInstance.lintPath(args); + boolean foundWarning = result.getResultsList().stream() + .anyMatch(warning -> warning.contains("Module 'math' imported but unused")); + Assert.assertTrue("Expected warning not found", foundWarning); + } +} \ No newline at end of file diff --git a/java/src/test/java/com/kcl/ListOptionsTest.java b/java/src/test/java/com/kcl/ListOptionsTest.java new file mode 100644 index 0000000..0f326bb --- /dev/null +++ b/java/src/test/java/com/kcl/ListOptionsTest.java @@ -0,0 +1,21 @@ +package com.kcl; + +import org.junit.Assert; +import org.junit.Test; + +import com.kcl.api.API; +import com.kcl.api.Spec.ParseProgram_Args; +import com.kcl.api.Spec.ListOptions_Result; + +public class ListOptionsTest { + @Test + public void testParseProgram() throws Exception { + ParseProgram_Args args = ParseProgram_Args.newBuilder().addPaths("./src/test_data/option/main.k").build(); + + API apiInstance = new API(); + ListOptions_Result result = apiInstance.listOptions(args); + Assert.assertEquals(result.getOptions(0).getName(), "key1"); + Assert.assertEquals(result.getOptions(1).getName(), "key2"); + Assert.assertEquals(result.getOptions(2).getName(), "metadata-key"); + } +} diff --git a/java/src/test/java/com/kcl/LoadSettingsFileTest.java b/java/src/test/java/com/kcl/LoadSettingsFileTest.java new file mode 100644 index 0000000..7a8eefc --- /dev/null +++ b/java/src/test/java/com/kcl/LoadSettingsFileTest.java @@ -0,0 +1,22 @@ +package com.kcl; + +import org.junit.Assert; +import org.junit.Test; + +import com.kcl.api.API; +import com.kcl.api.Spec.LoadSettingsFiles_Args; +import com.kcl.api.Spec.LoadSettingsFiles_Result; + +public class LoadSettingsFileTest { + @Test + public void testLoadSettingsFile() throws Exception { + API api = new API(); + LoadSettingsFiles_Args args = LoadSettingsFiles_Args.newBuilder().addFiles("./src/test_data/settings/kcl.yaml") + .build(); + LoadSettingsFiles_Result result = api.loadSettingsFiles(args); + Assert.assertEquals(result.getKclCliConfigs().getFilesCount(), 0); + Assert.assertEquals(result.getKclCliConfigs().getStrictRangeCheck(), true); + Assert.assertEquals(result.getKclOptions(0).getKey(), "key"); + Assert.assertEquals(result.getKclOptions(0).getValue(), "\"value\""); + } +} diff --git a/java/src/test/java/com/kcl/ParseTest.java b/java/src/test/java/com/kcl/ParseTest.java new file mode 100644 index 0000000..4d13679 --- /dev/null +++ b/java/src/test/java/com/kcl/ParseTest.java @@ -0,0 +1,20 @@ +package com.kcl; + +import org.junit.Assert; +import org.junit.Test; + +import com.kcl.api.API; +import com.kcl.api.Spec.ParseFile_Args; +import com.kcl.api.Spec.ParseFile_Result; + +public class ParseTest { + @Test + public void testParseFile() throws Exception { + ParseFile_Args args = ParseFile_Args.newBuilder().setPath("./src/test_data/parse/main.k").build(); + + API apiInstance = new API(); + ParseFile_Result result = apiInstance.parseFile(args); + Assert.assertNotNull(result.getAstJson()); + Assert.assertEquals(result.getDepsCount(), 2); + } +} diff --git a/java/src/test/java/com/kcl/RenameTest.java b/java/src/test/java/com/kcl/RenameTest.java new file mode 100644 index 0000000..0958afe --- /dev/null +++ b/java/src/test/java/com/kcl/RenameTest.java @@ -0,0 +1,40 @@ +package com.kcl; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.Assert; +import org.junit.Test; + +import com.kcl.api.API; +import com.kcl.api.Spec.RenameCode_Args; +import com.kcl.api.Spec.RenameCode_Result; +import com.kcl.api.Spec.Rename_Args; +import com.kcl.api.Spec.Rename_Result; + +public class RenameTest { + @Test + public void testRenameCode() throws Exception { + API api = new API(); + RenameCode_Args args = RenameCode_Args.newBuilder().setPackageRoot("/mock/path").setSymbolPath("a") + .putSourceCodes("/mock/path/main.k", "a = 1\nb = a").setNewName("a2").build(); + RenameCode_Result result = api.renameCode(args); + Assert.assertEquals(result.getChangedCodesOrThrow("/mock/path/main.k"), "a2 = 1\nb = a2"); + } + + @Test + public void testRename() throws Exception { + String backupContent = new String(Files.readAllBytes(Paths.get("./src/test_data/rename/main.bak")), + StandardCharsets.UTF_8); + Files.write(Paths.get("./src/test_data/rename/main.k"), backupContent.getBytes(StandardCharsets.UTF_8)); + + Rename_Args args = Rename_Args.newBuilder().setPackageRoot("./src/test_data/rename").setSymbolPath("a") + .addFilePaths("./src/test_data/rename/main.k").setNewName("a2").build(); + + API apiInstance = new API(); + Rename_Result result = apiInstance.rename(args); + + Assert.assertTrue(result.getChangedFiles(0), result.getChangedFiles(0).contains("main.k")); + } +} diff --git a/java/src/test/java/com/kcl/TestingTest.java b/java/src/test/java/com/kcl/TestingTest.java new file mode 100644 index 0000000..90861bb --- /dev/null +++ b/java/src/test/java/com/kcl/TestingTest.java @@ -0,0 +1,18 @@ +package com.kcl; + +import com.kcl.api.API; +import com.kcl.api.Spec.Test_Args; +import com.kcl.api.Spec.Test_Result; + +import org.junit.Assert; +import org.junit.Test; + +public class TestingTest { + @Test + public void testLintPathApi() throws Exception { + API apiInstance = new API(); + Test_Args args = Test_Args.newBuilder().addPkgList("./src/test_data/testing/...").build(); + Test_Result result = apiInstance.test(args); + Assert.assertEquals(result.getInfoCount(), 2); + } +} diff --git a/java/src/test/java/com/kcl/UpdateDependenciesTest.java b/java/src/test/java/com/kcl/UpdateDependenciesTest.java index cca925d..38f81c4 100644 --- a/java/src/test/java/com/kcl/UpdateDependenciesTest.java +++ b/java/src/test/java/com/kcl/UpdateDependenciesTest.java @@ -1,6 +1,8 @@ package com.kcl; import com.kcl.api.API; +import com.kcl.api.Spec.ExecProgram_Args; +import com.kcl.api.Spec.ExecProgram_Result; import com.kcl.api.Spec.UpdateDependencies_Args; import com.kcl.api.Spec.UpdateDependencies_Result; @@ -17,4 +19,20 @@ public void testUpdateDependencies() throws Exception { UpdateDependencies_Args.newBuilder().setManifestPath("./src/test_data/update_dependencies").build()); Assert.assertEquals(result.getExternalPkgsCount(), 2); } + + @Test + public void testExecProgramWithExternalDependencies() throws Exception { + // API instance + API api = new API(); + + UpdateDependencies_Result result = api.updateDependencies( + UpdateDependencies_Args.newBuilder().setManifestPath("./src/test_data/update_dependencies").build()); + Assert.assertEquals(result.getExternalPkgsCount(), 2); + + ExecProgram_Args execArgs = ExecProgram_Args.newBuilder().addAllExternalPkgs(result.getExternalPkgsList()) + .addKFilenameList("./src/test_data/update_dependencies/main.k").build(); + + ExecProgram_Result execResult = api.execProgram(execArgs); + Assert.assertEquals(execResult.getYamlResult(), "a: Hello World!"); + } } diff --git a/java/src/test/java/com/kcl/ValidateTest.java b/java/src/test/java/com/kcl/ValidateTest.java new file mode 100644 index 0000000..ec5a0ac --- /dev/null +++ b/java/src/test/java/com/kcl/ValidateTest.java @@ -0,0 +1,25 @@ +package com.kcl; + +import org.junit.Assert; +import org.junit.Test; + +import com.kcl.api.API; +import com.kcl.api.Spec.ValidateCode_Args; +import com.kcl.api.Spec.ValidateCode_Result; + +public class ValidateTest { + @Test + public void testValidateCodeApi() throws Exception { + String code = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n" + + " 0 < age < 120\n"; + + String data = "{\"name\": \"Alice\", \"age\": 10}"; + + ValidateCode_Args args = ValidateCode_Args.newBuilder().setCode(code).setData(data).setFormat("json").build(); + API apiInstance = new API(); + ValidateCode_Result result = apiInstance.validateCode(args); + + Assert.assertTrue(result.getSuccess()); + Assert.assertEquals("", result.getErrMessage()); + } +} diff --git a/java/src/test_data/format_path/test.k b/java/src/test_data/format_path/test.k new file mode 100644 index 0000000..1337a53 --- /dev/null +++ b/java/src/test_data/format_path/test.k @@ -0,0 +1 @@ +a = 1 diff --git a/java/src/test_data/get_schema_ty/aaa/kcl.mod b/java/src/test_data/get_schema_ty/aaa/kcl.mod new file mode 100644 index 0000000..1e29d9e --- /dev/null +++ b/java/src/test_data/get_schema_ty/aaa/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "aaa" +edition = "0.0.1" +version = "0.0.1" + diff --git a/java/src/test_data/get_schema_ty/aaa/main.k b/java/src/test_data/get_schema_ty/aaa/main.k new file mode 100644 index 0000000..bba48ed --- /dev/null +++ b/java/src/test_data/get_schema_ty/aaa/main.k @@ -0,0 +1,10 @@ +import bbb as b +import ccc as c + +a = b.B { + name: "b instance in a" +} + +a_c = c.C { + name: "c instance in a" +} \ No newline at end of file diff --git a/java/src/test_data/get_schema_ty/bbb/kcl.mod b/java/src/test_data/get_schema_ty/bbb/kcl.mod new file mode 100644 index 0000000..e9ea10a --- /dev/null +++ b/java/src/test_data/get_schema_ty/bbb/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "bbb" +edition = "0.0.1" +version = "0.0.1" + diff --git a/java/src/test_data/get_schema_ty/bbb/main.k b/java/src/test_data/get_schema_ty/bbb/main.k new file mode 100644 index 0000000..21dad4f --- /dev/null +++ b/java/src/test_data/get_schema_ty/bbb/main.k @@ -0,0 +1,2 @@ +schema B: + name: str \ No newline at end of file diff --git a/java/src/test_data/get_schema_ty/ccc/kcl.mod b/java/src/test_data/get_schema_ty/ccc/kcl.mod new file mode 100644 index 0000000..9a762a4 --- /dev/null +++ b/java/src/test_data/get_schema_ty/ccc/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "ccc" +edition = "0.0.1" +version = "0.0.1" + diff --git a/java/src/test_data/get_schema_ty/ccc/main.k b/java/src/test_data/get_schema_ty/ccc/main.k new file mode 100644 index 0000000..463ff96 --- /dev/null +++ b/java/src/test_data/get_schema_ty/ccc/main.k @@ -0,0 +1,2 @@ +schema C: + name: str \ No newline at end of file diff --git a/java/src/test_data/lint_path/test-lint.k b/java/src/test_data/lint_path/test-lint.k new file mode 100644 index 0000000..c0b471b --- /dev/null +++ b/java/src/test_data/lint_path/test-lint.k @@ -0,0 +1,3 @@ +import math + +a = 1 diff --git a/java/src/test_data/option/main.k b/java/src/test_data/option/main.k new file mode 100644 index 0000000..1f6edf4 --- /dev/null +++ b/java/src/test_data/option/main.k @@ -0,0 +1,5 @@ +a = option("key1") +b = option("key2", required=True) +c = { + metadata.key = option("metadata-key") +} diff --git a/java/src/test_data/parse/kcl.mod b/java/src/test_data/parse/kcl.mod new file mode 100644 index 0000000..e69de29 diff --git a/java/src/test_data/parse/main.k b/java/src/test_data/parse/main.k new file mode 100644 index 0000000..1f0792f --- /dev/null +++ b/java/src/test_data/parse/main.k @@ -0,0 +1,5 @@ +import pkg1 +import pkg2 + +a1 = pkg1.a +a2 = pkg2.a diff --git a/java/src/test_data/parse/pkg1/pkg.k b/java/src/test_data/parse/pkg1/pkg.k new file mode 100644 index 0000000..1337a53 --- /dev/null +++ b/java/src/test_data/parse/pkg1/pkg.k @@ -0,0 +1 @@ +a = 1 diff --git a/java/src/test_data/parse/pkg2/pkg.k b/java/src/test_data/parse/pkg2/pkg.k new file mode 100644 index 0000000..1337a53 --- /dev/null +++ b/java/src/test_data/parse/pkg2/pkg.k @@ -0,0 +1 @@ +a = 1 diff --git a/java/src/test_data/rename/main.bak b/java/src/test_data/rename/main.bak new file mode 100644 index 0000000..3a0fa58 --- /dev/null +++ b/java/src/test_data/rename/main.bak @@ -0,0 +1,2 @@ +a = 1 +b = a \ No newline at end of file diff --git a/java/src/test_data/rename/main.k b/java/src/test_data/rename/main.k new file mode 100644 index 0000000..d0f8ad1 --- /dev/null +++ b/java/src/test_data/rename/main.k @@ -0,0 +1,2 @@ +a2 = 1 +b = a2 \ No newline at end of file diff --git a/java/src/test_data/settings/kcl.yaml b/java/src/test_data/settings/kcl.yaml new file mode 100644 index 0000000..7b7300d --- /dev/null +++ b/java/src/test_data/settings/kcl.yaml @@ -0,0 +1,5 @@ +kcl_cli_configs: + strict_range_check: true +kcl_options: + - key: key + value: value diff --git a/java/src/test_data/testing/module/kcl.mod b/java/src/test_data/testing/module/kcl.mod new file mode 100644 index 0000000..35d888a --- /dev/null +++ b/java/src/test_data/testing/module/kcl.mod @@ -0,0 +1,3 @@ +[package] +name = "test_data" + diff --git a/java/src/test_data/testing/module/pkg/func.k b/java/src/test_data/testing/module/pkg/func.k new file mode 100644 index 0000000..26df9cf --- /dev/null +++ b/java/src/test_data/testing/module/pkg/func.k @@ -0,0 +1,3 @@ +func = lambda x { + x +} diff --git a/java/src/test_data/testing/module/pkg/func_test.k b/java/src/test_data/testing/module/pkg/func_test.k new file mode 100644 index 0000000..b022950 --- /dev/null +++ b/java/src/test_data/testing/module/pkg/func_test.k @@ -0,0 +1,7 @@ +test_func_0 = lambda { + assert func("a") == "a" +} + +test_func_1 = lambda { + assert func("b") == "b" +} diff --git a/java/src/test_data/update_dependencies/main.k b/java/src/test_data/update_dependencies/main.k new file mode 100644 index 0000000..b371e74 --- /dev/null +++ b/java/src/test_data/update_dependencies/main.k @@ -0,0 +1,4 @@ +import helloworld +import flask + +a = helloworld.The_first_kcl_program diff --git a/java/src/test_data/variables/main.k b/java/src/test_data/variables/main.k new file mode 100644 index 0000000..23979c5 --- /dev/null +++ b/java/src/test_data/variables/main.k @@ -0,0 +1,3 @@ +a = 1 +b = [1, 2, 3] +c = {"a": "b"} \ No newline at end of file diff --git a/python/README.md b/python/README.md index 23f87ee..dc24e82 100644 --- a/python/README.md +++ b/python/README.md @@ -236,7 +236,7 @@ assert list(result.symbols.values())[0].ty.schema_name == "AppConfig"

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