move newline removal to comptime
This commit is contained in:
parent
7849c5456b
commit
d76ace8670
1 changed files with 14 additions and 11 deletions
25
src/main.zig
25
src/main.zig
|
|
@ -195,6 +195,19 @@ fn startRecirculation(allocator: std.mem.Allocator, id_token: []const u8, serial
|
||||||
return result.status == .ok;
|
return result.status == .ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn removeNewlines(comptime input: []const u8) []const u8 {
|
||||||
|
comptime {
|
||||||
|
var result: [input.len]u8 = undefined;
|
||||||
|
var i: usize = 0;
|
||||||
|
for (input) |c| {
|
||||||
|
result[i] = if (c == '\n') ' ' else c;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
const final = result;
|
||||||
|
return &final;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Queries the device shadow to get current recirculation status
|
/// Queries the device shadow to get current recirculation status
|
||||||
fn getRecirculationStatus(allocator: std.mem.Allocator, id_token: []const u8, serial_number: []const u8) !json.Parsed(json.Value) {
|
fn getRecirculationStatus(allocator: std.mem.Allocator, id_token: []const u8, serial_number: []const u8) !json.Parsed(json.Value) {
|
||||||
var client = http.Client{ .allocator = allocator };
|
var client = http.Client{ .allocator = allocator };
|
||||||
|
|
@ -213,17 +226,7 @@ fn getRecirculationStatus(allocator: std.mem.Allocator, id_token: []const u8, se
|
||||||
\\}
|
\\}
|
||||||
;
|
;
|
||||||
|
|
||||||
var query_clean_buf: [query.len]u8 = undefined;
|
const query_clean = comptime removeNewlines(query);
|
||||||
// Remove newlines for JSON payload
|
|
||||||
const query_replacements = std.mem.replace(
|
|
||||||
u8,
|
|
||||||
query,
|
|
||||||
"\n",
|
|
||||||
" ",
|
|
||||||
query_clean_buf[0..],
|
|
||||||
);
|
|
||||||
const query_clean = query_clean_buf[0 .. query.len - ("\n".len * query_replacements)];
|
|
||||||
|
|
||||||
const body = try std.fmt.allocPrint(allocator,
|
const body = try std.fmt.allocPrint(allocator,
|
||||||
\\{{"query":"{s}","variables":{{"heater_serial_number":"{s}"}}}}
|
\\{{"query":"{s}","variables":{{"heater_serial_number":"{s}"}}}}
|
||||||
, .{ query_clean, serial_number });
|
, .{ query_clean, serial_number });
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue