Skip to content

Commit

Permalink
Merge pull request #104 from DWvanGeest/add-objectValues
Browse files Browse the repository at this point in the history
Add objectValues and objectValuesAll to stdlib
  • Loading branch information
lihaoyi-databricks authored Dec 16, 2020
2 parents ec08cec + 2cbefde commit 71c654e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
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))
})
}
}
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

0 comments on commit 71c654e

Please sign in to comment.