Skip to content

Commit

Permalink
accomodate Zig changes to std.builtin.Type
Browse files Browse the repository at this point in the history
  • Loading branch information
karlseguin committed Aug 29, 2024
1 parent 0a8d45f commit 07473a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
.version = "0.0.0",
.dependencies = .{
.metrics = .{
.url = "https://github.com/karlseguin/metrics.zig/archive/8892dce43b46a69c8e736329629f38df232adee1.tar.gz",
.hash = "1220232ab38d0c2cfb10680115c17ad2fa1a8531dbaf8bbfb359ec67e80c7d5f5758"
.url = "https://github.com/karlseguin/metrics.zig/archive/ca494b536787e95e5290527c0b30b82eff2d5d38.tar.gz",
.hash = "122084a9d77d05c33b1a559882e34a018e2c8d4d1a017ccebe5a8c956c16d545fd0a"
},
},
}
30 changes: 22 additions & 8 deletions src/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ pub const Json = struct {

pub fn int(self: *Json, key: []const u8, value: anytype) void {
const f = switch (@typeInfo(@TypeOf(value))) {
.Optional => blk: {
.optional => blk: {
if (value) |v| {
break :blk v;
}
self.writeNull(key);
return;
},
.Null => {
.@"null" => {
self.writeNull(key);
return;
},
Expand All @@ -182,14 +182,14 @@ pub const Json = struct {

pub fn float(self: *Json, key: []const u8, value: anytype) void {
const f = switch (@typeInfo(@TypeOf(value))) {
.Optional => blk: {
.optional => blk: {
if (value) |v| {
break :blk v;
}
self.writeNull(key);
return;
},
.Null => {
.@"null" => {
self.writeNull(key);
return;
},
Expand All @@ -207,14 +207,14 @@ pub const Json = struct {

pub fn boolean(self: *Json, key: []const u8, value: anytype) void {
const b = switch (@typeInfo(@TypeOf(value))) {
.Optional => blk: {
.optional => blk: {
if (value) |v| {
break :blk v;
}
self.writeNull(key);
return;
},
.Null => {
.@"null" => {
self.writeNull(key);
return;
},
Expand Down Expand Up @@ -270,7 +270,7 @@ pub const Json = struct {
const T = @TypeOf(value);

switch (@typeInfo(T)) {
.Optional => {
.optional => {
if (value) |v| {
self.string("@err", @errorName(v));
} else {
Expand All @@ -285,7 +285,7 @@ pub const Json = struct {
const T = @TypeOf(value);

switch (@typeInfo(T)) {
.Optional => {
.optional => {
if (value) |v| {
self.string(key, @errorName(v));
} else {
Expand Down Expand Up @@ -818,6 +818,18 @@ test "json: src doesn't fit" {
try expectLog(&json, null);
}


test "json: tabs" {
const p = try Pool.init(t.allocator, .{ .pool_size = 1, .encoding = .json, .large_buffer_count = 1, .large_buffer_size = 20, .buffer_size = 10 });
defer p.deinit();

var json = try Json.init(t.allocator, p);
defer json.deinit(t.allocator);

json.string("key1", "key_with_tab\t");
try expectLog(&json, "\"key1\":\"key_with_tab\\t\"");
}

test "json: fmt" {
const p = try Pool.init(t.allocator, .{ .pool_size = 1, .encoding = .json, .large_buffer_count = 1, .large_buffer_size = 20, .buffer_size = 10 });
defer p.deinit();
Expand Down Expand Up @@ -875,3 +887,5 @@ fn expectFmt(json: *Json, comptime fmt: []const u8, args: anytype) !void {
const expected = try std.fmt.bufPrint(&buf, "{{\"@ts\":9999999999999," ++ fmt ++ "}}\n", args);
try t.expectString(expected, out.items);
}


16 changes: 8 additions & 8 deletions src/logfmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ pub const LogFmt = struct {

pub fn int(self: *LogFmt, key: []const u8, value: anytype) void {
const n = switch (@typeInfo(@TypeOf(value))) {
.Optional => blk: {
.optional => blk: {
if (value) |v| {
break :blk v;
}
self.writeNull(key);
return;
},
.Null => {
.@"null" => {
self.writeNull(key);
return;
},
Expand All @@ -188,14 +188,14 @@ pub const LogFmt = struct {

pub fn float(self: *LogFmt, key: []const u8, value: anytype) void {
const f = switch (@typeInfo(@TypeOf(value))) {
.Optional => blk: {
.optional => blk: {
if (value) |v| {
break :blk v;
}
self.writeNull(key);
return;
},
.Null => {
.@"null" => {
self.writeNull(key);
return;
},
Expand All @@ -213,14 +213,14 @@ pub const LogFmt = struct {

pub fn boolean(self: *LogFmt, key: []const u8, value: anytype) void {
const b = switch (@typeInfo(@TypeOf(value))) {
.Optional => blk: {
.optional => blk: {
if (value) |v| {
break :blk v;
}
self.writeNull(key);
return;
},
.Null => {
.@"null" => {
self.writeNull(key);
return;
},
Expand Down Expand Up @@ -262,7 +262,7 @@ pub const LogFmt = struct {
const T = @TypeOf(value);

switch (@typeInfo(T)) {
.Optional => {
.optional => {
if (value) |v| {
self.string("@err", @errorName(v));
} else {
Expand All @@ -277,7 +277,7 @@ pub const LogFmt = struct {
const T = @TypeOf(value);

switch (@typeInfo(T)) {
.Optional => {
.optional => {
if (value) |v| {
self.string(key, @errorName(v));
} else {
Expand Down

0 comments on commit 07473a3

Please sign in to comment.