Skip to content

Commit d6e89cf

Browse files
committed
Merge branch 'caleberi-add-support-for-yaml-bool'
2 parents 60ac814 + 5217da6 commit d6e89cf

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/yaml.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub const parse = @import("parse.zig");
1313
const Node = parse.Node;
1414
const Tree = parse.Tree;
1515
const ParseError = parse.ParseError;
16+
const supportedTruthyBooleanValue: [4][]const u8 = .{ "y", "yes", "on", "true" };
17+
const supportedFalsyBooleanValue: [4][]const u8 = .{ "n", "no", "off", "false" };
1618

1719
pub const YamlError = error{
1820
UnexpectedNodeType,
@@ -28,6 +30,7 @@ pub const Value = union(enum) {
2830
empty,
2931
int: i64,
3032
float: f64,
33+
boolean: bool,
3134
string: []const u8,
3235
list: List,
3336
map: Map,
@@ -47,6 +50,11 @@ pub const Value = union(enum) {
4750
return self.string;
4851
}
4952

53+
pub fn asBool(self: Value) !bool {
54+
if (self != .boolean) return error.TypeMismatch;
55+
return self.boolean;
56+
}
57+
5058
pub fn asList(self: Value) !List {
5159
if (self != .list) return error.TypeMismatch;
5260
return self.list;
@@ -68,6 +76,7 @@ pub const Value = union(enum) {
6876
.int => |int| return writer.print("{}", .{int}),
6977
.float => |float| return writer.print("{d}", .{float}),
7078
.string => |string| return writer.print("{s}", .{string}),
79+
.boolean => |bool_val| return writer.print("{}", .{bool_val}),
7180
.list => |list| {
7281
const len = list.len;
7382
if (len == 0) return;
@@ -190,6 +199,21 @@ pub const Value = union(enum) {
190199
return Value{ .float = float };
191200
}
192201

202+
if (raw.len <= 5 and raw.len > 0) {
203+
const lower_raw = try std.ascii.allocLowerString(arena, raw);
204+
for (supportedTruthyBooleanValue) |v| {
205+
if (std.mem.eql(u8, v, lower_raw)) {
206+
return Value{ .boolean = true };
207+
}
208+
}
209+
210+
for (supportedFalsyBooleanValue) |v| {
211+
if (std.mem.eql(u8, v, lower_raw)) {
212+
return Value{ .boolean = false };
213+
}
214+
}
215+
}
216+
193217
return Value{ .string = try arena.dupe(u8, value.string_value.items) };
194218
} else {
195219
log.debug("Unexpected node type: {}", .{node.tag});
@@ -370,6 +394,7 @@ pub const Yaml = struct {
370394
fn parseValue(self: *Yaml, comptime T: type, value: Value) Error!T {
371395
return switch (@typeInfo(T)) {
372396
.int => math.cast(T, try value.asInt()) orelse return error.Overflow,
397+
.bool => self.parseBoolean(bool, value),
373398
.float => if (value.asFloat()) |float| {
374399
return math.lossyCast(T, float);
375400
} else |_| {
@@ -389,6 +414,11 @@ pub const Yaml = struct {
389414
};
390415
}
391416

417+
fn parseBoolean(self: *Yaml, comptime T: type, value: Value) Error!T {
418+
_ = self;
419+
return value.asBool();
420+
}
421+
392422
fn parseUnion(self: *Yaml, comptime T: type, value: Value) Error!T {
393423
const union_info = @typeInfo(T).@"union";
394424

test/simple.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ numbers:
33
- 10
44
- -8
55
- 6
6+
isyaml: false
7+
hasBoolean: NO
68
nested:
79
some: one
810
wick: john doe
11+
ok: TRUE
912
finally: [ 8.17,
1013
19.78 , 17 ,
1114
21 ]

test/test.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ test "simple" {
2424
nested: struct {
2525
some: []const u8,
2626
wick: []const u8,
27+
ok: bool,
2728
},
2829
finally: [4]f16,
30+
isyaml: bool,
31+
hasBoolean: bool,
2932

3033
pub fn eql(self: @This(), other: @This()) bool {
3134
if (self.names.len != other.names.len) return false;
@@ -61,7 +64,10 @@ test "simple" {
6164
.nested = .{
6265
.some = "one",
6366
.wick = "john doe",
67+
.ok = true,
6468
},
69+
.isyaml = false,
70+
.hasBoolean = false,
6571
.finally = [_]f16{ 8.17, 19.78, 17, 21 },
6672
};
6773
try testing.expect(result.eql(expected));

0 commit comments

Comments
 (0)