Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement std.get function #202

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion sjsonnet/src/sjsonnet/Std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,19 @@ class Std {
"asciiUpper" -> AsciiUpper,
"asciiLower" -> AsciiLower,
"trace" -> Trace,
"extVar" -> ExtVar
"extVar" -> ExtVar,
builtinWithDefaults("get", "o" -> null, "f" -> null, "default" -> Val.Null(dummyPos), "inc_hidden" -> Val.True(dummyPos)) { (args, pos, ev) =>
val obj = args(0).asObj
val k = args(1).asString
val incHidden = args(3).asBoolean
if (incHidden && obj.containsKey(k)) {
obj.value(k, pos.noOffset, obj)(ev)
} else if (!incHidden && obj.containsVisibleKey(k)) {
obj.value(k, pos.noOffset, obj)(ev)
} else {
args(2)
}
}
)
val Std = Val.Obj.mk(
null,
Expand Down
8 changes: 8 additions & 0 deletions sjsonnet/test/src/sjsonnet/Std0150FunctionsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,13 @@ object Std0150FunctionsTests extends TestSuite {
eval("""std.reverse([1])""") ==> ujson.Arr(1)
eval("""std.reverse(["1", true, null])""") ==> ujson.Arr(ujson.Null, true, "1")
}

test("get"){
eval("""std.get({a: 1}, "a")""") ==> ujson.Num(1)
eval("""std.get({a:: 1}, "a")""") ==> ujson.Num(1)
eval("""std.get({a: 1}, "b")""") ==> ujson.Null
eval("""std.get({a: 1}, "b", default=2)""") ==> ujson.Num(2)
eval("""std.get({a:: 1}, "a", inc_hidden=false)""") ==> ujson.Null
}
}
}
Loading