From 7f0e661371d41cfdc8b1649ed00e7a750c82e57a Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 22 Oct 2021 15:37:04 -0700 Subject: [PATCH] add Value.getT which forwards the requested type into the optional this will make using it in conjunction with orelse much easier --- src/lib.zig | 9 +++++++++ zig.mod | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/lib.zig b/src/lib.zig index 518722bf47079b63d04bece64f92bb103af4684e..8368ae6d760ea15b9aef18654c9331d773f926e9 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const extras = @import("extras"); pub const Value = union(enum) { Object: []Member, @@ -34,6 +35,14 @@ pub const Value = union(enum) { return self.fetch_inner(query, 0); } + pub fn getT(self: Value, query: anytype, comptime ty: std.meta.FieldEnum(Value)) ?extras.FieldType(Value, ty) { + if (self.get(query)) |val| { + if (val == .Null) return null; + return @field(val, @tagName(ty)); + } + return null; + } + fn fetch_inner(self: Value, query: anytype, comptime n: usize) ?Value { if (query.len == n) { return self; diff --git a/zig.mod b/zig.mod index 8a04c26bccdf0bd67ad32c52096e03a3b9b31287..0216bc33d122f1aef9d0e19d232b74616ffc9cd3 100644 --- a/zig.mod +++ b/zig.mod @@ -3,5 +3,8 @@ name: json main: src/lib.zig license: MIT description: "A JSON library for inspecting arbitrary values" +dependencies: + - src: git https://github.com/nektro/zig-extras + dev_dependencies: - src: git https://github.com/truemedian/zfetch -- 2.54.0