config: fix regression where we halted parsing on deprecated field (#7719)

Fix regression from d44a6cde2c where we
halted parsing on deprecated fields, which was not the intended
behavior.

This commit fixes that and adds a test to verify it.
pull/7721/head
Mitchell Hashimoto 2025-06-28 19:27:43 -07:00 committed by GitHub
commit 67fe3d418a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 3 deletions

View File

@ -132,14 +132,20 @@ pub fn parse(
break :value null;
};
parseIntoField(T, arena_alloc, dst, key, value) catch |err| {
parseIntoField(T, arena_alloc, dst, key, value) catch |err| err: {
// If we get an error parsing a field, then we try to fall
// back to compatibility handlers if able.
if (@hasDecl(T, "compatibility")) {
// If we have a compatibility handler for this key, then
// we call it and see if it handles the error.
if (T.compatibility.get(key)) |handler| {
if (handler(dst, arena_alloc, key, value)) return;
if (handler(dst, arena_alloc, key, value)) {
log.info(
"compatibility handler for {s} handled error, you may be using a deprecated field: {}",
.{ key, err },
);
break :err;
}
}
}
@ -838,6 +844,7 @@ test "parse: compatibility renamed" {
var data: struct {
a: bool = false,
b: bool = false,
_arena: ?ArenaAllocator = null,
pub const compatibility: std.StaticStringMap(
@ -850,12 +857,13 @@ test "parse: compatibility renamed" {
var iter = try std.process.ArgIteratorGeneral(.{}).init(
testing.allocator,
"--old=true",
"--old=true --b=true",
);
defer iter.deinit();
try parse(@TypeOf(data), testing.allocator, &data, &iter);
try testing.expect(data._arena != null);
try testing.expect(data.a);
try testing.expect(data.b);
}
test "parseIntoField: ignore underscore-prefixed fields" {