config: cannot set underscore-prefixed fields

This commit is contained in:
Mitchell Hashimoto
2023-09-11 08:31:42 -07:00
parent 1a9ebf3048
commit cc13f0fe49

View File

@ -83,7 +83,7 @@ fn parseIntoField(
assert(info == .Struct);
inline for (info.Struct.fields) |field| {
if (mem.eql(u8, field.name, key)) {
if (field.name[0] != '_' and mem.eql(u8, field.name, key)) {
// For optional fields, we just treat it as the child type.
// This lets optional fields default to null but get set by
// the CLI.
@ -167,7 +167,7 @@ fn parseIntoField(
}
}
return error.InvalidFlag;
return error.InvalidField;
}
fn parseBool(v: []const u8) !bool {
@ -240,6 +240,23 @@ test "parse: quoted value" {
try testing.expectEqualStrings("hello!", data.b);
}
test "parseIntoField: ignore underscore-prefixed fields" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
var data: struct {
_a: []const u8 = "12",
} = .{};
try testing.expectError(
error.InvalidField,
parseIntoField(@TypeOf(data), alloc, &data, "_a", "42"),
);
try testing.expectEqualStrings("12", data._a);
}
test "parseIntoField: string" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);