Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing of pointers #63

Open
wants to merge 2 commits into
base: v2.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't quite work for const / immutable types though. A lot of the code in this module is convoluted to allow for const & immutable values to be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fix pushed, not sure if that fix is the best way?

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pointers should always be optional. I can't see a situation where they wouldn't be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can think of one - when using mutually recursive types, only one point of recursion might be optional, e.g.:

import configy.attributes;

/// Node for an AST for some language that uses infix operators.
struct Node {
  // Only one of these must be set.
  @Optional BinaryExpression* addition;
  @Optional BinaryExpression* subtraction;
  // ...
}

struct BinaryExpression {
  // You've chosen which kind of expression this is.
  // Now, you MUST specify the operands, so these are not optional:
  Node* left, right;
}

Copy link
Contributor Author

@VPanteleev-S7 VPanteleev-S7 Jan 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess left and right could just be Node. Seems a bit weird though, with how ASTs are usually represented in memory. Might be asymmetric with other parts where Node values are optional.

}
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);
}
Loading