Skip to content

Commit

Permalink
enforce type constraints a little more, add ability to ignore errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Herringway committed Jul 20, 2016
1 parent 7e28226 commit abb5d79
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions source/siryul/common.d
Expand Up @@ -82,6 +82,8 @@ struct CustomParser {
enum AsString;
///Write field as binary (NYI)
enum AsBinary;
///Errors are ignored; value will be .init
enum IgnoreErrors;

alias isSimpleList = templateAnd!(isIterable, templateNot!isSomeString);
static assert(isSimpleList!(int[]));
Expand Down
8 changes: 7 additions & 1 deletion source/siryul/dyaml.d
Expand Up @@ -98,7 +98,12 @@ private T fromYAML(T, BitFlags!DeSiryulize flags)(Node node) @safe if (!isInfini
} else
enforce(node.containsKey(memberName), new YAMLException("Missing non-@Optional "~memberName~" in node"));
alias fromFunc = getConvertFromFunc!(T, __traits(getMember, output, member));
__traits(getMember, output, member) = fromFunc(node[memberName].fromYAML!(Parameters!(fromFunc)[0], flags));
static if (hasUDA!(__traits(getMember, T, member), IgnoreErrors)) {
try {
__traits(getMember, output, member) = fromFunc(node[memberName].fromYAML!(Parameters!(fromFunc)[0], flags));
} catch (YAMLException) {}
} else
__traits(getMember, output, member) = fromFunc(node[memberName].fromYAML!(Parameters!(fromFunc)[0], flags));
}
return output;
} else static if(isOutputRange!(T, ElementType!T)) {
Expand Down Expand Up @@ -127,6 +132,7 @@ private T fromYAML(T, BitFlags!DeSiryulize flags)(Node node) @safe if (!isInfini
output[fromYAML!(KeyType!T, flags)(key)] = fromYAML!(ValueType!T, flags)(value);
return output;
} else static if (is(T == bool)) {
enforce(node.tag == `tag:yaml.org,2002:bool`, new YAMLException("Expecting a boolean value"));
return node.get!T;
} else
static assert(false, "Cannot read type "~T.stringof~" from YAML"); //unreachable, hopefully.
Expand Down
9 changes: 8 additions & 1 deletion source/siryul/json.d
Expand Up @@ -60,6 +60,7 @@ private T fromJSON(T, BitFlags!DeSiryulize flags)(JSONValue node) @trusted if (!
return node.integer.to!T;
if (node.type == JSON_TYPE.NULL)
return T.init;
expect(node, JSON_TYPE.STRING);
return node.str.to!T;
} else static if (isSomeChar!T) {
expect(node, JSON_TYPE.STRING, JSON_TYPE.NULL);
Expand All @@ -80,7 +81,13 @@ private T fromJSON(T, BitFlags!DeSiryulize flags)(JSONValue node) @trusted if (!
} else
enforce(memberName in node.object, new JSONDException("Missing non-@Optional "~memberName~" in node"));
alias fromFunc = getConvertFromFunc!(T, field);
__traits(getMember, output, member) = fromFunc(node[memberName].fromJSON!(Parameters!(fromFunc)[0], flags));
static if (hasUDA!(field, IgnoreErrors)) {
try {
__traits(getMember, output, member) = fromFunc(node[memberName].fromJSON!(Parameters!(fromFunc)[0], flags));
} catch (UnexpectedTypeException) {} //just skip it
} else {
__traits(getMember, output, member) = fromFunc(node[memberName].fromJSON!(Parameters!(fromFunc)[0], flags));
}
}
return output;
} else static if(isOutputRange!(T, ElementType!T)) {
Expand Down
13 changes: 13 additions & 0 deletions source/siryul/siryul.d
Expand Up @@ -435,6 +435,19 @@ version(unittest) {
runTest2Fail!(float[])(3);
//Precision loss should be rejected by default
runTest2Fail!int(3.5);
//bool -> string???
runTest2Fail!string(true);
//string -> bool???
runTest2Fail!bool("nah");

struct IgnoreErrBad {
float n = 1.2;
}
struct IgnoreErrGood {
@IgnoreErrors int n = 5;
}
foreach (siryulizer; siryulizers)
assert(IgnoreErrBad().toString!siryulizer.fromString!(IgnoreErrGood, siryulizer).n == IgnoreErrGood.init.n, "IgnoreErrors test failed for "~siryulizer.stringof);
}
///Use standard ISO8601 format for dates and times - YYYYMMDDTHHMMSS.FFFFFFFTZ
enum ISO8601;
Expand Down

0 comments on commit abb5d79

Please sign in to comment.