zig-yaml
zig-yaml copied to clipboard
Struct's default value not being used
Hi,
I'm considering using zig-yaml for a little side project I'm working on. One of the features I would like is the initialisation of the struct with its default values if it is missing from the yaml file.
I've made a variation of the sample from the README.md
test "yaml" {
const Yaml = @import("yaml").Yaml;
const source =
\\names: [ John Doe, MacIntosh, Jane Austin ]
\\numbers:
\\ - 10
\\ - -8
\\ - 6
\\nested:
\\ wick: john doe
\\finally: [ 8.17,
\\ 19.78 , 17 ,
\\ 21 ]
;
const allocator = std.testing.allocator;
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
var yaml: Yaml = .{ .source = source };
defer yaml.deinit(allocator);
try yaml.load(allocator);
const Simple = struct {
names: []const []const u8,
numbers: []const i16,
nested: struct {
some: []const u8 = "some",
wick: []const u8,
},
finally: [4]f16,
};
const simple = try yaml.parse(arena.allocator(), Simple);
try std.testing.expectEqual(simple.names.len, 3);
std.debug.print("### {} \n {s}\n", .{ simple, simple.nested.some });
}
This test currently fails for me:)
/home/shri/.cache/zig/p/zig_yaml-0.1.0-C1161rqKAgBMVoj-bfHWpzEdjtcd2FUU8VerXw-jnrQ-/src/Yaml.zig:194:13: 0x1241bae in parseStruct__anon_43651 (test)
return error.StructFieldMissing;
^
/home/shri/.cache/zig/p/zig_yaml-0.1.0-C1161rqKAgBMVoj-bfHWpzEdjtcd2FUU8VerXw-jnrQ-/src/Yaml.zig:93:5: 0x1237dc3 in parseValue__anon_43157 (test)
return switch (@typeInfo(T)) {
^
/home/shri/.cache/zig/p/zig_yaml-0.1.0-C1161rqKAgBMVoj-bfHWpzEdjtcd2FUU8VerXw-jnrQ-/src/Yaml.zig:196:38: 0x1230bbf in parseStruct__anon_42708 (test)
@field(parsed, field.name) = try self.parseValue(arena, field.type, unwrapped);
^
/home/shri/.cache/zig/p/zig_yaml-0.1.0-C1161rqKAgBMVoj-bfHWpzEdjtcd2FUU8VerXw-jnrQ-/src/Yaml.zig:93:5: 0x122aa57 in parseValue__anon_42129 (test)
return switch (@typeInfo(T)) {
^
/home/shri/.cache/zig/p/zig_yaml-0.1.0-C1161rqKAgBMVoj-bfHWpzEdjtcd2FUU8VerXw-jnrQ-/src/Yaml.zig:64:9: 0x12246a3 in parse__anon_41623 (test)
return self.parseValue(arena, T, self.docs.items[0]);
^
I see that https://github.com/kubkon/zig-yaml/issues/85 was closed with https://github.com/kubkon/zig-yaml/pull/86 getting merged in. I thought this was bringing in precisely this feature.
I am (as far as I know), using the latest commit:
zig fetch --save=yaml https://github.com/kubkon/zig-yaml/archive/47aed6a945d1eaeb47d63b7f10b8f68ad6e37820.tar.gz
I am pretty new to zig, so it's entirely likely that I'm doing something wrong - and I'd appreciate some assistance in resolving this.
Thanks & Best Wishes
Should be fixed by #97