Skip to content

Commit

Permalink
Merge pull request #419 from Peefy/more-cpp-api-docs
Browse files Browse the repository at this point in the history
docs: add more cpp API documents
  • Loading branch information
Peefy authored Jul 17, 2024
2 parents cf42494 + 9cbe6cf commit d8f15a8
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/reference/xlang-api/c-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Validate code using schema and JSON/YAML data strings.
<details><summary>Example</summary>
<p>
```rust
```c
#include <kcl_lib.h>
int validate(const char* code_str, const char* data_str)
Expand Down
135 changes: 134 additions & 1 deletion docs/reference/xlang-api/cpp-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Validate code using schema and JSON/YAML data strings.
<details><summary>Example</summary>
<p>

```rust
```cpp
#include "kcl_lib.hpp"
#include <iostream>

Expand Down Expand Up @@ -113,3 +113,136 @@ int main()
</p>
</details>
### override_file
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.
<details><summary>Example</summary>
<p>
The content of `main.k` is
```c++
a = 1
b = {
"a": 1
"b": 2
}
```

C++ Code

```cpp
#include "kcl_lib.hpp"
#include <iostream>

int main()
{
auto args = kcl_lib::OverrideFileArgs {
.file = rust::String("main.k"),
.specs = rust::Vec({ rust::String("b.a=2") }),
};
auto result = kcl_lib::override_file(args);
std::cout << result.result << std::endl;
std::cout << result.parse_errors.size() << std::endl;
return 0;
}
```

</p>
</details>

### update_dependencies

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

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

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" }
```

C++ Code

```c++
#include "kcl_lib.hpp"
#include <iostream>

int main()
{
auto args = kcl_lib::UpdateDependenciesArgs {
.manifest_path = rust::String("../test_data/update_dependencies"),
};
auto result = kcl_lib::update_dependencies(args);
std::cout << result.external_pkgs[0].pkg_name.c_str() << std::endl;
std::cout << result.external_pkgs[1].pkg_name.c_str() << std::endl;
return 0;
}
```

</p>
</details>

Call `exec_program` with external dependencies

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

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

```c++
import helloworld
import flask

a = helloworld.The_first_kcl_program
```

C++ Code

```c++
#include "kcl_lib.hpp"
#include <iostream>

int main()
{
auto args = kcl_lib::UpdateDependenciesArgs {
.manifest_path = rust::String("../test_data/update_dependencies"),
};
auto result = kcl_lib::update_dependencies(args);
auto exec_args = kcl_lib::ExecProgramArgs {
.k_filename_list = rust::Vec({ rust::String("../test_data/update_dependencies/main.k") }),
.external_pkgs = result.external_pkgs,
};
auto exec_result = kcl_lib::exec_program(exec_args);
std::cout << exec_result.yaml_result.c_str() << std::endl;
return 0;
}
```

</p>
</details>
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Validate code using schema and JSON/YAML data strings.
<details><summary>Example</summary>
<p>
```rust
```c
#include <kcl_lib.h>
int validate(const char* code_str, const char* data_str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Validate code using schema and JSON/YAML data strings.
<details><summary>Example</summary>
<p>

```rust
```cpp
#include "kcl_lib.hpp"
#include <iostream>

Expand Down Expand Up @@ -113,3 +113,136 @@ int main()
</p>
</details>
### override_file
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.
<details><summary>Example</summary>
<p>
The content of `main.k` is
```c++
a = 1
b = {
"a": 1
"b": 2
}
```

C++ Code

```cpp
#include "kcl_lib.hpp"
#include <iostream>

int main()
{
auto args = kcl_lib::OverrideFileArgs {
.file = rust::String("main.k"),
.specs = rust::Vec({ rust::String("b.a=2") }),
};
auto result = kcl_lib::override_file(args);
std::cout << result.result << std::endl;
std::cout << result.parse_errors.size() << std::endl;
return 0;
}
```

</p>
</details>

### update_dependencies

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

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

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" }
```

C++ Code

```c++
#include "kcl_lib.hpp"
#include <iostream>

int main()
{
auto args = kcl_lib::UpdateDependenciesArgs {
.manifest_path = rust::String("../test_data/update_dependencies"),
};
auto result = kcl_lib::update_dependencies(args);
std::cout << result.external_pkgs[0].pkg_name.c_str() << std::endl;
std::cout << result.external_pkgs[1].pkg_name.c_str() << std::endl;
return 0;
}
```

</p>
</details>

Call `exec_program` with external dependencies

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

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

```c++
import helloworld
import flask

a = helloworld.The_first_kcl_program
```

C++ Code

```c++
#include "kcl_lib.hpp"
#include <iostream>

int main()
{
auto args = kcl_lib::UpdateDependenciesArgs {
.manifest_path = rust::String("../test_data/update_dependencies"),
};
auto result = kcl_lib::update_dependencies(args);
auto exec_args = kcl_lib::ExecProgramArgs {
.k_filename_list = rust::Vec({ rust::String("../test_data/update_dependencies/main.k") }),
.external_pkgs = result.external_pkgs,
};
auto exec_result = kcl_lib::exec_program(exec_args);
std::cout << exec_result.yaml_result.c_str() << std::endl;
return 0;
}
```

</p>
</details>
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Validate code using schema and JSON/YAML data strings.
<details><summary>Example</summary>
<p>
```rust
```c
#include <kcl_lib.h>
int validate(const char* code_str, const char* data_str)
Expand Down
Loading

0 comments on commit d8f15a8

Please sign in to comment.