Skip to content

Commit

Permalink
fix: Properly handle invalid values in enabled / disabled handling
Browse files Browse the repository at this point in the history
Otherwise we are showing internal D-YAML errors.
  • Loading branch information
Geod24 committed Jul 31, 2024
1 parent 07ac9b0 commit be14b9c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
8 changes: 4 additions & 4 deletions source/configy/Read.d
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private TLFR.Type parseMapping (alias TLFR)
}
}

const enabledState = node.isMappingEnabled!(TLFR.Type)(defaultValue);
const enabledState = node.isMappingEnabled!(TLFR.Type)(path, defaultValue);

if (enabledState.field != EnabledState.Field.None)
dbgWrite("%s: Mapping is enabled: %s", TLFR.Type.stringof.paint(Cyan), (!!enabledState).paintBool());
Expand Down Expand Up @@ -993,7 +993,7 @@ private template NameIs (string searching)

/// Returns whether or not the field has a `enabled` / `disabled` field,
/// and its value. If it does not, returns `true`.
private EnabledState isMappingEnabled (M) (Node node, auto ref M default_)
private EnabledState isMappingEnabled (M) (Node node, string path, auto ref M default_)
{
import std.meta : Filter;

Expand All @@ -1007,13 +1007,13 @@ private EnabledState isMappingEnabled (M) (Node node, auto ref M default_)
"` conflicts with `disabled` field `" ~ DMT[0].FieldName ~ "`");

if (auto ptr = "enabled" in node)
return EnabledState(EnabledState.Field.Enabled, (*ptr).as!bool);
return EnabledState(EnabledState.Field.Enabled, (*ptr).parseScalar!(bool)(path, "enabled"));
return EnabledState(EnabledState.Field.Enabled, __traits(getMember, default_, EMT[0].FieldName));
}
else static if (DMT.length)
{
if (auto ptr = "disabled" in node)
return EnabledState(EnabledState.Field.Disabled, (*ptr).as!bool);
return EnabledState(EnabledState.Field.Disabled, (*ptr).parseScalar!(bool)(path, "disabled"));
return EnabledState(EnabledState.Field.Disabled, __traits(getMember, default_, DMT[0].FieldName));
}
else
Expand Down
37 changes: 37 additions & 0 deletions source/configy/Test.d
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,40 @@ dynamic:
assert(c1.static_[1].value == 70);
assert(c1.static_[2].value == 71);
}

/// Test around enabled / disabled not being boolean values
unittest
{
static struct SectionE {
bool enabled;
int value;
string val2;
}

static struct SectionD {
bool disabled = false;
int value;
string val2;
}

static struct Config {
SectionE es;
SectionD ds;
}

try parseConfigString!Config(`es:
enabled: false
ds:
disabled: MAYBE
`, "/dev/null");
catch (ConfigException exc)
assert(exc.toString() == "/dev/null(3:12): ds.disabled: Expected to be a value of type bool, but is a scalar");

try parseConfigString!Config(`es:
enabled: PERHAPS
ds:
disabled: true
`, "/dev/null");
catch (ConfigException exc)
assert(exc.toString() == "/dev/null(1:11): es.enabled: Expected to be a value of type bool, but is a scalar");
}

0 comments on commit be14b9c

Please sign in to comment.