graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
refactor: extract shared JSON getters into util.zig (#123)
justrach9e5591a2b14e
diff --git a/src/cube.zig b/src/cube.zigindex 22ee554b..dddc38b2 100644--- a/src/cube.zig+++ b/src/cube.zig@@ -14,8 +14,9 @@ const ansi = @import("ansi.zig");const style = &ansi.style;const root = @import("main.zig");-const strFieldObj = root.strFieldObj;-const intFieldObj = root.intFieldObj;+const util = @import("util.zig");+const strFieldObj = util.strFieldObj;+const intFieldObj = util.intFieldObj;const codegraff_device_base = root.codegraff_device_base;const harness_version = root.harness_version;diff --git a/src/main.zig b/src/main.zigindex 87a63473..d8fca50f 100644--- a/src/main.zig+++ b/src/main.zig@@ -135,6 +135,12 @@ const pricing = @import("pricing.zig");const ModelPrice = pricing.ModelPrice;const price_table = pricing.price_table;const priceFor = pricing.priceFor;++// Pure shared helpers (JSON ObjectMap getters) live in util.zig (#123). Aliased+// so the ~50 existing strFieldObj/intFieldObj call sites stay unqualified.+const util = @import("util.zig");+const strFieldObj = util.strFieldObj;+const intFieldObj = util.intFieldObj;const Billing = pricing.Billing;const billingFor = pricing.billingFor;const usdFor = pricing.usdFor;@@ -157,6 +163,7 @@ test {_ = pricing;_ = ansi;_ = serve;+ _ = util;}/// Wire format + auth style + endpoint per provider. Base URLs and env-var@@ -9107,10 +9114,6 @@ fn codexLogin(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, refrestry out.flush();}-pub fn strFieldObj(obj: std.json.ObjectMap, name: []const u8) ?[]const u8 {- const v = obj.get(name) orelse return null;- return if (v == .string) v.string else null;-}/// Extract a query-string parameter from an HTTP request line ("GET /p?k=v…").fn queryParam(req_line: []const u8, key: []const u8) ?[]const u8 {@@ -9414,10 +9417,6 @@ fn httpJsonPost(io: Io, gpa: Allocator, arena: Allocator, url: []const u8, body:return v.object;}-pub fn intFieldObj(obj: std.json.ObjectMap, name: []const u8, default: i64) i64 {- const v = obj.get(name) orelse return default;- return if (v == .integer) v.integer else default;-}fn codegraffLogin(io: Io, gpa: Allocator, arena: Allocator, home: []const u8) !void {var obuf: [4096]u8 = undefined;@@ -16714,17 +16713,6 @@ test "strField/intField: typed JSON field extraction, null on wrong type or non-try std.testing.expect(intField(v, "missing") == null);}-test "strFieldObj/intFieldObj: object-map variants with defaults" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const a = arena_state.allocator();- const v = std.json.parseFromSliceLeaky(Value, a, "{\"s\":\"hi\",\"n\":42}", .{}) catch unreachable;- try std.testing.expectEqualStrings("hi", strFieldObj(v.object, "s").?);- try std.testing.expect(strFieldObj(v.object, "n") == null);- try std.testing.expectEqual(@as(i64, 42), intFieldObj(v.object, "n", -1));- try std.testing.expectEqual(@as(i64, -1), intFieldObj(v.object, "s", -1)); // wrong type -> default- try std.testing.expectEqual(@as(i64, -1), intFieldObj(v.object, "missing", -1));-}test "b64url: url-safe base64 without padding" {var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);diff --git a/src/util.zig b/src/util.zignew file mode 100644index 00000000..3a99da7e--- /dev/null+++ b/src/util.zig@@ -0,0 +1,30 @@+//! Small pure helpers shared across modules — currently the two JSON+//! ObjectMap getters used by the gateway/cube CLI, the OAuth flows, and the+//! trajectory renderer. Leaf module: std only. Split out of main.zig (#123).++const std = @import("std");+const Value = std.json.Value;++/// Read a string field from a JSON object, or null if absent/non-string.+pub fn strFieldObj(obj: std.json.ObjectMap, name: []const u8) ?[]const u8 {+ const v = obj.get(name) orelse return null;+ return if (v == .string) v.string else null;+}++/// Read an integer field from a JSON object, or `default` if absent/non-integer.+pub fn intFieldObj(obj: std.json.ObjectMap, name: []const u8, default: i64) i64 {+ const v = obj.get(name) orelse return default;+ return if (v == .integer) v.integer else default;+}++test "strFieldObj/intFieldObj: object-map variants with defaults" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ const v = std.json.parseFromSliceLeaky(Value, a, "{\"s\":\"hi\",\"n\":42}", .{}) catch unreachable;+ try std.testing.expectEqualStrings("hi", strFieldObj(v.object, "s").?);+ try std.testing.expect(strFieldObj(v.object, "n") == null);+ try std.testing.expectEqual(@as(i64, 42), intFieldObj(v.object, "n", -1));+ try std.testing.expectEqual(@as(i64, -1), intFieldObj(v.object, "s", -1)); // wrong type -> default+ try std.testing.expectEqual(@as(i64, -1), intFieldObj(v.object, "missing", -1));+}