import salad.util : edig; enum fieldName = "strVariable"; Node n = [ fieldName: "string value" ]; string strVariable_; enum exp = Assign!(n, strVariable_).stripLeftAll; static assert(exp == q"EOS import salad.util : edig; import std.algorithm : map; import std.array : array; strVariable_ = n.edig("strVariable").as!string; EOS".stripLeftAll, exp); mixin(exp); assert(strVariable_ == "string value");
optional of non-array type
import std.exception : assertNotThrown; enum fieldName = "param"; Node n = [ fieldName: true ]; Optional!bool param_; enum exp = Assign!(n, param_).stripLeftAll; static assert(exp == q"EOS import salad.util : edig; import std.algorithm : map; import std.array : array; if (auto f = "param" in n) { param_ = (*f).as!bool; } EOS".stripLeftAll, exp); mixin(exp); assert(param_.tryMatch!((bool b) => b) .assertNotThrown);
optional of array type
import std.algorithm : map; import std.array : array; import std.exception : assertNotThrown; enum fieldName = "params"; Node n = [ fieldName: [1, 2, 3] ]; Optional!(int[]) params_; enum exp = Assign!(n, params_).stripLeftAll; static assert(exp == q"EOS import salad.util : edig; import std.algorithm : map; import std.array : array; if (auto f = "params" in n) { params_ = (*f).sequence.map!((a) { int ret; ret = a.as!int; return ret; }).array; } EOS".stripLeftAll, exp); mixin(exp); assert(params_.tryMatch!((int[] arr) => arr) .assertNotThrown == [1, 2, 3]);