Skip to content

Commit

Permalink
Fix a bug in the strict implementation of setUnion (#242)
Browse files Browse the repository at this point in the history
Relax setUnion strictness towards arguments: basically std.set() and
std.setUnion will be able to create a set.
Fix the bad bug where one of the argument was empty - we just returned
the empty set instead of the other one.
  • Loading branch information
stephenamar-db authored Dec 17, 2024
1 parent 7160a49 commit aa88f44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 8 additions & 5 deletions sjsonnet/src/sjsonnet/Std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1323,10 +1323,10 @@ class Std {
builtinWithDefaults("setUnion", "a" -> null, "b" -> null, "keyF" -> Val.False(dummyPos)) { (args, pos, ev) =>
val a = toSetArrOrString(args, 0, pos, ev)
val b = toSetArrOrString(args, 1, pos, ev)
if (ev.settings.strictSetOperations && a.length == 0) {
args(0)
} else if (ev.settings.strictSetOperations && b.length == 0) {
args(1)
if (a.isEmpty) {
uniqArr(pos, ev, sortArr(pos, ev, args(1), args(2)), args(2))
} else if (b.isEmpty) {
uniqArr(pos, ev, sortArr(pos, ev, args(0), args(2)), args(2))
} else {
val concat = new Val.Arr(pos, a ++ b)
uniqArr(pos, ev, sortArr(pos, ev, concat, args(2)), args(2))
Expand Down Expand Up @@ -1656,8 +1656,11 @@ class Std {
}
}

private def uniqArr(pos: Position, ev: EvalScope, arr: Val, keyF: Val) = {
private def uniqArr(pos: Position, ev: EvalScope, arr: Val, keyF: Val): Val = {
val arrValue = toArrOrString(arr, pos, ev)
if (arrValue.length <= 1) {
return arr
}

val out = new mutable.ArrayBuffer[Lazy]
for (v <- arrValue) {
Expand Down
6 changes: 6 additions & 0 deletions sjsonnet/test/src/sjsonnet/StdWithKeyFTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ object StdWithKeyFTests extends TestSuite {
eval("std.setUnion(std.set([\"c\", \"c\", \"b\"]), std.set([\"b\", \"b\", \"a\", \"b\", \"a\"]))").toString() ==>
"""["a","b","c"]"""

eval("std.setUnion(std.set([]), std.set([\"b\", \"b\", \"a\", \"b\", \"a\"]))").toString() ==>
"""["a","b"]"""

eval("std.setUnion(std.set([\"c\", \"c\", \"b\"]), std.set([]))").toString() ==>
"""["b","c"]"""

eval(
"""local arr1 = std.set([
{
Expand Down

0 comments on commit aa88f44

Please sign in to comment.