mirror of https://github.com/Hax-io/libpb
Compare commits
3 Commits
c89d6bd8b6
...
aa77afccac
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | aa77afccac | |
Tristan B. Velloza Kildaire | da22ae1c61 | |
Tristan B. Velloza Kildaire | 4fc4242c1e |
|
@ -15,3 +15,4 @@ libpb-test-*
|
||||||
*.obj
|
*.obj
|
||||||
*.lst
|
*.lst
|
||||||
liblibpb.a
|
liblibpb.a
|
||||||
|
dub.selections.json
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 6.4 KiB |
Binary file not shown.
9
dub.json
9
dub.json
|
@ -3,9 +3,14 @@
|
||||||
"Tristan B. Velloza Kildaire"
|
"Tristan B. Velloza Kildaire"
|
||||||
],
|
],
|
||||||
"copyright": "Copyright © 2022, Tristan B. Velloza Kildaire",
|
"copyright": "Copyright © 2022, Tristan B. Velloza Kildaire",
|
||||||
|
"dependencies": {
|
||||||
|
"jstruct": "0.1.2"
|
||||||
|
},
|
||||||
"description": "PocketBase wrapper with serializer/deserializer support",
|
"description": "PocketBase wrapper with serializer/deserializer support",
|
||||||
|
"libs": [
|
||||||
|
"curl"
|
||||||
|
],
|
||||||
"license": "LGPL v3.0",
|
"license": "LGPL v3.0",
|
||||||
"name": "libpb",
|
"name": "libpb",
|
||||||
"targetType" : "library",
|
"targetType": "library"
|
||||||
"libs":["curl"]
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,213 +0,0 @@
|
||||||
module libpb.deserialization;
|
|
||||||
|
|
||||||
import std.json;
|
|
||||||
import libpb.exceptions : RemoteFieldMissing;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* T
|
|
||||||
*
|
|
||||||
* Params:
|
|
||||||
* RecordType = type of the struct to construct
|
|
||||||
*/
|
|
||||||
mixin template T(RecordType)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
RecordType record;
|
|
||||||
|
|
||||||
// Alias as to only expand later when used in compile-time
|
|
||||||
alias structTypes = FieldTypeTuple!(RecordType);
|
|
||||||
alias structNames = FieldNameTuple!(RecordType);
|
|
||||||
alias structValues = record.tupleof;
|
|
||||||
|
|
||||||
static foreach(cnt; 0..structTypes.length)
|
|
||||||
{
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg, structTypes[cnt]);
|
|
||||||
pragma(msg, structNames[cnt]);
|
|
||||||
// pragma(msg, structValues[cnt]);
|
|
||||||
}
|
|
||||||
|
|
||||||
pragma(msg, "Bruh type");
|
|
||||||
pragma(msg, structTypes[cnt]);
|
|
||||||
// pragma(msg, __traits(identifier, mixin(structTypes[cnt])));
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), JSONValue))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]];
|
|
||||||
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg,"record."~structNames[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else static if(__traits(isSame, mixin(structTypes[cnt]), bool))
|
|
||||||
{
|
|
||||||
mixin("record."~structNames[cnt]) = jsonIn[structNames[cnt]].boolean();
|
|
||||||
|
|
||||||
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])[]))
|
|
||||||
{
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(JSONException e)
|
|
||||||
{
|
|
||||||
throw new RemoteFieldMissing();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return record;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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},
|
|
||||||
"isMale": true,
|
|
||||||
"list": [1,2,3]
|
|
||||||
}
|
|
||||||
`);
|
|
||||||
|
|
||||||
mixin T!(Person);
|
|
||||||
Person person = fromJSON(json);
|
|
||||||
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
writeln(person);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(cmp(person.firstname, "Tristan") == 0);
|
|
||||||
assert(cmp(person.lastname, "Kildaire") == 0);
|
|
||||||
assert(person.age == 23);
|
|
||||||
assert(person.isMale == true);
|
|
||||||
assert(person.obj["bruh"].integer() == 1);
|
|
||||||
//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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -6,8 +6,7 @@ import std.net.curl;
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
import std.string : cmp;
|
import std.string : cmp;
|
||||||
import libpb.exceptions;
|
import libpb.exceptions;
|
||||||
import libpb.serialization;
|
import jstruct : fromJSON, SerializationError, serializeRecord;
|
||||||
import libpb.deserialization;
|
|
||||||
|
|
||||||
|
|
||||||
private mixin template AuthTokenHeader(alias http, PocketBase pbInstance)
|
private mixin template AuthTokenHeader(alias http, PocketBase pbInstance)
|
||||||
|
@ -158,8 +157,7 @@ public class PocketBase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mixin T!(RecordType);
|
recordsOut ~= fromJSON!(RecordType)(returnedItem);
|
||||||
recordsOut ~= fromJSON(returnedItem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return recordsOut;
|
return recordsOut;
|
||||||
|
@ -189,6 +187,10 @@ public class PocketBase
|
||||||
{
|
{
|
||||||
throw new PocketBaseParsingException();
|
throw new PocketBaseParsingException();
|
||||||
}
|
}
|
||||||
|
catch(SerializationError e)
|
||||||
|
{
|
||||||
|
throw new RemoteFieldMissing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -269,8 +271,7 @@ public class PocketBase
|
||||||
responseJSON["passwordConfirm"] = "";
|
responseJSON["passwordConfirm"] = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
mixin T!(RecordType);
|
recordOut = fromJSON!(RecordType)(responseJSON);
|
||||||
recordOut = fromJSON(responseJSON);
|
|
||||||
|
|
||||||
return recordOut;
|
return recordOut;
|
||||||
}
|
}
|
||||||
|
@ -303,6 +304,10 @@ public class PocketBase
|
||||||
{
|
{
|
||||||
throw new PocketBaseParsingException();
|
throw new PocketBaseParsingException();
|
||||||
}
|
}
|
||||||
|
catch(SerializationError e)
|
||||||
|
{
|
||||||
|
throw new RemoteFieldMissing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -350,9 +355,7 @@ public class PocketBase
|
||||||
recordResponse["email"] = "";
|
recordResponse["email"] = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recordOut = fromJSON!(RecordType)(recordResponse);
|
||||||
mixin T!(RecordType);
|
|
||||||
recordOut = fromJSON(recordResponse);
|
|
||||||
|
|
||||||
// Store the token
|
// Store the token
|
||||||
token = responseJSON["token"].str();
|
token = responseJSON["token"].str();
|
||||||
|
@ -380,6 +383,10 @@ public class PocketBase
|
||||||
{
|
{
|
||||||
throw new PocketBaseParsingException();
|
throw new PocketBaseParsingException();
|
||||||
}
|
}
|
||||||
|
catch(SerializationError e)
|
||||||
|
{
|
||||||
|
throw new RemoteFieldMissing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -451,8 +458,7 @@ public class PocketBase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mixin T!(RecordType);
|
recordOut = fromJSON!(RecordType)(responseJSON);
|
||||||
recordOut = fromJSON(responseJSON);
|
|
||||||
|
|
||||||
return recordOut;
|
return recordOut;
|
||||||
}
|
}
|
||||||
|
@ -476,6 +482,10 @@ public class PocketBase
|
||||||
{
|
{
|
||||||
throw new PocketBaseParsingException();
|
throw new PocketBaseParsingException();
|
||||||
}
|
}
|
||||||
|
catch(SerializationError e)
|
||||||
|
{
|
||||||
|
throw new RemoteFieldMissing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -557,8 +567,7 @@ public class PocketBase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mixin T!(RecordType);
|
recordOut = fromJSON!(RecordType)(responseJSON);
|
||||||
recordOut = fromJSON(responseJSON);
|
|
||||||
|
|
||||||
return recordOut;
|
return recordOut;
|
||||||
}
|
}
|
||||||
|
@ -590,6 +599,10 @@ public class PocketBase
|
||||||
{
|
{
|
||||||
throw new PocketBaseParsingException();
|
throw new PocketBaseParsingException();
|
||||||
}
|
}
|
||||||
|
catch(SerializationError e)
|
||||||
|
{
|
||||||
|
throw new RemoteFieldMissing();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,109 +0,0 @@
|
||||||
module libpb.serialization;
|
|
||||||
|
|
||||||
import std.json;
|
|
||||||
import std.conv : to;
|
|
||||||
import std.traits : FieldTypeTuple, FieldNameTuple;
|
|
||||||
|
|
||||||
public JSONValue serializeRecord(RecordType)(RecordType record)
|
|
||||||
{
|
|
||||||
// Final JSON to submit
|
|
||||||
JSONValue builtJSON;
|
|
||||||
|
|
||||||
// Alias as to only expand later when used in compile-time
|
|
||||||
alias structTypes = FieldTypeTuple!(RecordType);
|
|
||||||
alias structNames = FieldNameTuple!(RecordType);
|
|
||||||
alias structValues = record.tupleof;
|
|
||||||
|
|
||||||
static foreach(cnt; 0..structTypes.length)
|
|
||||||
{
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg, structTypes[cnt]);
|
|
||||||
pragma(msg, structNames[cnt]);
|
|
||||||
// pragma(msg, 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
|
|
||||||
{
|
|
||||||
debug(dbg)
|
|
||||||
{
|
|
||||||
pragma(msg, "Yaa");
|
|
||||||
}
|
|
||||||
builtJSON[structNames[cnt]] = to!(string)(structValues[cnt]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return builtJSON;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test serialization of a struct to JSON
|
|
||||||
private enum EnumType
|
|
||||||
{
|
|
||||||
DOG,
|
|
||||||
CAT
|
|
||||||
}
|
|
||||||
unittest
|
|
||||||
{
|
|
||||||
import std.algorithm.searching : canFind;
|
|
||||||
import std.string : cmp;
|
|
||||||
import std.stdio : writeln;
|
|
||||||
|
|
||||||
struct Person
|
|
||||||
{
|
|
||||||
public string firstname, lastname;
|
|
||||||
public int age;
|
|
||||||
public string[] list;
|
|
||||||
public JSONValue extraJSON;
|
|
||||||
public EnumType eType;
|
|
||||||
}
|
|
||||||
|
|
||||||
Person p1;
|
|
||||||
p1.firstname = "Tristan";
|
|
||||||
p1.lastname = "Kildaire";
|
|
||||||
p1.age = 23;
|
|
||||||
p1.list = ["1", "2", "3"];
|
|
||||||
p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`);
|
|
||||||
p1.eType = EnumType.CAT;
|
|
||||||
|
|
||||||
JSONValue serialized = serializeRecord(p1);
|
|
||||||
|
|
||||||
string[] keys = serialized.object().keys();
|
|
||||||
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