mirror of https://github.com/Hax-io/libpb
Added enum support
This commit is contained in:
parent
ccc2a5cf31
commit
4046f15230
|
@ -117,6 +117,8 @@ public class PocketBase
|
|||
alias structNames = FieldNameTuple!(RecordType);
|
||||
alias structValues = record.tupleof;
|
||||
|
||||
alias bool isBasic;
|
||||
|
||||
static foreach(cnt; 0..structTypes.length)
|
||||
{
|
||||
debug(dbg)
|
||||
|
@ -126,13 +128,42 @@ public class PocketBase
|
|||
// pragma(msg, structValues[cnt]);
|
||||
}
|
||||
|
||||
|
||||
static if(__traits(isSame, mixin(structTypes[cnt]), int))
|
||||
{
|
||||
builtJSON[structNames[cnt]] = structValues[cnt];
|
||||
}
|
||||
|
||||
debug(dbg)
|
||||
else static if(__traits(isSame, mixin(structTypes[cnt]), uint))
|
||||
{
|
||||
writeln(builtJSON.toPrettyString());
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return builtJSON;
|
||||
}
|
||||
|
@ -219,8 +250,11 @@ public class PocketBase
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public enum EnumType
|
||||
{
|
||||
DOG,
|
||||
CAT
|
||||
}
|
||||
|
||||
// Test serialization of a struct to JSON
|
||||
unittest
|
||||
|
@ -228,12 +262,15 @@ unittest
|
|||
import std.algorithm.searching : canFind;
|
||||
import std.string : cmp;
|
||||
|
||||
|
||||
|
||||
struct Person
|
||||
{
|
||||
public string firstname, lastname;
|
||||
public int age;
|
||||
public string[] list;
|
||||
public JSONValue extraJSON;
|
||||
public EnumType eType;
|
||||
}
|
||||
|
||||
Person p1;
|
||||
|
@ -242,6 +279,7 @@ unittest
|
|||
p1.age = 23;
|
||||
p1.list = ["1", "2", "3"];
|
||||
p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`);
|
||||
p1.eType = EnumType.CAT;
|
||||
|
||||
JSONValue serialized = PocketBase.serializeRecord(p1);
|
||||
|
||||
|
@ -249,6 +287,11 @@ unittest
|
|||
assert(canFind(keys, "firstname") && cmp(serialized["firstname"].str(), "Tristan") == 0);
|
||||
assert(canFind(keys, "lastname") && cmp(serialized["lastname"].str(), "Kildaire") == 0);
|
||||
assert(canFind(keys, "age") && serialized["age"].integer() == 23);
|
||||
|
||||
debug(dbg)
|
||||
{
|
||||
writeln(serialized.toPrettyString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue