Skip to content

Commit aa88f44

Browse files
Fix a bug in the strict implementation of setUnion (#242)
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.
1 parent 7160a49 commit aa88f44

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

sjsonnet/src/sjsonnet/Std.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,10 +1323,10 @@ class Std {
13231323
builtinWithDefaults("setUnion", "a" -> null, "b" -> null, "keyF" -> Val.False(dummyPos)) { (args, pos, ev) =>
13241324
val a = toSetArrOrString(args, 0, pos, ev)
13251325
val b = toSetArrOrString(args, 1, pos, ev)
1326-
if (ev.settings.strictSetOperations && a.length == 0) {
1327-
args(0)
1328-
} else if (ev.settings.strictSetOperations && b.length == 0) {
1329-
args(1)
1326+
if (a.isEmpty) {
1327+
uniqArr(pos, ev, sortArr(pos, ev, args(1), args(2)), args(2))
1328+
} else if (b.isEmpty) {
1329+
uniqArr(pos, ev, sortArr(pos, ev, args(0), args(2)), args(2))
13301330
} else {
13311331
val concat = new Val.Arr(pos, a ++ b)
13321332
uniqArr(pos, ev, sortArr(pos, ev, concat, args(2)), args(2))
@@ -1656,8 +1656,11 @@ class Std {
16561656
}
16571657
}
16581658

1659-
private def uniqArr(pos: Position, ev: EvalScope, arr: Val, keyF: Val) = {
1659+
private def uniqArr(pos: Position, ev: EvalScope, arr: Val, keyF: Val): Val = {
16601660
val arrValue = toArrOrString(arr, pos, ev)
1661+
if (arrValue.length <= 1) {
1662+
return arr
1663+
}
16611664

16621665
val out = new mutable.ArrayBuffer[Lazy]
16631666
for (v <- arrValue) {

sjsonnet/test/src/sjsonnet/StdWithKeyFTests.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ object StdWithKeyFTests extends TestSuite {
157157
eval("std.setUnion(std.set([\"c\", \"c\", \"b\"]), std.set([\"b\", \"b\", \"a\", \"b\", \"a\"]))").toString() ==>
158158
"""["a","b","c"]"""
159159

160+
eval("std.setUnion(std.set([]), std.set([\"b\", \"b\", \"a\", \"b\", \"a\"]))").toString() ==>
161+
"""["a","b"]"""
162+
163+
eval("std.setUnion(std.set([\"c\", \"c\", \"b\"]), std.set([]))").toString() ==>
164+
"""["b","c"]"""
165+
160166
eval(
161167
"""local arr1 = std.set([
162168
{

0 commit comments

Comments
 (0)