Skip to content

Commit

Permalink
Fix issue when parsing second union field
Browse files Browse the repository at this point in the history
When parsing the second field of a union and the first field is a
struct, in the inlined loop, the first field returns
error.StructFieldMissing which should be ignored, as error.TypeMismatch
already is.
  • Loading branch information
djboni authored and kubkon committed Jan 15, 2024
1 parent 953bf8e commit c67849c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/yaml.zig
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,10 @@ pub const Yaml = struct {
inline for (union_info.fields) |field| {
if (self.parseValue(field.type, value)) |u_value| {
return @unionInit(T, field.name, u_value);
} else |err| {
if (@as(@TypeOf(err) || error{TypeMismatch}, err) != error.TypeMismatch) return err;
} else |err| switch (err) {
error.TypeMismatch => {},
error.StructFieldMissing => {},
else => return err,
}
}
} else return error.UntaggedUnion;
Expand Down
48 changes: 48 additions & 0 deletions src/yaml/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,54 @@ test "typed nested structs" {
try testing.expectEqualStrings("wait, what?", simple.a.c);
}

test "typed union with nested struct" {
const source =
\\a:
\\ b: hello there
;

var yaml = try Yaml.load(testing.allocator, source);
defer yaml.deinit();

const simple = try yaml.parse(union(enum) {
tag_a: struct {
a: struct {
b: []const u8,
},
},
tag_c: struct {
c: struct {
d: []const u8,
},
},
});
try testing.expectEqualStrings("hello there", simple.tag_a.a.b);
}

test "typed union with nested struct 2" {
const source =
\\c:
\\ d: hello there
;

var yaml = try Yaml.load(testing.allocator, source);
defer yaml.deinit();

const simple = try yaml.parse(union(enum) {
tag_a: struct {
a: struct {
b: []const u8,
},
},
tag_c: struct {
c: struct {
d: []const u8,
},
},
});
try testing.expectEqualStrings("hello there", simple.tag_c.c.d);
}

test "single quoted string" {
const source =
\\- 'hello'
Expand Down

0 comments on commit c67849c

Please sign in to comment.