Skip to content

Commit

Permalink
Add parsing of pointers
Browse files Browse the repository at this point in the history
Pointers can be useful if we want to allow using types recursively
directly (i.e. not as an array or mapping), for example to express
tree structures (decision trees, composite types, etc.).
  • Loading branch information
VPanteleev-S7 committed Jan 15, 2025
1 parent 384a064 commit 7ac1825
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions source/configy/read.d
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,14 @@ package FR.Type parseField (alias FR)
);
}
}
else static if (is(FR.Type == T*, T))
{
// Allocate and parse pointers' values.
auto value = new T;
*value = node.parseField!(NestedFieldRef!(T, FR))(
path, T.init, ctx);
return value;
}
else
{
static assert (!is(FR.Type == union),
Expand Down
18 changes: 18 additions & 0 deletions source/configy/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,21 @@ ds:
catch (ConfigException exc)
assert(exc.toString() == "/dev/null(1:11): es.enabled: Expected to be a value of type bool, but is a scalar");
}

/// Test pointers
unittest
{
static struct N {
@Optional int value;
@Optional N* left, right;
}
auto c = parseConfigString!N(`left:
left:
value: 1
right:
value: 2
`, "/dev/null");
assert(c.left.left.value == 1);
assert(c.left.right is null);
assert(c.right.value == 2);
}

0 comments on commit 7ac1825

Please sign in to comment.