sam701 / zig-toml

Zig TOML (v1.0.0) parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parser does not respect default values

Foretack opened this issue ยท comments

pub fn main() !void {
    var parser = toml.Parser(Watah).init(std.heap.page_allocator);
    var parsed = try parser.parseString(
        \\forsen = "abc"
    );

    std.debug.print("{s} {any}\n", .{ parsed.value.forsen, parsed.value.fdefault });
}

const Watah = struct {
    forsen: []const u8,
    fdefault: u8 = 2,
};

Even though fdefault has a default value of 2 here, the parser returns an error because it doesn't find it in the string:

> zig run src/main.zig
error: MissingRequiredField
D:\VSRepos\test\src\zig-toml\src\struct_mapping.zig:37:25: 0x7ff7eb4876a9 in intoStruct__anon_4214 (main.exe.obj)
                        return error.MissingRequiredField;
                        ^
D:\VSRepos\test\src\zig-toml\src\main.zig:98:17: 0x7ff7eb4888d8 in parseString (main.exe.obj)
                return err;
                ^
D:\VSRepos\test\src\main.zig:6:18: 0x7ff7eb488c65 in main (main.exe.obj)
    var parsed = try parser.parseString(

Good point ๐Ÿ‘ I'll push a fix.

The default value in the struct does not seem to be used by the parser as a fallback value.

@yehuthi Could you submit a code snippet to get better insights? I added a test last time that addresses this issue. But maybe not all cases were covered ๐Ÿค”

Ah, I see, the value is never checked in the test. I'll have a look. ๐Ÿ‘€

@yehuthi Indeed the issue was not fixed correctly. Now it should be. I added a test to check default struct values. Thanks for reporting. ๐Ÿ‘