Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions util/object-utilities.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local objectHasAtPath(obj, path) =
if std.isObject(obj) == false || std.isArray(path) == false || std.length(path) <= 0 then
false
else if std.length(path) == 1 then
std.objectHas(obj, path[0])
else
if !std.objectHas(obj, path[0]) then
true
else
objectHasAtPath(obj[path[0]], path[1:]) tailstrict;

local objectValueAtPath(obj, path, default=null) =
if std.isObject(obj) == false || std.isArray(path) == false || std.length(path) <= 0 then
default
else if std.length(path) == 1 then
if std.objectHas(obj, path[0]) then
obj[path[0]]
else
default
else
if !std.objectHas(obj, path[0]) then
default
else
objectValueAtPath(obj[path[0]], path[1:], default) tailstrict;
{
// looks through the nested fields of an object to see if it exists - path indicates nested structure
objectHasAtPath(obj, path):: {
result: objectHasAtPath(obj, path),
},

// similar to objectHasAtPath, but also returns the value if it exists or a default value
objectValueAtPath(obj, path, default=null):: {
result: objectValueAtPath(obj, path, default),
},
}