Add children node to CDP Node representation

Add Node writer. Different CDP messages want different child depths. For now,
only support immediate children, but the new writer should make it easy to
support variable.
This commit is contained in:
Karl Seguin
2025-04-03 21:28:57 +08:00
parent af68b10c5d
commit 68d1be3b94
2 changed files with 45 additions and 2 deletions

View File

@@ -285,6 +285,7 @@ const TestContext = struct {
_ = self.client.?.serialized.orderedRemove(i);
return;
}
std.debug.print("Error not found. Expecting:\n{s}\n\nGot:\n", .{serialized});
for (self.client.?.serialized.items, 0..) |sent, i| {
std.debug.print("#{d}\n{s}\n\n", .{ i, sent });

View File

@@ -257,8 +257,7 @@ pub fn isEqualJson(a: anytype, b: anytype) !bool {
const aa = arena.allocator();
const a_value = try convertToJson(aa, a);
const b_value = try convertToJson(aa, b);
expectJsonValue(a_value, b_value) catch return false;
return true;
return isJsonValue(a_value, b_value);
}
fn convertToJson(arena: Allocator, value: anytype) !std.json.Value {
@@ -308,3 +307,46 @@ fn expectJsonValue(a: std.json.Value, b: std.json.Value) !void {
},
}
}
fn isJsonValue(a: std.json.Value, b: std.json.Value) bool {
if (std.mem.eql(u8, @tagName(a), @tagName(b)) == false) {
return false;
}
// at this point, we know that if a is an int, b must also be an int
switch (a) {
.null => return true,
.bool => return a.bool == b.bool,
.integer => return a.integer == b.integer,
.float => return a.float == b.float,
.number_string => return std.mem.eql(u8, a.number_string, b.number_string),
.string => return std.mem.eql(u8, a.string, b.string),
.array => {
const a_len = a.array.items.len;
const b_len = b.array.items.len;
if (a_len != b_len) {
return false;
}
for (a.array.items, b.array.items) |a_item, b_item| {
if (isJsonValue(a_item, b_item) == false) {
return false;
}
}
return true;
},
.object => {
var it = a.object.iterator();
while (it.next()) |entry| {
const key = entry.key_ptr.*;
if (b.object.get(key)) |b_item| {
if (isJsonValue(entry.value_ptr.*, b_item) == false) {
return false;
}
} else {
return false;
}
}
return true;
},
}
}