Skip to content

Commit

Permalink
feat!: skip _ten fields handling in to/from_json (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Nov 24, 2024
1 parent 3aeaecf commit 2b3ce4f
Show file tree
Hide file tree
Showing 201 changed files with 741 additions and 1,002 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=AudioFrameTest.FromJson"
"--gtest_filter=CmdConversionTest.CmdConversionPathArray1"
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"env": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ class cmd_start_graph_t : public cmd_t {
err != nullptr ? err->get_internal_representation() : nullptr);
}

bool set_nodes_and_connections_from_json(const char *json_str,
error_t *err = nullptr) {
return ten_cmd_start_graph_init_from_json_str(
bool set_graph_from_json(const char *json_str, error_t *err = nullptr) {
return ten_cmd_start_graph_set_graph_from_json_str(
c_msg, json_str,
err != nullptr ? err->get_internal_representation() : nullptr);
}

bool set_long_running_mode(bool long_running_mode, error_t *err = nullptr) {
return ten_cmd_start_graph_set_long_running_mode(
c_msg, long_running_mode,
err != nullptr ? err->get_internal_representation() : nullptr);
}

// @{
cmd_start_graph_t(cmd_start_graph_t &other) = delete;
cmd_start_graph_t(cmd_start_graph_t &&other) = delete;
Expand Down
5 changes: 4 additions & 1 deletion core/include/ten_runtime/msg/cmd/start_graph/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ TEN_RUNTIME_API bool ten_cmd_start_graph_set_predefined_graph_name(
ten_shared_ptr_t *self, const char *predefined_graph_name,
ten_error_t *err);

TEN_RUNTIME_API bool ten_cmd_start_graph_init_from_json_str(
TEN_RUNTIME_API bool ten_cmd_start_graph_set_long_running_mode(
ten_shared_ptr_t *self, bool long_running_mode, ten_error_t *err);

TEN_RUNTIME_API bool ten_cmd_start_graph_set_graph_from_json_str(
ten_shared_ptr_t *self, const char *json_str, ten_error_t *err);
26 changes: 0 additions & 26 deletions core/include_internal/ten_runtime/msg/msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ TEN_RUNTIME_PRIVATE_API const char *ten_raw_msg_get_type_string(
TEN_RUNTIME_PRIVATE_API void ten_msg_clear_and_set_dest_from_extension_info(
ten_shared_ptr_t *self, ten_extension_info_t *extension_info);

TEN_RUNTIME_PRIVATE_API void ten_msg_clear_and_set_dest_to_extension(
ten_shared_ptr_t *self, ten_extension_t *extension);

TEN_RUNTIME_PRIVATE_API void ten_msg_correct_dest(ten_shared_ptr_t *msg,
ten_engine_t *engine);

Expand Down Expand Up @@ -264,10 +261,6 @@ TEN_RUNTIME_PRIVATE_API bool ten_msg_validate_schema(
ten_shared_ptr_t *self, ten_schema_store_t *schema_store, bool is_msg_out,
ten_error_t *err);

TEN_RUNTIME_PRIVATE_API bool ten_raw_msg_get_one_field_from_json(
ten_msg_t *self, ten_msg_field_process_data_t *field, void *user_data,
ten_error_t *err);

TEN_RUNTIME_PRIVATE_API bool
ten_raw_msg_get_one_field_from_json_include_internal_field(
ten_msg_t *self, ten_msg_field_process_data_t *field, void *user_data,
Expand Down Expand Up @@ -303,25 +296,6 @@ TEN_RUNTIME_API ten_shared_ptr_t *ten_msg_create_from_msg_type(

TEN_RUNTIME_API void ten_raw_msg_destroy(ten_msg_t *self);

TEN_RUNTIME_API const char *ten_msg_json_get_string_field_in_ten(
ten_json_t *json, const char *field);

TEN_RUNTIME_PRIVATE_API bool ten_msg_json_get_is_ten_field_exist(
ten_json_t *json, const char *field);

TEN_RUNTIME_API int64_t
ten_msg_json_get_integer_field_in_ten(ten_json_t *json, const char *field);

TEN_RUNTIME_PRIVATE_API TEN_MSG_TYPE
ten_msg_json_get_msg_type(ten_json_t *json);

TEN_RUNTIME_API const char *ten_raw_msg_get_name(ten_msg_t *self);

TEN_RUNTIME_API bool ten_raw_msg_set_name_with_size(ten_msg_t *self,
const char *msg_name,
size_t msg_name_len,
ten_error_t *err);

TEN_RUNTIME_PRIVATE_API bool ten_raw_msg_set_name(ten_msg_t *self,
const char *msg_name,
ten_error_t *err);
Expand Down
3 changes: 0 additions & 3 deletions core/include_internal/ten_runtime/msg/msg_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ typedef void (*ten_msg_engine_handler_func_t)(ten_engine_t *engine,
typedef ten_msg_t *(*ten_raw_msg_clone_func_t)(ten_msg_t *msg,
ten_list_t *excluded_field_ids);

typedef void (*ten_raw_msg_destroy_func_t)(ten_msg_t *msg);

typedef bool (*ten_raw_msg_loop_all_fields_func_t)(
ten_msg_t *msg, ten_raw_msg_process_one_field_func_t cb, void *user_data,
ten_error_t *err);
Expand All @@ -58,7 +56,6 @@ typedef struct ten_msg_info_t {

bool create_in_path;
ten_msg_engine_handler_func_t engine_handler;

ten_raw_msg_clone_func_t clone;
ten_raw_msg_loop_all_fields_func_t loop_all_fields;
ten_raw_msg_validate_schema_func_t validate_schema;
Expand Down
8 changes: 4 additions & 4 deletions core/include_internal/ten_utils/schema/bindings/rust/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
// knows the structure's layout.
typedef struct ten_schema_t ten_schema_t;

TEN_UTILS_API ten_schema_t *ten_schema_create_from_json_string_proxy(
const char *json_string, const char **err_msg);
TEN_UTILS_API ten_schema_t *ten_schema_create_from_json_str_proxy(
const char *json_str, const char **err_msg);

TEN_UTILS_API void ten_schema_destroy_proxy(const ten_schema_t *self);

TEN_UTILS_API bool ten_schema_adjust_and_validate_json_string_proxy(
const ten_schema_t *self, const char *json_string, const char **err_msg);
TEN_UTILS_API bool ten_schema_adjust_and_validate_json_str_proxy(
const ten_schema_t *self, const char *json_str, const char **err_msg);

TEN_UTILS_API bool ten_schema_is_compatible_proxy(const ten_schema_t *self,
const ten_schema_t *target,
Expand Down
8 changes: 4 additions & 4 deletions core/include_internal/ten_utils/schema/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ TEN_UTILS_PRIVATE_API void ten_schema_init(ten_schema_t *self);

TEN_UTILS_PRIVATE_API void ten_schema_deinit(ten_schema_t *self);

TEN_UTILS_PRIVATE_API ten_schema_t *ten_schema_create_from_json_string(
const char *json_string, const char **err_msg);
TEN_UTILS_PRIVATE_API ten_schema_t *ten_schema_create_from_json_str(
const char *json_str, const char **err_msg);

TEN_UTILS_PRIVATE_API bool ten_schema_adjust_and_validate_json_string(
ten_schema_t *self, const char *json_string, const char **err_msg);
TEN_UTILS_PRIVATE_API bool ten_schema_adjust_and_validate_json_str(
ten_schema_t *self, const char *json_str, const char **err_msg);

TEN_UTILS_API ten_schema_t *ten_schema_create_from_json(ten_json_t *json);

Expand Down
10 changes: 5 additions & 5 deletions core/src/ten_runtime/metadata/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include "include_internal/ten_rust/ten_rust.h"
#endif

static bool ten_metadata_load_from_json_string(ten_value_t *metadata,
const char *json_str,
ten_error_t *err) {
static bool ten_metadata_load_from_json_str(ten_value_t *metadata,
const char *json_str,
ten_error_t *err) {
TEN_ASSERT(metadata && ten_value_check_integrity(metadata) && json_str,
"Should not happen.");

Expand Down Expand Up @@ -57,7 +57,7 @@ static bool ten_metadata_load_from_json_file(ten_value_t *metadata,
return false;
}

bool ret = ten_metadata_load_from_json_string(metadata, buf, err);
bool ret = ten_metadata_load_from_json_str(metadata, buf, err);
if (!ret) {
TEN_LOGW(
"Try to load metadata from file '%s', but file content with wrong "
Expand All @@ -83,7 +83,7 @@ static bool ten_metadata_load_from_type_ane_value(ten_value_t *metadata,
break;

case TEN_METADATA_JSON_STR:
if (!ten_metadata_load_from_json_string(metadata, value, err)) {
if (!ten_metadata_load_from_json_str(metadata, value, err)) {
result = false;
goto done;
}
Expand Down
77 changes: 70 additions & 7 deletions core/src/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <time.h>

#include "include_internal/ten_runtime/app/app.h"
#include "include_internal/ten_runtime/common/constant_str.h"
#include "include_internal/ten_runtime/common/loc.h"
#include "include_internal/ten_runtime/extension/extension.h"
#include "include_internal/ten_runtime/extension/extension_addon_and_instance_name_pair.h"
Expand Down Expand Up @@ -81,20 +82,71 @@ ten_shared_ptr_t *ten_cmd_start_graph_create(void) {
ten_raw_cmd_start_graph_destroy);
}

static bool ten_raw_cmd_start_graph_as_msg_get_graph_from_json(
ten_msg_t *self, ten_msg_field_process_data_t *field, void *user_data,
ten_error_t *err) {
TEN_ASSERT(self && ten_raw_msg_check_integrity(self), "Should not happen.");
TEN_ASSERT(field, "Should not happen.");
TEN_ASSERT(
field->field_value && ten_value_check_integrity(field->field_value),
"Should not happen.");

if (ten_c_string_is_equal(field->field_name, TEN_STR_NODES) ||
ten_c_string_is_equal(field->field_name, TEN_STR_CONNECTIONS)) {
ten_json_t *json = (ten_json_t *)user_data;
TEN_ASSERT(json, "Should not happen.");

json = ten_json_object_peek(json, field->field_name);
if (!json) {
// Some fields are optional, and it is allowed for the corresponding
// JSON block to be absent during deserialization.
return true;
}

if (!ten_value_set_from_json(field->field_value, json)) {
// If the field value cannot be set from the JSON, it means that the
// JSON format is incorrect.
if (err) {
ten_error_set(err, TEN_ERRNO_INVALID_JSON,
"Invalid JSON format for field %s.", field->field_name);
}

return false;
}
}

// During JSON deserialization, the field value may be modified, so we set the
// value_is_changed_after_process flag.
field->value_is_changed_after_process = true;

return true;
}

bool ten_raw_cmd_start_graph_init_from_json(ten_cmd_start_graph_t *self,
ten_json_t *json,
ten_error_t *err) {
TEN_ASSERT(self && ten_raw_cmd_check_integrity((ten_cmd_t *)self),
"Should not happen.");
TEN_ASSERT(json && ten_json_check_integrity(json), "Should not happen.");

// =-=-=
return ten_raw_cmd_start_graph_loop_all_fields(
(ten_msg_t *)self,
ten_raw_msg_get_one_field_from_json_include_internal_field, json, err);
}

static bool ten_raw_cmd_start_graph_as_msg_init_from_json_str(
bool ten_raw_cmd_start_graph_set_graph_from_json(ten_cmd_start_graph_t *self,
ten_json_t *json,
ten_error_t *err) {
TEN_ASSERT(self && ten_raw_cmd_check_integrity((ten_cmd_t *)self),
"Should not happen.");
TEN_ASSERT(json && ten_json_check_integrity(json), "Should not happen.");

return ten_raw_cmd_start_graph_loop_all_fields(
(ten_msg_t *)self, ten_raw_cmd_start_graph_as_msg_get_graph_from_json,
json, err);
}

static bool ten_raw_cmd_start_graph_set_graph_from_json_str(
ten_msg_t *self, const char *json_str, ten_error_t *err) {
TEN_ASSERT(self && ten_raw_cmd_check_integrity((ten_cmd_t *)self),
"Invalid argument.");
Expand All @@ -105,21 +157,21 @@ static bool ten_raw_cmd_start_graph_as_msg_init_from_json_str(
return false;
}

bool rc = ten_raw_cmd_start_graph_init_from_json(
bool rc = ten_raw_cmd_start_graph_set_graph_from_json(
(ten_cmd_start_graph_t *)self, json, err);

ten_json_destroy(json);

return rc;
}

bool ten_cmd_start_graph_init_from_json_str(ten_shared_ptr_t *self,
const char *json_str,
ten_error_t *err) {
bool ten_cmd_start_graph_set_graph_from_json_str(ten_shared_ptr_t *self,
const char *json_str,
ten_error_t *err) {
TEN_ASSERT(self && ten_cmd_check_integrity(self), "Invalid argument.");
TEN_ASSERT(json_str, "Invalid argument.");

return ten_raw_cmd_start_graph_as_msg_init_from_json_str(
return ten_raw_cmd_start_graph_set_graph_from_json_str(
ten_msg_get_raw_msg(self), json_str, err);
}

Expand Down Expand Up @@ -385,6 +437,17 @@ bool ten_cmd_start_graph_set_predefined_graph_name(
predefined_graph_name);
}

bool ten_cmd_start_graph_set_long_running_mode(ten_shared_ptr_t *self,
bool long_running_mode,
ten_error_t *err) {
TEN_ASSERT(self && ten_cmd_base_check_integrity(self) &&
ten_msg_get_type(self) == TEN_MSG_TYPE_CMD_START_GRAPH,
"Should not happen.");

return ten_value_set_bool(&get_raw_cmd(self)->long_running_mode,
long_running_mode);
}

ten_string_t *ten_raw_cmd_start_graph_get_predefined_graph_name(
ten_cmd_start_graph_t *self) {
TEN_ASSERT(self && ten_raw_cmd_check_integrity((ten_cmd_t *)self) &&
Expand Down
Loading

0 comments on commit 2b3ce4f

Please sign in to comment.