Skip to content

Commit

Permalink
Serialize - handle read errors
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Nov 29, 2024
1 parent 219a2b6 commit a28250e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
28 changes: 13 additions & 15 deletions src/ECS/Serialize/ComponentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ private string ReadRaw (DataEntity dataEntity, Entity entity)
case JsonEvent.ValueNull:
break;
case JsonEvent.ObjectStart:
ev = ReadRawComponents();
if (ev != JsonEvent.ObjectEnd &&
ev != JsonEvent.ArrayEnd) {
error = ReadRawComponents(entity);
if (error != null) {
// could support also scalar types in the future: string, number or boolean
return $"'components' member must be object or array. was {ev}. id: {entity.Id}, component: '{parser.key}'";
return error;
}
break;
default:
return $"expect 'components' == object or null. id: {entity.Id}. was: {ev}";
return $"expect 'components' == object, array or null. id: {entity.Id}. was: {ev}";
}
return null;
}
Expand Down Expand Up @@ -298,7 +297,7 @@ private static Archetype FindArchetype(ArchetypeKey searchKey, EntityStoreBase s
return newArchetype;
}

private JsonEvent ReadRawComponents()
private string ReadRawComponents(Entity entity)
{
var ev = parser.NextEvent();
while (true) {
Expand All @@ -312,21 +311,18 @@ private JsonEvent ReadRawComponents()
}
components[componentCount++] = new RawComponent(RawComponentType.Object, rawKey, start, parser.Position);
ev = parser.NextEvent();
if (ev == JsonEvent.ObjectEnd) {
return JsonEvent.ObjectEnd;
}
break;
case JsonEvent.ObjectEnd:
return JsonEvent.ObjectEnd;
return null;
case JsonEvent.ArrayStart:
return ReadRawRelations();
return ReadRawRelations(entity);
default:
return ev;
return $"'components' member must be object or array. was {ev}. id: {entity.Id}, component: '{parser.key}'";
}
}
}

private JsonEvent ReadRawRelations()
private string ReadRawRelations(Entity entity)
{
var rawKey = ToRawKey(parser.key);
var startRelation = relationCount;
Expand All @@ -346,11 +342,13 @@ private JsonEvent ReadRawRelations()
ArrayUtils.Resize(ref components, 2 * componentCount);
}
components[componentCount++] = new RawComponent(RawComponentType.Array, rawKey, startRelation, relationCount);
return JsonEvent.ArrayEnd;
return null;
}
break;
case JsonEvent.ArrayEnd:
return null;
default:
return ev;
return $"'components' member expect array of objects. was {ev}. id: {entity.Id}, component: '{parser.key}'";
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/ECS/Serialize/Test_ComponentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static void Test_ComponentReader_read_invalid_components()
var node = new DataEntity { pid = 10, components = new JsonValue("123") };
var entity = converter.DataEntityToEntity(node, store, out var error);
NotNull(entity);
AreEqual("expect 'components' == object or null. id: 10. was: ValueNumber", error);
AreEqual("expect 'components' == object, array or null. id: 10. was: ValueNumber", error);

node = new DataEntity { pid = 10, components = new JsonValue("invalid") };
entity = converter.DataEntityToEntity(node, store, out error);
Expand Down
63 changes: 63 additions & 0 deletions src/Tests/ECS/Serialize/Test_SerializeRelations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class Test_SerializeRelations
@"[{
""id"": 1,
""components"": {
""name"": {""value"":""test""},
""IntRelation"": [{""value"":101}]
}
},{
Expand All @@ -24,6 +25,30 @@ public static class Test_SerializeRelations
}
}]";

private const string JsonEmptyRelations =
@"[{
""id"": 1,
""components"": {
""IntRelation"": []
}
}]";

private const string JsonError =
@"[{
""id"": 1,
""components"": {
""IntRelation"": [1]
}
}]";

private const string JsonInvalid =
@"[{
""id"": 1,
""components"": {
""IntRelation"": [x]
}
}]";


#region read relations
[Test]
Expand All @@ -47,6 +72,43 @@ public static void Test_SerializeRelations_read()
AreEqual(201, relations2[0].value);
AreEqual(202, relations2[1].value);
}

[Test]
public static void Test_SerializeRelations_read_empty_relations()
{
var store = new EntityStore();
var serializer = new EntitySerializer();
var stream = Test_Serializer.StringAsStream(JsonEmptyRelations);

var result = serializer.ReadIntoStore(store, stream);
IsNull(result.error);

var entity1 = store.GetEntityById(1);
var relations1 = entity1.GetRelations<IntRelation>();
AreEqual(0, relations1.Length);
}

[Test]
public static void Test_SerializeRelations_read_error()
{
var store = new EntityStore();
var serializer = new EntitySerializer();
var stream = Test_Serializer.StringAsStream(JsonError);

var result = serializer.ReadIntoStore(store, stream);
AreEqual("'components' member expect array of objects. was ValueNumber. id: 1, component: 'IntRelation' path: '[0]' at position: 70", result.error);
}

[Test]
public static void Test_SerializeRelations_read_invalid_JSON()
{
var store = new EntityStore();
var serializer = new EntitySerializer();
var stream = Test_Serializer.StringAsStream(JsonInvalid);

var result = serializer.ReadIntoStore(store, stream);
AreEqual("unexpected character while reading value. Found: x path: '[0].components.IntRelation[0]' at position: 61", result.error);
}
#endregion


Expand All @@ -57,6 +119,7 @@ public static void Test_SerializeRelations_write()
var store = new EntityStore();
Entity entity1 = store.CreateEntity(1);
entity1.AddRelation(new IntRelation { value = 101 });
entity1.AddComponent(new EntityName("test"));

Entity entity2 = store.CreateEntity(2);
entity2.AddRelation(new IntRelation { value = 201 });
Expand Down

0 comments on commit a28250e

Please sign in to comment.