Added enum support

This commit is contained in:
Tristan B. Velloza Kildaire 2022-12-29 17:13:56 +02:00
parent ccc2a5cf31
commit 4046f15230
1 changed files with 50 additions and 7 deletions

View File

@ -117,6 +117,8 @@ public class PocketBase
alias structNames = FieldNameTuple!(RecordType); alias structNames = FieldNameTuple!(RecordType);
alias structValues = record.tupleof; alias structValues = record.tupleof;
alias bool isBasic;
static foreach(cnt; 0..structTypes.length) static foreach(cnt; 0..structTypes.length)
{ {
debug(dbg) debug(dbg)
@ -126,13 +128,42 @@ public class PocketBase
// pragma(msg, structValues[cnt]); // pragma(msg, structValues[cnt]);
} }
builtJSON[structNames[cnt]] = structValues[cnt];
static if(__traits(isSame, mixin(structTypes[cnt]), int))
{
builtJSON[structNames[cnt]] = structValues[cnt];
}
else static if(__traits(isSame, mixin(structTypes[cnt]), uint))
{
builtJSON[structNames[cnt]] = structValues[cnt];
}
else static if(__traits(isSame, mixin(structTypes[cnt]), ulong))
{
builtJSON[structNames[cnt]] = structValues[cnt];
}
else static if(__traits(isSame, mixin(structTypes[cnt]), long))
{
builtJSON[structNames[cnt]] = structValues[cnt];
}
else static if(__traits(isSame, mixin(structTypes[cnt]), string))
{
builtJSON[structNames[cnt]] = structValues[cnt];
}
else static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue))
{
builtJSON[structNames[cnt]] = structValues[cnt];
}
else static if(__traits(isSame, mixin(structTypes[cnt]), bool))
{
builtJSON[structNames[cnt]] = structValues[cnt];
}
else
{
pragma(msg, "Yaa");
builtJSON[structNames[cnt]] = to!(string)(structValues[cnt]);
}
} }
debug(dbg)
{
writeln(builtJSON.toPrettyString());
}
return builtJSON; return builtJSON;
} }
@ -219,14 +250,19 @@ public class PocketBase
} }
public enum EnumType
{
DOG,
CAT
}
// Test serialization of a struct to JSON // Test serialization of a struct to JSON
unittest unittest
{ {
import std.algorithm.searching : canFind; import std.algorithm.searching : canFind;
import std.string : cmp; import std.string : cmp;
struct Person struct Person
{ {
@ -234,6 +270,7 @@ unittest
public int age; public int age;
public string[] list; public string[] list;
public JSONValue extraJSON; public JSONValue extraJSON;
public EnumType eType;
} }
Person p1; Person p1;
@ -242,6 +279,7 @@ unittest
p1.age = 23; p1.age = 23;
p1.list = ["1", "2", "3"]; p1.list = ["1", "2", "3"];
p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`); p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`);
p1.eType = EnumType.CAT;
JSONValue serialized = PocketBase.serializeRecord(p1); JSONValue serialized = PocketBase.serializeRecord(p1);
@ -249,6 +287,11 @@ unittest
assert(canFind(keys, "firstname") && cmp(serialized["firstname"].str(), "Tristan") == 0); assert(canFind(keys, "firstname") && cmp(serialized["firstname"].str(), "Tristan") == 0);
assert(canFind(keys, "lastname") && cmp(serialized["lastname"].str(), "Kildaire") == 0); assert(canFind(keys, "lastname") && cmp(serialized["lastname"].str(), "Kildaire") == 0);
assert(canFind(keys, "age") && serialized["age"].integer() == 23); assert(canFind(keys, "age") && serialized["age"].integer() == 23);
debug(dbg)
{
writeln(serialized.toPrettyString());
}
} }