Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support For Yaml Bool #40

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/yaml.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub const Value = union(enum) {
float: f64,
string: []const u8,
list: List,
boolean: bool,
map: Map,

pub fn asInt(self: Value) !i64 {
Expand All @@ -47,6 +48,11 @@ pub const Value = union(enum) {
return self.string;
}

pub fn asBool(self: Value) !bool {
if (self != .boolean) return error.TypeMismatch;
return self.boolean;
}

pub fn asList(self: Value) !List {
if (self != .list) return error.TypeMismatch;
return self.list;
Expand All @@ -68,6 +74,7 @@ pub const Value = union(enum) {
.int => |int| return writer.print("{}", .{int}),
.float => |float| return writer.print("{d}", .{float}),
.string => |string| return writer.print("{s}", .{string}),
.boolean => |bool_val| return writer.print("{}", .{bool_val}),
.list => |list| {
const len = list.len;
if (len == 0) return;
Expand Down Expand Up @@ -190,6 +197,17 @@ pub const Value = union(enum) {
return Value{ .float = float };
}

const isTruthy = std.mem.eql(u8, raw, "true");
const isFalsy = std.mem.eql(u8, raw, "false");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am no YAML expert but I think we should handle all possible variants for a boolean:

y|Y|yes|Yes|YES|n|N|no|No|NO|
true|True|TRUE|false|False|FALSE|
on|On|ON|off|Off|OFF

taken from https://stackoverflow.com/questions/53648244/specifying-the-string-value-yes-in-yaml#53648738

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will look into this

Copy link
Author

@caleberi caleberi Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kubkon I checked the yaml specification and it seems y|Y|yes|Yes|YES|n|N|no|No|NO is a backwards-compatible support but it will be nice to have for this project.

I have gone ahead to include it in this PR. Hopefully, it is optimal enough. I was looking for a constant solution but so far this is what I have come up with


if (isTruthy) {
return Value{ .boolean = true };
}

if (isFalsy) {
return Value{ .boolean = false };
}

return Value{ .string = try arena.dupe(u8, value.string_value.items) };
} else {
log.debug("Unexpected node type: {}", .{node.tag});
Expand Down Expand Up @@ -377,6 +395,7 @@ pub const Yaml = struct {
},
.Struct => self.parseStruct(T, try value.asMap()),
.Union => self.parseUnion(T, value),
.Bool => value.asBool(),
.Array => self.parseArray(T, try value.asList()),
.Pointer => if (value.asList()) |list| {
return self.parsePointer(T, .{ .list = list });
Expand Down
2 changes: 2 additions & 0 deletions test/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ numbers:
- 10
- -8
- 6
isyaml: false
nested:
some: one
wick: john doe
ok: false
finally: [ 8.17,
19.78 , 17 ,
21 ]
4 changes: 4 additions & 0 deletions test/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ test "simple" {
nested: struct {
some: []const u8,
wick: []const u8,
ok: bool,
},
finally: [4]f16,
isyaml: bool,

pub fn eql(self: @This(), other: @This()) bool {
if (self.names.len != other.names.len) return false;
Expand Down Expand Up @@ -61,7 +63,9 @@ test "simple" {
.nested = .{
.some = "one",
.wick = "john doe",
.ok = false,
},
.isyaml = false,
.finally = [_]f16{ 8.17, 19.78, 17, 21 },
};
try testing.expect(result.eql(expected));
Expand Down