Skip to content

Commit

Permalink
refactor: refine codes (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn authored Nov 1, 2024
1 parent 3cdb2e0 commit 99acfd8
Show file tree
Hide file tree
Showing 5 changed files with 454 additions and 235 deletions.
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

0 comments on commit 99acfd8

Please sign in to comment.