Skip to content

Commit

Permalink
Make hooks work for the top-level type
Browse files Browse the repository at this point in the history
Before this change, the various hooks (converter, fromYAML, fromString)
would only work after one level of recursion.
This was obviously an oversight and is now corrected.
  • Loading branch information
Geod24 committed Jan 11, 2024
1 parent 6883350 commit 264f89f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/configy/Read.d
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ public T parseConfig (T) (
fullyQualifiedName!T,
strict == StrictMode.Warn ?
strict.paint(Yellow) : strict.paintIf(!!strict, Green, Red));
return node.parseMapping!(StructFieldRef!T)(
null, T.init, const(Context)(cmdln, strict), null);
return node.parseField!(StructFieldRef!T)(
null, T.init, const(Context)(cmdln, strict));
case NodeID.sequence:
case NodeID.scalar:
case NodeID.invalid:
Expand Down
50 changes: 50 additions & 0 deletions source/configy/Test.d
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,53 @@ deps:
assert(c.deps[2] == Package(null, PackageDef("fur", null, 42)));
assert(c.deps[3] == Package("/one/last/path"));
}

/// Test top level hook (fromYAML / fromString)
unittest
{
static struct Version1 {
uint fileVersion;
uint value;
}

static struct Version2 {
uint fileVersion;
string str;
}

static struct Config
{
uint fileVersion;
union {
Version1 v1;
Version2 v2;
}
static Config fromYAML (scope ConfigParser!Config parser)
{
static struct OnlyVersion { uint fileVersion; }
auto vers = parseConfig!OnlyVersion(
CLIArgs.init, parser.node, StrictMode.Ignore);
switch (vers.fileVersion) {
case 1:
return Config(1, parser.parseAs!Version1);
case 2:
Config conf = Config(2);
conf.v2 = parser.parseAs!Version2;
return conf;
default:
assert(0);
}
}
}

auto v1 = parseConfigString!Config("fileVersion: 1\nvalue: 42", "/dev/null");
auto v2 = parseConfigString!Config("fileVersion: 2\nstr: hello world", "/dev/null");

assert(v1.fileVersion == 1);
assert(v1.v1.fileVersion == 1);
assert(v1.v1.value == 42);

assert(v2.fileVersion == 2);
assert(v2.v2.fileVersion == 2);
assert(v2.v2.str == "hello world");
}

0 comments on commit 264f89f

Please sign in to comment.