From cc13f0fe4954e50673cf6f1532864d2b49c34f6c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 11 Sep 2023 08:31:42 -0700 Subject: [PATCH] config: cannot set underscore-prefixed fields --- src/cli_args.zig | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cli_args.zig b/src/cli_args.zig index b89c51a0a..76bcbe245 100644 --- a/src/cli_args.zig +++ b/src/cli_args.zig @@ -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);