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

Add objectValues and objectValuesAll to stdlib #104

Merged
merged 1 commit into from
Dec 16, 2020
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
60 changes: 42 additions & 18 deletions sjsonnet/src/sjsonnet/Std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,20 @@ object Std {
v1.getVisibleKeys().get(v2).isDefined
},
builtin("objectFields", "o"){ (ev, fs, v1: Val.Obj) =>
val keys = v1.getVisibleKeys()
.collect{case (k, false) => k}
.toSeq
val maybeSorted = if(ev.preserveOrder) {
keys
} else {
keys.sorted
}
Val.Arr(maybeSorted.map(k => Val.Lazy(Val.Str(k))))
val keys = getVisibleKeys(ev, v1)
Val.Arr(keys.map(k => Val.Lazy(Val.Str(k))))
},
builtin("objectFieldsAll", "o"){ (ev, fs, v1: Val.Obj) =>
val keys = v1.getVisibleKeys()
.collect{case (k, _) => k}
.toSeq
val maybeSorted = if(ev.preserveOrder) {
keys
} else {
keys.sorted
}
Val.Arr(maybeSorted.map(k => Val.Lazy(Val.Str(k))))
val keys = getAllKeys(ev, v1)
Val.Arr(keys.map(k => Val.Lazy(Val.Str(k))))
},
builtin("objectValues", "o"){ (ev, fs, v1: Val.Obj) =>
val keys = getVisibleKeys(ev, v1)
getObjValuesFromKeys(ev, fs, v1, keys)
},
builtin("objectValuesAll", "o"){ (ev, fs, v1: Val.Obj) =>
val keys = getAllKeys(ev, v1)
getObjValuesFromKeys(ev, fs, v1, keys)
},
builtin("type", "x"){ (ev, fs, v1: Val) =>
v1 match{
Expand Down Expand Up @@ -1019,4 +1013,34 @@ object Std {
val output = str.toSeq.sliding(1).toList
Val.Arr(output.map(s => Val.Lazy(Val.Str(s.toString()))).toSeq)
}

def getVisibleKeys(ev: EvalScope, v1: Val.Obj): Seq[String] = {
val keys = v1.getVisibleKeys()
.collect{case (k, false) => k}
.toSeq

maybeSortKeys(ev, keys)
}

def getAllKeys(ev: EvalScope, v1: Val.Obj): Seq[String] = {
val keys = v1.getVisibleKeys()
.collect{case (k, _) => k}
.toSeq

maybeSortKeys(ev, keys)
}

def maybeSortKeys(ev: EvalScope, keys: Seq[String]): Seq[String] = {
if(ev.preserveOrder) {
keys
} else {
keys.sorted
}
}

def getObjValuesFromKeys(ev: EvalScope, fs: FileScope, v1: Val.Obj, keys: Seq[String]): Val.Arr = {
Val.Arr(keys.map { k =>
Val.Lazy(v1.value(k, -1)(fs, ev))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit unsure about the offset argument here, but saw -1 used elsewhere. Let me know if it should be different.

})
}
}
27 changes: 27 additions & 0 deletions sjsonnet/test/resources/test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,33 @@ std.assertEqual(std.objectFields({ x::: 1 } { x: 1 }), ['x']) &&
std.assertEqual(std.objectFields({ x::: 1 } { x:: 1 }), []) &&
std.assertEqual(std.objectFields({ x::: 1 } { x::: 1 }), ['x']) &&

std.assertEqual(std.objectValues({}), []) &&
std.assertEqual(std.objectValues({ x: 1, y: 2 }), [1, 2]) &&
std.assertEqual(std.objectValues({ a: 1, b: 2, c: null}), [1, 2, null]) &&
std.assertEqual(std.objectValues({ x: 1 } { x: 1 }), [1]) &&
std.assertEqual(std.objectValues({ x: 1 } { x:: 1 }), []) &&
std.assertEqual(std.objectValues({ x: 1 } { x::: 1 }), [1]) &&
std.assertEqual(std.objectValues({ x:: 1 } { x: 1 }), []) &&
std.assertEqual(std.objectValues({ x:: 1 } { x:: 1 }), []) &&
std.assertEqual(std.objectValues({ x:: 1 } { x::: 1 }), [1]) &&
std.assertEqual(std.objectValues({ x::: 1 } { x: 1 }), [1]) &&
std.assertEqual(std.objectValues({ x::: 1 } { x:: 1 }), []) &&
std.assertEqual(std.objectValues({ x::: 1 } { x::: 1 }), [1]) &&
std.assertEqual(std.objectValues({ a: 1, b: "2", c: [1, 2], d: {x: 1, y: [2, 3]}}), [1, "2", [1, 2], {x: 1, y: [2, 3]}]) &&

std.assertEqual(std.objectValuesAll({}), []) &&
std.assertEqual(std.objectValuesAll({ x: 1, y: 2 }), [1, 2]) &&
std.assertEqual(std.objectValuesAll({ a: 1, b: 2, c: null}), [1, 2, null]) &&
std.assertEqual(std.objectValuesAll({ x: 1 } { x: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ x: 1 } { x:: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ x: 1 } { x::: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ x:: 1 } { x: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ x:: 1 } { x:: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ x:: 1 } { x::: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ x::: 1 } { x: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ x::: 1 } { x:: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ x::: 1 } { x::: 1 }), [1]) &&
std.assertEqual(std.objectValuesAll({ a: 1, b: "2", c: [1, 2], d: {x: 1, y: [2, 3]}}), [1, "2", [1, 2], {x: 1, y: [2, 3]}]) &&

std.assertEqual(std.toString({ a: 1, b: 2 }), '{"a": 1, "b": 2}') &&
std.assertEqual(std.toString({}), '{ }') &&
Expand Down