From b0168345fbe6db110f128b28310d105e6bd9b29c Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev <108007295+VPanteleev-S7@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:18:37 +0000 Subject: [PATCH] configy: Add parsing of pointers 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.). --- source/configy/read.d | 8 ++++++++ source/configy/test.d | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/source/configy/read.d b/source/configy/read.d index 5f842a6..5a75785 100644 --- a/source/configy/read.d +++ b/source/configy/read.d @@ -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), diff --git a/source/configy/test.d b/source/configy/test.d index 330acbf..072a4dd 100644 --- a/source/configy/test.d +++ b/source/configy/test.d @@ -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); +}