Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refine codes #222

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions core/include_internal/ten_utils/value/value_can_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,35 @@ TEN_UTILS_PRIVATE_API bool can_convert_int64_to_float64(int64_t value);
TEN_UTILS_PRIVATE_API bool can_convert_uint32_to_float64(uint32_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint64_to_float64(uint64_t value);

TEN_UTILS_PRIVATE_API bool can_convert_int16_to_int8(int16_t value);

TEN_UTILS_PRIVATE_API bool can_convert_int32_to_int8(int32_t value);

TEN_UTILS_PRIVATE_API bool can_convert_int64_to_int8(int64_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint8_to_int8(uint8_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint16_to_int8(uint16_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint32_to_int8(uint32_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint64_to_int8(uint64_t value);

TEN_UTILS_PRIVATE_API bool can_convert_int32_to_int16(int32_t value);

TEN_UTILS_PRIVATE_API bool can_convert_int64_to_int16(int64_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint16_to_int16(uint16_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint32_to_int16(uint32_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint64_to_int16(uint64_t value);

TEN_UTILS_PRIVATE_API bool can_convert_int64_to_int32(int64_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint32_to_int32(uint32_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint64_to_int32(uint64_t value);

TEN_UTILS_PRIVATE_API bool can_convert_uint64_to_int64(uint64_t value);
4 changes: 4 additions & 0 deletions core/src/ten_utils/schema/keywords/keyword_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ static bool ten_schema_keyword_type_validate_value(
return true;
}

// Automatically perform TEN-supported value conversion based on the type. This
// conversion is generally safe, meaning the value will not change as a result.
static bool ten_schema_keyword_type_adjust_value(
ten_schema_keyword_t *self_, ten_value_t *value,
ten_schema_error_t *schema_err) {
Expand All @@ -92,6 +94,8 @@ static bool ten_schema_keyword_type_adjust_value(
TEN_TYPE schema_type = self->type;
TEN_TYPE value_type = ten_value_get_type(value);
if (value_type == schema_type) {
// The types are consistent, so there's no need to adjust the value; this is
// a normal situation.
return true;
}

Expand Down
112 changes: 112 additions & 0 deletions core/src/ten_utils/value/value_can_convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,115 @@ bool can_convert_uint64_to_float64(uint64_t value) {
uint64_t converted_back = (uint64_t)double_value;
return value == converted_back;
}

bool can_convert_int16_to_int8(int16_t value) {
if (value < INT8_MIN || value > INT8_MAX) {
return false;
}
return true;
}

bool can_convert_int32_to_int8(int32_t value) {
if (value < INT8_MIN || value > INT8_MAX) {
return false;
}
return true;
}

bool can_convert_int64_to_int8(int64_t value) {
if (value < INT8_MIN || value > INT8_MAX) {
return false;
}
return true;
}

bool can_convert_uint8_to_int8(uint8_t value) {
if (value > INT8_MAX) {
return false;
}
return true;
}

bool can_convert_uint16_to_int8(uint16_t value) {
if (value > INT8_MAX) {
return false;
}
return true;
}

bool can_convert_uint32_to_int8(uint32_t value) {
if (value > INT8_MAX) {
return false;
}
return true;
}

bool can_convert_uint64_to_int8(uint64_t value) {
if (value > INT8_MAX) {
return false;
}
return true;
}

bool can_convert_int32_to_int16(int32_t value) {
if (value < INT16_MIN || value > INT16_MAX) {
return false;
}
return true;
}

bool can_convert_int64_to_int16(int64_t value) {
if (value < INT16_MIN || value > INT16_MAX) {
return false;
}
return true;
}

bool can_convert_uint16_to_int16(uint16_t value) {
if (value > INT16_MAX) {
return false;
}
return true;
}

bool can_convert_uint32_to_int16(uint32_t value) {
if (value > INT16_MAX) {
return false;
}
return true;
}

bool can_convert_uint64_to_int16(uint64_t value) {
if (value > INT16_MAX) {
return false;
}
return true;
}

bool can_convert_int64_to_int32(int64_t value) {
if (value < INT32_MIN || value > INT32_MAX) {
return false;
}
return true;
}

bool can_convert_uint32_to_int32(uint32_t value) {
if (value > INT32_MAX) {
return false;
}
return true;
}

bool can_convert_uint64_to_int32(uint64_t value) {
if (value > INT32_MAX) {
return false;
}
return true;
}

bool can_convert_uint64_to_int64(uint64_t value) {
if (value > INT64_MAX) {
return false;
}
return true;
}
Loading
Loading