Skip to content

Commit

Permalink
Fix JSON serialization for auto_override/toggle components
Browse files Browse the repository at this point in the history
Summary: This diff fixes an issue where components with the `auto_override` and/or `toggle` flags weren't correctly serialized to JSON.

Differential Revision: D64616203
  • Loading branch information
SanderMertens authored and facebook-github-bot committed Oct 18, 2024
1 parent ba608b2 commit 072be46
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 69 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
78 changes: 39 additions & 39 deletions include/flecs/bake_config.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef FLECS_BAKE_CONFIG_H
#define FLECS_BAKE_CONFIG_H

/* Headers of public dependencies */
/* No dependencies */

/* Convenience macro for exporting symbols */
#ifndef flecs_STATIC
#if defined(flecs_EXPORTS) && (defined(_MSC_VER) || defined(__MINGW32__))
#define FLECS_API __declspec(dllexport)
#elif defined(flecs_EXPORTS)
#define FLECS_API __attribute__((__visibility__("default")))
#elif defined(_MSC_VER)
#define FLECS_API __declspec(dllimport)
#else
#define FLECS_API
#endif
#else
#define FLECS_API
#endif

#endif

/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef FLECS_BAKE_CONFIG_H
#define FLECS_BAKE_CONFIG_H

/* Headers of public dependencies */
/* No dependencies */

/* Convenience macro for exporting symbols */
#ifndef flecs_STATIC
#if defined(flecs_EXPORTS) && (defined(_MSC_VER) || defined(__MINGW32__))
#define FLECS_API __declspec(dllexport)
#elif defined(flecs_EXPORTS)
#define FLECS_API __attribute__((__visibility__("default")))
#elif defined(_MSC_VER)
#define FLECS_API __declspec(dllimport)
#else
#define FLECS_API
#endif
#else
#define FLECS_API
#endif

#endif
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
50 changes: 25 additions & 25 deletions test/meta/include/meta/bake_config.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef META_BAKE_CONFIG_H
#define META_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>
#include <bake_test.h>

#endif

/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'
* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef META_BAKE_CONFIG_H
#define META_BAKE_CONFIG_H

/* Headers of public dependencies */
#include <flecs.h>
#include <bake_test.h>

#endif
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);
}
Loading

0 comments on commit 072be46

Please sign in to comment.