Skip to content

Commit

Permalink
feat: add ten_value_is_equal for object & array
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Dec 3, 2024
1 parent 8a82a8d commit 83b8d96
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions core/src/ten_utils/value/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,14 +864,52 @@ bool ten_value_is_equal(ten_value_t *self, ten_value_t *target) {
ten_list_size(&target->content.object)) {
return false;
}
// TODO(Wei): Implement the equality check for object-type value.

ten_list_foreach (&self->content.object, iter_self) {
ten_value_kv_t *kv_self = ten_ptr_listnode_get(iter_self.node);
TEN_ASSERT(kv_self && ten_value_kv_check_integrity(kv_self),
"Invalid argument.");

// Peek the corresponding value in the target object.
ten_value_t *kv_target = ten_value_object_peek(
target, ten_string_get_raw_str(&kv_self->key));
if (!kv_target) {
// Key does not exist in target
return false;
}

// Recursively check equality of the values.
if (!ten_value_is_equal(kv_self->value, kv_target)) {
return false;
}
}
break;
case TEN_TYPE_ARRAY:
if (ten_list_size(&self->content.array) !=
ten_list_size(&target->content.array)) {
return false;
}
// TODO(Wei): Implement the equality check for array-type value.

{
ten_list_iterator_t iter_self = ten_list_begin(&self->content.array);
ten_list_iterator_t iter_target =
ten_list_begin(&target->content.array);

while (!ten_list_iterator_is_end(iter_self) &&
!ten_list_iterator_is_end(iter_target)) {
ten_value_t *value_self =
(ten_value_t *)ten_list_iterator_to_listnode(iter_self);
ten_value_t *value_target =
(ten_value_t *)ten_list_iterator_to_listnode(iter_target);

if (!ten_value_is_equal(value_self, value_target)) {
return false;
}

iter_self = ten_list_iterator_next(iter_self);
iter_target = ten_list_iterator_next(iter_target);
}
}
break;
case TEN_TYPE_INT8:
return self->content.int8 == target->content.int8;
Expand Down

0 comments on commit 83b8d96

Please sign in to comment.