mirror of https://github.com/Hax-io/libpb
- `fromJSON()` now throws `RemoteFieldMissing` exception if the input JSON does not have a field found in the input struct type `RecordType`
- Added a unittest to test the above
This commit is contained in:
parent
3d9b9a4976
commit
707a1b9373
|
@ -1,11 +1,28 @@
|
||||||
module libpb.deserialization;
|
module libpb.deserialization;
|
||||||
|
|
||||||
import std.json;
|
import std.json;
|
||||||
|
import libpb.exceptions : RemoteFieldMissing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* T
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* RecordType = type of the struct to construct
|
||||||
|
*/
|
||||||
mixin template T(RecordType)
|
mixin template T(RecordType)
|
||||||
{
|
{
|
||||||
import std.traits : FieldTypeTuple, FieldNameTuple;
|
import std.traits : FieldTypeTuple, FieldNameTuple;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deserializes the provided JSON into a struct of type RecordType
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* jsonIn = the JSON to deserialize
|
||||||
|
* Throws:
|
||||||
|
* RemoteFieldMissing = if the field names in the provided RecordType
|
||||||
|
* cannot be found within the prpvided JSONValue `jsonIn`.
|
||||||
|
* Returns: an instance of RecordType
|
||||||
|
*/
|
||||||
public RecordType fromJSON(JSONValue jsonIn)
|
public RecordType fromJSON(JSONValue jsonIn)
|
||||||
{
|
{
|
||||||
RecordType record;
|
RecordType record;
|
||||||
|
@ -24,83 +41,94 @@ mixin template T(RecordType)
|
||||||
// pragma(msg, structValues[cnt]);
|
// pragma(msg, structValues[cnt]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static if(__traits(isSame, mixin(structTypes[cnt]), byte))
|
pragma(msg, "Bruh type");
|
||||||
{
|
pragma(msg, structTypes[cnt]);
|
||||||
mixin("record."~structNames[cnt]) = cast(byte)jsonIn[structNames[cnt]].integer();
|
// pragma(msg, __traits(identifier, mixin(structTypes[cnt])));
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), ubyte))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(ubyte)jsonIn[structNames[cnt]].uinteger();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), short))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(short)jsonIn[structNames[cnt]].integer();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), ushort))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(ushort)jsonIn[structNames[cnt]].uinteger();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), int))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(int)jsonIn[structNames[cnt]].integer();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), uint))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(uint)jsonIn[structNames[cnt]].uinteger();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), ulong))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(ulong)jsonIn[structNames[cnt]].uinteger();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), long))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = cast(long)jsonIn[structNames[cnt]].integer();
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), string))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].str();
|
|
||||||
|
|
||||||
debug(dbg)
|
try
|
||||||
{
|
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue))
|
|
||||||
{
|
{
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]];
|
static if(__traits(isSame, mixin(structTypes[cnt]), byte))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(byte)jsonIn[structNames[cnt]].integer();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), ubyte))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(ubyte)jsonIn[structNames[cnt]].uinteger();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), short))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(short)jsonIn[structNames[cnt]].integer();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), ushort))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(ushort)jsonIn[structNames[cnt]].uinteger();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), int))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(int)jsonIn[structNames[cnt]].integer();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), uint))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(uint)jsonIn[structNames[cnt]].uinteger();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), ulong))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(ulong)jsonIn[structNames[cnt]].uinteger();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), long))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = cast(long)jsonIn[structNames[cnt]].integer();
|
||||||
|
}
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), string))
|
||||||
|
{
|
||||||
|
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].str();
|
||||||
|
|
||||||
debug(dbg)
|
debug(dbg)
|
||||||
{
|
{
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
pragma(msg,"record."~structNames[cnt]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
else static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue))
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), bool))
|
{
|
||||||
{
|
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]];
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
|
||||||
|
|
||||||
debug(dbg)
|
debug(dbg)
|
||||||
{
|
{
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
pragma(msg,"record."~structNames[cnt]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
else static if(__traits(isSame, mixin(structTypes[cnt]), bool))
|
||||||
//FIXME: Not sure how to get array support going, very new to meta programming
|
{
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), mixin(structTypes[cnt])[]))
|
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
|
||||||
|
|
||||||
debug(dbg)
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg,"record."~structNames[cnt]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//FIXME: Not sure how to get array support going, very new to meta programming
|
||||||
|
else static if(__traits(isSame, mixin(structTypes[cnt]), mixin(structTypes[cnt])[]))
|
||||||
{
|
{
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
||||||
|
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg,"record."~structNames[cnt]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// throw new
|
||||||
|
//TODO: Throw error
|
||||||
|
debug(dbg)
|
||||||
|
{
|
||||||
|
pragma(msg, "Unknown type for de-serialization");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
catch(JSONException e)
|
||||||
{
|
{
|
||||||
// throw new
|
throw new RemoteFieldMissing();
|
||||||
//TODO: Throw error
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg, "Unknown type for de-serialization");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,3 +175,39 @@ unittest
|
||||||
assert(person.obj["bruh"].integer() == 1);
|
assert(person.obj["bruh"].integer() == 1);
|
||||||
//TODO: list test case
|
//TODO: list test case
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.string : cmp;
|
||||||
|
import std.stdio : writeln;
|
||||||
|
|
||||||
|
struct Person
|
||||||
|
{
|
||||||
|
public string firstname, lastname;
|
||||||
|
public int age;
|
||||||
|
public bool isMale;
|
||||||
|
public JSONValue obj;
|
||||||
|
public int[] list;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONValue json = parseJSON(`{
|
||||||
|
"firstname" : "Tristan",
|
||||||
|
"lastname": "Kildaire",
|
||||||
|
"age": 23,
|
||||||
|
"obj" : {"bruh":1},
|
||||||
|
"list": [1,2,3]
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
|
||||||
|
mixin T!(Person);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Person person = fromJSON(json);
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
catch(RemoteFieldMissing)
|
||||||
|
{
|
||||||
|
assert(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -61,3 +61,12 @@ public final class PocketBaseParsingException : PBException
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public final class RemoteFieldMissing : PBException
|
||||||
|
{
|
||||||
|
this()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue