Skip to content

Commit

Permalink
getting the type out of string is hard
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Schadek committed Jul 11, 2019
1 parent a81e291 commit 4ce8932
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
62 changes: 57 additions & 5 deletions source/graphql/helper.d
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,11 @@ const(Document) lexAndParse(string s) {

string stringTypeStrip(string str) {
import std.algorithm.searching : startsWith, endsWith, canFind;
import std.string : capitalize;
import std.string : capitalize, indexOf, strip;
immutable fs = ["Nullable!", "NullableStore!", "GQLDCustomLeaf!"];
immutable arr = "[]";
outer: while(true) {
str = str.strip();
foreach(f; fs) {
if(str.startsWith(f)) {
str = str[f.length .. $];
Expand Down Expand Up @@ -478,6 +479,9 @@ string stringTypeStrip(string str) {
break;
}

const comma = str.indexOf(',');
str = comma == -1 ? str : str[0 .. comma].strip();

str = canFind(["ubyte", "byte", "ushort", "short", "long", "ulong"], str)
? "Int"
: str;
Expand Down Expand Up @@ -515,17 +519,65 @@ unittest {
assert(r == "__Type", r);
}

Json toGraphqlJson(T)(auto ref T obj) {
Json toGraphqlJson(T)(auto ref T input) {
import std.typecons : Nullable;
import std.array : empty;
import std.traits : isArray, isAggregateType, isBasicType, isSomeString,
isScalarType, isSomeString, FieldNameTuple, FieldTypeTuple;

import nullablestore;

import graphql.uda : GQLDCustomLeaf;
import std.traits : isArray, FieldNameTuple, FieldTypeTuple;
static if(isArray!T && !isSomeString!T) {
Json ret = Json.emptyArray();
foreach(ref it; input) {
ret ~= toGraphqlJson(it);
}
return ret;
} else static if(is(T : GQLDCustomLeaf!Type, Type...)) {
return Json(Type[1](input));
} else static if(is(T : Nullable!Type, Type)) {
return input.isNull() ? Json(null) : toGraphqlJson(input.get());
} else static if(isBasicType!T || isScalarType!T || isSomeString!T) {
return serializeToJson(input);
} else static if(isAggregateType!T) {
Json ret = Json.emptyObject();

// the important bit is the setting of the __typename field
ret["__typename"] = T.stringof;
alias names = FieldNameTuple!(T);
alias types = FieldTypeTuple!(T);
static foreach(idx; 0 .. names.length) {{
static if(!names[idx].empty) {
static if(is(types[idx] : NullableStore!Type, Type)) {
} else static if(is(types[idx] == enum)) {
ret[names[idx]] = serializeToJson(
__traits(getMember, input, names[idx])
);
} else {
ret[names[idx]] = toGraphqlJson(
__traits(getMember, input, names[idx])
);
}
}
}}
return ret;
} else {
static assert(false, T.stringof ~ " not supported");
}
}

/*Json toGraphqlJson(T)(auto ref T obj) {
import graphql.uda : GQLDCustomLeaf;
import std.traits : isArray, isSomeString, FieldNameTuple, FieldTypeTuple;
import std.array : empty;
import std.typecons : Nullable;
import nullablestore;
alias names = FieldNameTuple!(T);
alias types = FieldTypeTuple!(T);
static if(isArray!T) {
static if(isArray!T && !isSomeString!T) {
Json ret = Json.emptyArray();
foreach(ref it; obj) {
ret ~= toGraphqlJson(it);
Expand Down Expand Up @@ -563,7 +615,7 @@ Json toGraphqlJson(T)(auto ref T obj) {
}}
}
return ret;
}
}*/

string dtToString(DateTime dt) {
return dt.toISOExtString();
Expand Down
18 changes: 12 additions & 6 deletions source/graphql/schema/typeconversions.d
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ template typeToTypeEnum(Type) {
}

template typeToTypeName(Type) {
import graphql.uda : GQLDCustomLeaf;
static if(is(Type == enum)) {
enum typeToTypeName = Type.stringof;
} else static if(is(Type == bool)) {
enum typeToTypeName = "Bool";
} else static if(is(Type == GQLDCustomLeaf!F, F)) {
enum typeToTypeName = F.stringof;
} else static if(is(Type == GQLDCustomLeaf!Fs, Fs...)) {
enum typeToTypeName = Fs[0].stringof;
} else static if(isFloatingPoint!(Type)) {
enum typeToTypeName = "Float";
} else static if(isIntegral!(Type)) {
Expand All @@ -69,6 +70,11 @@ template typeToTypeName(Type) {
}
}

unittest {
import std.datetime : Date;
static assert(typeToTypeName!(GQLDCustomLeaf!Date) == "Date");
}

template typeToParameterTypeName(Type) {
template level2(Type) {
static if(is(Type : Nullable!F, F)) {
Expand Down Expand Up @@ -111,7 +117,7 @@ unittest {
template isScalarType(Type) {
static if(is(Type == bool)) {
enum isScalarType = true;
} else static if(is(Type == GQLDCustomLeaf!F, F)) {
} else static if(is(Type == GQLDCustomLeaf!Fs, Fs...)) {
enum isScalarType = true;
} else static if(isFloatingPoint!(Type)) {
enum isScalarType = true;
Expand Down Expand Up @@ -321,7 +327,7 @@ Json typeToJsonImpl(Type,Schema,Orig)() {
// fields
static if((is(Type == class) || is(Type == interface) || is(Type == struct))
&& !is(Type : Nullable!K, K) && !is(Type : NullableStore!K, K)
&& !is(Type : GQLDCustomLeaf!K, K))
&& !is(Type : GQLDCustomLeaf!Ks, Ks...))
{
ret[Constants.fields] = typeFields!Type();
} else {
Expand Down Expand Up @@ -376,9 +382,9 @@ Json typeToJsonImpl(Type,Schema,Orig)() {

// needed to resolve ofType
static if(is(Type : Nullable!F, F) || is(Type : NullableStore!F, F)
|| is(Type : GQLDCustomLeaf!F, F))
|| is(Type : GQLDCustomLeaf!Fs, Fs...))
{
ret[Constants.ofTypeName] = F.stringof;
ret[Constants.ofTypeName] = Fs[0].stringof;
} else static if(isArray!Type) {
ret[Constants.ofTypeName] = ElementEncodingType!(Type).stringof;
}
Expand Down
7 changes: 5 additions & 2 deletions source/graphql/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ template isNotObject(Type) {

template isNotCustomLeaf(Type) {
import graphql.uda;
enum isNotCustomLeaf = !is(Type : GQLDCustomLeaf!F, F);
enum isNotCustomLeaf = !is(Type : GQLDCustomLeaf!Fs, Fs...);
}

unittest {
Expand All @@ -129,7 +129,7 @@ unittest {

template collectTypesImpl(Type) {
import graphql.uda;
static if(is(Type : GQLDCustomLeaf!F, F)) {
static if(is(Type : GQLDCustomLeaf!Fs, Fs...)) {
alias collectTypesImpl = AliasSeq!(Type);
} else static if(is(Type == interface)) {
alias RetTypes = AliasSeq!(collectReturnType!(Type,
Expand Down Expand Up @@ -266,6 +266,9 @@ template fixupBasicTypes(T) {
} else static if(isArray!T) {
alias ElemFix = fixupBasicTypes!(ElementEncodingType!T);
alias fixupBasicTypes = ElemFix[];
} else static if(is(T : GQLDCustomLeaf!Fs, Fs...)) {
alias ElemFix = fixupBasicTypes!(Fs[0]);
alias fixupBasicTypes = GQLDCustomLeaf!(ElemFix, Fs[1 .. $]);
} else static if(is(T : Nullable!F, F)) {
alias ElemFix = fixupBasicTypes!(F);
alias fixupBasicTypes = Nullable!(ElemFix);
Expand Down
1 change: 1 addition & 0 deletions source/graphql/validation/schemabased.d
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class SchemaValidator(Schema) : Visitor {
this.schemaStack.back.name, name)
);


string followType = field[Constants.typenameOrig].get!string();
string old = followType;
followType = followType.stringTypeStrip();
Expand Down

0 comments on commit 4ce8932

Please sign in to comment.