|
| 1 | +--- |
| 2 | +sidebar_position: 8 |
| 3 | +--- |
| 4 | + |
| 5 | +# C API |
| 6 | + |
| 7 | +The [C API](https://github.com/kcl-lang/lib/tree/main/c) is in the development stage and contributions are welcome. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | ++ Make |
| 12 | ++ C Compiler |
| 13 | ++ Cargo |
| 14 | + |
| 15 | +## API Reference |
| 16 | + |
| 17 | +### exec_program |
| 18 | + |
| 19 | +Execute KCL file with arguments and return the JSON/YAML result. |
| 20 | + |
| 21 | +<details><summary>Example</summary> |
| 22 | +<p> |
| 23 | + |
| 24 | +```c |
| 25 | +#include <kcl_lib.h> |
| 26 | + |
| 27 | +int exec_file(const char* file_str) { |
| 28 | + uint8_t buffer[BUFFER_SIZE]; |
| 29 | + uint8_t result_buffer[BUFFER_SIZE]; |
| 30 | + size_t message_length; |
| 31 | + bool status; |
| 32 | + struct Buffer file = { |
| 33 | + .buffer = file_str, |
| 34 | + .len = strlen(file_str), |
| 35 | + }; |
| 36 | + struct Buffer* files[] = { &file }; |
| 37 | + struct RepeatedString strs = { .repeated = &files[0], .index = 0, .max_size = 1 }; |
| 38 | + ExecProgram_Args args = ExecProgram_Args_init_zero; |
| 39 | + args.k_filename_list.funcs.encode = encode_str_list; |
| 40 | + args.k_filename_list.arg = &strs; |
| 41 | + |
| 42 | + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
| 43 | + status = pb_encode(&stream, ExecProgram_Args_fields, &args); |
| 44 | + message_length = stream.bytes_written; |
| 45 | + |
| 46 | + if (!status) { |
| 47 | + printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); |
| 48 | + return 1; |
| 49 | + } |
| 50 | + |
| 51 | + const char* api_str = "KclvmService.ExecProgram"; |
| 52 | + size_t result_length = call_native((const uint8_t*)api_str, strlen(api_str), buffer, message_length, result_buffer); |
| 53 | + if (check_error_prefix(result_buffer)) { |
| 54 | + printf("%s", result_buffer); |
| 55 | + return 1; |
| 56 | + } |
| 57 | + pb_istream_t istream = pb_istream_from_buffer(result_buffer, result_length); |
| 58 | + |
| 59 | + ExecProgram_Result result = ExecProgram_Result_init_default; |
| 60 | + |
| 61 | + uint8_t yaml_value_buffer[BUFFER_SIZE] = { 0 }; |
| 62 | + result.yaml_result.arg = yaml_value_buffer; |
| 63 | + result.yaml_result.funcs.decode = decode_string; |
| 64 | + |
| 65 | + uint8_t json_value_buffer[BUFFER_SIZE] = { 0 }; |
| 66 | + result.json_result.arg = json_value_buffer; |
| 67 | + result.json_result.funcs.decode = decode_string; |
| 68 | + |
| 69 | + uint8_t err_value_buffer[BUFFER_SIZE] = { 0 }; |
| 70 | + result.err_message.arg = err_value_buffer; |
| 71 | + result.err_message.funcs.decode = decode_string; |
| 72 | + |
| 73 | + uint8_t log_value_buffer[BUFFER_SIZE] = { 0 }; |
| 74 | + result.log_message.arg = log_value_buffer; |
| 75 | + result.log_message.funcs.decode = decode_string; |
| 76 | + |
| 77 | + status = pb_decode(&istream, ExecProgram_Result_fields, &result); |
| 78 | + |
| 79 | + if (!status) { |
| 80 | + printf("Decoding failed: %s\n", PB_GET_ERROR(&istream)); |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + if (result.yaml_result.arg) { |
| 85 | + printf("%s\n", (char*)result.yaml_result.arg); |
| 86 | + } |
| 87 | + |
| 88 | + return 0; |
| 89 | +} |
| 90 | + |
| 91 | +int main() |
| 92 | +{ |
| 93 | + exec_file("./test_data/schema.k"); |
| 94 | + return 0; |
| 95 | +} |
| 96 | +``` |
| 97 | +
|
| 98 | +</p> |
| 99 | +</details> |
| 100 | +
|
| 101 | +### validate_code |
| 102 | +
|
| 103 | +Validate code using schema and JSON/YAML data strings. |
| 104 | +
|
| 105 | +<details><summary>Example</summary> |
| 106 | +<p> |
| 107 | +
|
| 108 | +```rust |
| 109 | +#include <kcl_lib.h> |
| 110 | +
|
| 111 | +int validate(const char* code_str, const char* data_str) |
| 112 | +{ |
| 113 | + uint8_t buffer[BUFFER_SIZE]; |
| 114 | + uint8_t result_buffer[BUFFER_SIZE]; |
| 115 | + size_t message_length; |
| 116 | + bool status; |
| 117 | +
|
| 118 | + ValidateCode_Args validate_args = ValidateCode_Args_init_zero; |
| 119 | + validate_args.code.funcs.encode = encode_string; |
| 120 | + validate_args.code.arg = (void*)code_str; |
| 121 | + validate_args.data.funcs.encode = encode_string; |
| 122 | + validate_args.data.arg = (void*)data_str; |
| 123 | +
|
| 124 | + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); |
| 125 | + status = pb_encode(&stream, ValidateCode_Args_fields, &validate_args); |
| 126 | + message_length = stream.bytes_written; |
| 127 | +
|
| 128 | + if (!status) { |
| 129 | + printf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); |
| 130 | + return 1; |
| 131 | + } |
| 132 | +
|
| 133 | + const char* api_str = "KclvmService.ValidateCode"; |
| 134 | + size_t result_length = call_native((const uint8_t*)api_str, strlen(api_str), buffer, message_length, result_buffer); |
| 135 | + pb_istream_t istream = pb_istream_from_buffer(result_buffer, result_length); |
| 136 | + ValidateCode_Result result = ValidateCode_Result_init_default; |
| 137 | +
|
| 138 | + result.err_message.funcs.decode = decode_string; |
| 139 | + uint8_t value_buffer[BUFFER_SIZE] = { 0 }; |
| 140 | + result.err_message.arg = value_buffer; |
| 141 | +
|
| 142 | + status = pb_decode(&istream, ValidateCode_Result_fields, &result); |
| 143 | +
|
| 144 | + if (!status) { |
| 145 | + printf("Decoding failed: %s\n", PB_GET_ERROR(&istream)); |
| 146 | + return 1; |
| 147 | + } |
| 148 | +
|
| 149 | + printf("Validate Status: %d\n", result.success); |
| 150 | + if (result.err_message.arg) { |
| 151 | + printf("Validate Error Message: %s\n", (char*)result.err_message.arg); |
| 152 | + } |
| 153 | + return 0; |
| 154 | +} |
| 155 | +
|
| 156 | +int main() |
| 157 | +{ |
| 158 | + const char* code_str = "schema Person:\n" |
| 159 | + " name: str\n" |
| 160 | + " age: int\n" |
| 161 | + " check:\n" |
| 162 | + " 0 < age < 120\n"; |
| 163 | + const char* data_str = "{\"name\": \"Alice\", \"age\": 10}"; |
| 164 | + const char* error_data_str = "{\"name\": \"Alice\", \"age\": 1110}"; |
| 165 | + validate(code_str, data_str); |
| 166 | + validate(code_str, error_data_str); |
| 167 | + return 0; |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +</p> |
| 172 | +</details> |
0 commit comments