authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-22 15:37:04 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-22 15:37:04 -07:00
log7f0e661371d41cfdc8b1649ed00e7a750c82e57a
tree2d0b39a01753c9999eafb9254593e7c08d54a6ae
parent3ea23e8ae3798d3929b4c4b54ce739a67914f3ec

add Value.getT which forwards the requested type into the optional

this will make using it in conjunction with orelse much easier

2 files changed, 12 insertions(+), 0 deletions(-)

src/lib.zig+9
......@@ -1,4 +1,5 @@
11const std = @import("std");
2const extras = @import("extras");
23
34pub const Value = union(enum) {
45 Object: []Member,
......@@ -34,6 +35,14 @@ pub const Value = union(enum) {
3435 return self.fetch_inner(query, 0);
3536 }
3637
38 pub fn getT(self: Value, query: anytype, comptime ty: std.meta.FieldEnum(Value)) ?extras.FieldType(Value, ty) {
39 if (self.get(query)) |val| {
40 if (val == .Null) return null;
41 return @field(val, @tagName(ty));
42 }
43 return null;
44 }
45
3746 fn fetch_inner(self: Value, query: anytype, comptime n: usize) ?Value {
3847 if (query.len == n) {
3948 return self;
zig.mod+3
......@@ -3,5 +3,8 @@ name: json
33main: src/lib.zig
44license: MIT
55description: "A JSON library for inspecting arbitrary values"
6dependencies:
7 - src: git https://github.com/nektro/zig-extras
8
69dev_dependencies:
710 - src: git https://github.com/truemedian/zfetch