From dfea4ea1509d87df265dd97ea34b182cc826d972 Mon Sep 17 00:00:00 2001 From: sigod Date: Mon, 24 Aug 2015 19:51:24 +0300 Subject: [PATCH 1/2] added @asString attribute (needed for 2 fields in GCMNotification) --- source/gcm.d | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/source/gcm.d b/source/gcm.d index d29b9d8..8b82816 100644 --- a/source/gcm.d +++ b/source/gcm.d @@ -101,12 +101,14 @@ struct GCMNotification string body_loc_key; /// Indicates the string value to replace format specifiers in body string for localization. + @asString string[] body_loc_args; /// Indicates the key to the title string for localization. string title_loc_key; /// Indicates the string value to replace format specifiers in title string for localization. + @asString string[] title_loc_args; } @@ -205,6 +207,9 @@ class GCM } } +/// +enum asString; + private: alias Alias(alias a) = a; @@ -234,7 +239,6 @@ template isISOExtStringSerializable(T) } //TODO: support classes -//TODO: `asString` fields JSONValue convert(T)(T value) { import std.traits : isSomeFunction, isTypeTuple; @@ -259,17 +263,28 @@ JSONValue convert(T)(T value) continue; } + JSONValue json = void; + static if (__traits(compiles, { auto v = JSONValue(__traits(getMember, value, field_name)); })) { - ret[stripName!field_name] = JSONValue(__traits(getMember, value, field_name)); + json = JSONValue(__traits(getMember, value, field_name)); } else static if (isISOExtStringSerializable!FieldN) { - ret[stripName!field_name] = __traits(getMember, value, field_name).toISOExtString(); + json = __traits(getMember, value, field_name).toISOExtString(); } else static if (is(FieldN == struct)) { - ret[stripName!field_name] = convert(__traits(getMember, value, field_name)); + json = convert(__traits(getMember, value, field_name)); } else static assert(false, FieldN.stringof ~ " not supported"); + + //TODO: use hasUDA from 2.068 + static if (__traits(getAttributes, __traits(getMember, value, field_name)).length > 0 + && __traits(isSame, __traits(getAttributes, __traits(getMember, value, field_name))[0], asString)) + { + json = JSONValue(json.toString()); + } + + ret[stripName!field_name] = json; } } From 7c5a05c77a2dcae199fd767a5a2f320cc4ae5310 Mon Sep 17 00:00:00 2001 From: sigod Date: Wed, 26 Aug 2015 00:11:42 +0300 Subject: [PATCH 2/2] use hasUDA from 2.068.0 version --- source/gcm.d | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/source/gcm.d b/source/gcm.d index 8b82816..8173c26 100644 --- a/source/gcm.d +++ b/source/gcm.d @@ -238,6 +238,19 @@ template isISOExtStringSerializable(T) is(typeof(T.init.toISOExtString()) == string) && is(typeof(T.fromISOExtString("")) == T); } +static if (__VERSION__ < 2068) { + //TODO: remove in future versions of compiler + template hasUDA(alias symbol, alias attribute) + { + import std.typetuple : staticIndexOf; + + enum bool hasUDA = staticIndexOf!(attribute, __traits(getAttributes, symbol)) != -1; + } +} +else { + import std.traits : hasUDA; +} + //TODO: support classes JSONValue convert(T)(T value) { @@ -277,10 +290,7 @@ JSONValue convert(T)(T value) else static assert(false, FieldN.stringof ~ " not supported"); - //TODO: use hasUDA from 2.068 - static if (__traits(getAttributes, __traits(getMember, value, field_name)).length > 0 - && __traits(isSame, __traits(getAttributes, __traits(getMember, value, field_name))[0], asString)) - { + static if (hasUDA!(Field, asString)) { json = JSONValue(json.toString()); }