Make getParams return nullable

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-04-18 12:10:20 +02:00
parent e4ae2df1a4
commit 43a558f5ae
2 changed files with 13 additions and 5 deletions

View File

@@ -67,7 +67,6 @@ fn browserSetDownloadBehavior(
downloadPath: ?[]const u8 = null,
eventsEnabled: ?bool = null,
};
const params = try getParams(alloc, Params, scanner);
std.log.debug("params {any}", .{params});
_ = try getParams(alloc, Params, scanner);
return result(alloc, id, null, null, null);
}

View File

@@ -125,13 +125,22 @@ pub fn getParams(
alloc: std.mem.Allocator,
comptime T: type,
scanner: *std.json.Scanner,
) !T {
try checkKey("params", (try scanner.next()).string);
) !?T {
// if next token is the end of the object, there is no "params"
const t = try scanner.next();
if (t == .object_end) return null;
// if next token is not "params" there is no "params"
if (!std.mem.eql(u8, "params", t.string)) return null;
// parse "params"
const options = std.json.ParseOptions{
.max_value_len = scanner.input.len,
.allocate = .alloc_if_needed,
};
return std.json.innerParse(T, alloc, scanner, options);
const params = try std.json.innerParse(T, alloc, scanner, options);
return params;
}
pub fn getSessionID(scanner: *std.json.Scanner) !?[]const u8 {