Skip to content

Commit

Permalink
Fix JSON serialization for auto_override/toggle components
Browse files Browse the repository at this point in the history
Differential Revision: D64616203

Pull Request resolved: #1407
  • Loading branch information
SanderMertens committed Oct 18, 2024
1 parent ba608b2 commit eb07b1a
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 5 deletions.
17 changes: 16 additions & 1 deletion distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -42273,6 +42273,18 @@ void flecs_json_id_member(
ecs_id_t id,
bool fullpath)
{
ecs_id_t flags = id & ECS_ID_FLAGS_MASK;

if (flags & ECS_AUTO_OVERRIDE) {
ecs_strbuf_appendlit(buf, "auto_override|");
id &= ~ECS_AUTO_OVERRIDE;
}

if (flags & ECS_TOGGLE) {
ecs_strbuf_appendlit(buf, "toggle|");
id &= ~ECS_TOGGLE;
}

if (fullpath) {
flecs_json_id_member_fullpath(buf, world, id);
return;
Expand Down Expand Up @@ -43822,8 +43834,11 @@ bool flecs_json_serialize_table_tags(
}

flecs_json_next(buf);
flecs_json_path_or_label(buf, world, id,

ecs_strbuf_appendlit(buf, "\"");
flecs_json_id_member(buf, world, id,
desc ? desc->serialize_full_paths : true);
ecs_strbuf_appendlit(buf, "\"");

tag_count ++;
}
Expand Down
12 changes: 12 additions & 0 deletions src/addons/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,18 @@ void flecs_json_id_member(
ecs_id_t id,
bool fullpath)
{
ecs_id_t flags = id & ECS_ID_FLAGS_MASK;

if (flags & ECS_AUTO_OVERRIDE) {
ecs_strbuf_appendlit(buf, "auto_override|");
id &= ~ECS_AUTO_OVERRIDE;
}

if (flags & ECS_TOGGLE) {
ecs_strbuf_appendlit(buf, "toggle|");
id &= ~ECS_TOGGLE;
}

if (fullpath) {
flecs_json_id_member_fullpath(buf, world, id);
return;
Expand Down
5 changes: 4 additions & 1 deletion src/addons/json/serialize_iter_result_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ bool flecs_json_serialize_table_tags(
}

flecs_json_next(buf);
flecs_json_path_or_label(buf, world, id,

ecs_strbuf_appendlit(buf, "\"");
flecs_json_id_member(buf, world, id,
desc ? desc->serialize_full_paths : true);
ecs_strbuf_appendlit(buf, "\"");

tag_count ++;
}
Expand Down
11 changes: 9 additions & 2 deletions test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"vector_of_struct_with_strings",
"vector_of_arrays_of_strings",
"vector_of_opaque"
]
]
}, {
"id": "StructTypes",
"testcases": [
Expand Down Expand Up @@ -720,7 +720,14 @@
"serialize_sparse_mixed",
"serialize_sparse_inherited",
"serialize_sparse_inherited_pair",
"serialize_sparse_inherited_mixed"
"serialize_sparse_inherited_mixed",
"serialize_auto_override_w_inherited",
"serialize_auto_override",
"serialize_auto_override_pair",
"serialize_auto_override_fullpath",
"serialize_auto_override_pair_fullpath",
"serialize_toggle",
"serialize_toggle_pair"
]
}, {
"id": "SerializeIterToJson",
Expand Down
165 changes: 165 additions & 0 deletions test/meta/src/SerializeEntityToJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1778,3 +1778,168 @@ void SerializeEntityToJson_serialize_sparse_inherited_mixed(void) {

ecs_fini(world);
}

void SerializeEntityToJson_serialize_auto_override_w_inherited(void) {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);

ecs_entity_t e = ecs_entity(world, { .name = "e"});
ecs_auto_override(world, e, Position);

ecs_entity_to_json_desc_t desc = {
.serialize_inherited = true
};

char *json = ecs_entity_to_json(world, e, &desc);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|Position\"]}");
ecs_os_free(json);

ecs_fini(world);
}

void SerializeEntityToJson_serialize_auto_override(void) {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);

ecs_entity_t e = ecs_entity(world, { .name = "e"});
ecs_auto_override(world, e, Position);

char *json = ecs_entity_to_json(world, e, NULL);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|Position\"]}");
ecs_os_free(json);

ecs_fini(world);
}

void SerializeEntityToJson_serialize_auto_override_pair(void) {
ecs_world_t *world = ecs_init();

ECS_TAG(world, Rel);
ECS_TAG(world, Tgt);

ecs_entity_t e = ecs_entity(world, { .name = "e"});
ecs_auto_override_pair(world, e, Rel, Tgt);

char *json = ecs_entity_to_json(world, e, NULL);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|(Rel,Tgt)\"]}");
ecs_os_free(json);

ecs_fini(world);
}

void SerializeEntityToJson_serialize_auto_override_fullpath(void) {
ecs_world_t *world = ecs_init();

ECS_TAG(world, Rel);
ECS_TAG(world, Tgt);

ecs_entity_t tag = ecs_entity(world, { .name = "tags.foo"});

ecs_entity_t e = ecs_entity(world, { .name = "e"});
ecs_auto_override_id(world, e, tag);

{
ecs_entity_to_json_desc_t desc = {
.serialize_full_paths = true
};

char *json = ecs_entity_to_json(world, e, &desc);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|tags.foo\"]}");
ecs_os_free(json);
}

{
ecs_entity_to_json_desc_t desc = {
.serialize_full_paths = false
};

char *json = ecs_entity_to_json(world, e, &desc);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|foo\"]}");
ecs_os_free(json);
}

ecs_fini(world);
}

void SerializeEntityToJson_serialize_auto_override_pair_fullpath(void) {
ecs_world_t *world = ecs_init();

ECS_TAG(world, Rel);
ECS_TAG(world, Tgt);

ecs_entity_t rel = ecs_entity(world, { .name = "tags.rel"});
ecs_entity_t tgt = ecs_entity(world, { .name = "tags.tgt"});

ecs_entity_t e = ecs_entity(world, { .name = "e"});
ecs_auto_override_pair(world, e, rel, tgt);

{
ecs_entity_to_json_desc_t desc = {
.serialize_full_paths = true
};

char *json = ecs_entity_to_json(world, e, &desc);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|(tags.rel,tags.tgt)\"]}");
ecs_os_free(json);
}

{
ecs_entity_to_json_desc_t desc = {
.serialize_full_paths = false
};

char *json = ecs_entity_to_json(world, e, &desc);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"auto_override|(rel,tgt)\"]}");
ecs_os_free(json);
}

ecs_fini(world);
}

void SerializeEntityToJson_serialize_toggle(void) {
ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, Position);

ecs_add_id(world, ecs_id(Position), EcsCanToggle);

ecs_entity_t e = ecs_entity(world, { .name = "e"});
ecs_add(world, e, Position);
ecs_enable_component(world, e, Position, false);

char *json = ecs_entity_to_json(world, e, NULL);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"toggle|Position\"], \"components\":{\"Position\":null}}");
ecs_os_free(json);

ecs_fini(world);
}

void SerializeEntityToJson_serialize_toggle_pair(void) {
ecs_world_t *world = ecs_init();

ECS_TAG(world, Rel);
ECS_TAG(world, Tgt);

ecs_add_id(world, Rel, EcsCanToggle);

ecs_entity_t e = ecs_entity(world, { .name = "e"});
ecs_add_pair(world, e, Rel, Tgt);
ecs_enable_pair(world, e, Rel, Tgt, false);

char *json = ecs_entity_to_json(world, e, NULL);
test_assert(json != NULL);
test_json(json, "{\"name\":\"e\", \"tags\":[\"toggle|(Rel,Tgt)\"],\"pairs\":{\"Rel\":[\"Tgt\"]}}");
ecs_os_free(json);

ecs_fini(world);
}
37 changes: 36 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,13 @@ void SerializeEntityToJson_serialize_sparse_mixed(void);
void SerializeEntityToJson_serialize_sparse_inherited(void);
void SerializeEntityToJson_serialize_sparse_inherited_pair(void);
void SerializeEntityToJson_serialize_sparse_inherited_mixed(void);
void SerializeEntityToJson_serialize_auto_override_w_inherited(void);
void SerializeEntityToJson_serialize_auto_override(void);
void SerializeEntityToJson_serialize_auto_override_pair(void);
void SerializeEntityToJson_serialize_auto_override_fullpath(void);
void SerializeEntityToJson_serialize_auto_override_pair_fullpath(void);
void SerializeEntityToJson_serialize_toggle(void);
void SerializeEntityToJson_serialize_toggle_pair(void);

// Testsuite 'SerializeIterToJson'
void SerializeIterToJson_serialize_1_comps_empty(void);
Expand Down Expand Up @@ -3646,6 +3653,34 @@ bake_test_case SerializeEntityToJson_testcases[] = {
{
"serialize_sparse_inherited_mixed",
SerializeEntityToJson_serialize_sparse_inherited_mixed
},
{
"serialize_auto_override_w_inherited",
SerializeEntityToJson_serialize_auto_override_w_inherited
},
{
"serialize_auto_override",
SerializeEntityToJson_serialize_auto_override
},
{
"serialize_auto_override_pair",
SerializeEntityToJson_serialize_auto_override_pair
},
{
"serialize_auto_override_fullpath",
SerializeEntityToJson_serialize_auto_override_fullpath
},
{
"serialize_auto_override_pair_fullpath",
SerializeEntityToJson_serialize_auto_override_pair_fullpath
},
{
"serialize_toggle",
SerializeEntityToJson_serialize_toggle
},
{
"serialize_toggle_pair",
SerializeEntityToJson_serialize_toggle_pair
}
};

Expand Down Expand Up @@ -4895,7 +4930,7 @@ static bake_test_suite suites[] = {
"SerializeEntityToJson",
NULL,
NULL,
62,
69,
SerializeEntityToJson_testcases
},
{
Expand Down

0 comments on commit eb07b1a

Please sign in to comment.