Skip to content

Commit

Permalink
Removing manual trim from various functions (string, int, bool) and c…
Browse files Browse the repository at this point in the history
…reated helper function to trim values at a higher level. Adjusted tests to specifically test spaces
  • Loading branch information
BitlyTwiser committed Sep 15, 2024
1 parent 3aa6d78 commit f20a082
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,7 @@ pub fn Ymlz(comptime Destination: type) type {

fn parseStringExpression(self: *Self, raw_line: []const u8, depth: usize, is_multiline: bool) ![]const u8 {
const expression = try self.parseSimpleExpression(raw_line, depth, is_multiline);
var value = self.getExpressionValue(expression);

// Trim spaces to avoid errors when placing spaces after strings in the yaml
value = std.mem.trim(u8, value, " ");
const value = self.getExpressionValueWithTrim(expression);

if (value.len == 0) return value;

Expand Down Expand Up @@ -420,6 +417,17 @@ pub fn Ymlz(comptime Destination: type) type {
return str;
}

fn getExpressionValueWithTrim(self: *Self, expression: Expression) []const u8 {
_ = self;

// Trim spaces from value
return std.mem.trim(u8, switch (expression.value) {
.Simple => expression.value.Simple,
.KV => expression.value.KV.value,
else => @panic("Not implemeted for " ++ @typeName(@TypeOf(expression.value))),
}, " ");
}

fn getExpressionValue(self: *Self, expression: Expression) []const u8 {
_ = self;

Expand All @@ -432,10 +440,7 @@ pub fn Ymlz(comptime Destination: type) type {

fn parseBooleanExpression(self: *Self, raw_line: []const u8, depth: usize) !bool {
const expression = try self.parseSimpleExpression(raw_line, depth, false);
var value = self.getExpressionValue(expression);

// Trim spaces to avoid errors when placing spaces after strings in the yaml
value = std.mem.trim(u8, value, " ");
const value = self.getExpressionValueWithTrim(expression);

const isBooleanTrue = std.mem.eql(u8, value, "True") or std.mem.eql(u8, value, "true") or std.mem.eql(u8, value, "On") or std.mem.eql(u8, value, "on");

Expand All @@ -454,9 +459,7 @@ pub fn Ymlz(comptime Destination: type) type {

fn parseNumericExpression(self: *Self, comptime T: type, raw_line: []const u8, depth: usize) !T {
const expression = try self.parseSimpleExpression(raw_line, depth, false);
var value = self.getExpressionValue(expression);

value = std.mem.trim(u8, value, " ");
const value = self.getExpressionValueWithTrim(expression);

switch (@typeInfo(T)) {
.Int => {
Expand Down
3 changes: 3 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ test "Multiple elements in yaml file" {
try expect(result.elements[0].bool_val == true);
try expect(std.mem.eql(u8, result.elements[0].name, "Example Name"));

// Ensure 1st element does *not* have spaces (A space is manually entered into the yaml file)
try expect(!std.mem.eql(u8, result.elements[0].name, "Example Name "));

// Test 2nd element
try expect(result.elements[1].bool_val == false);
try expect(std.mem.eql(u8, result.elements[1].name, "Example Name 2"));
Expand Down

0 comments on commit f20a082

Please sign in to comment.