Skip to content

Commit 7073005

Browse files
authored
chore: Make std.setUnion accept str. (#480)
Motivation: Make std.setUnion accept str.
1 parent 49358f6 commit 7073005

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

build.mill

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ object sjsonnet extends VersionFileModule {
203203

204204
val osName = System.getProperty("os.name").toLowerCase
205205

206-
def nativeLinkingOptions = super.nativeLinkingOptions() ++ (if (osName == "linux") Seq("-static") else Seq())
206+
def nativeLinkingOptions =
207+
super.nativeLinkingOptions() ++ (if (osName == "linux") Seq("-static") else Seq())
207208

208209
def releaseMode = ReleaseMode.ReleaseFull
209210
def nativeLTO = LTO.Full

sjsonnet/src/sjsonnet/Std.scala

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,8 +1707,8 @@ class Std(
17071707
builtin(Set_),
17081708
builtinWithDefaults("setUnion", "a" -> null, "b" -> null, "keyF" -> Val.False(dummyPos)) {
17091709
(args, pos, ev) =>
1710-
val a = toSetArr(args, 0, pos, ev)
1711-
val b = toSetArr(args, 1, pos, ev)
1710+
val a = toArrOrString(args(0), pos, ev)
1711+
val b = toArrOrString(args(1), pos, ev)
17121712
if (a.isEmpty) {
17131713
uniqArr(pos, ev, sortArr(pos, ev, args(1), args(2)), args(2))
17141714
} else if (b.isEmpty) {
@@ -1948,13 +1948,6 @@ class Std(
19481948
builtin(Native)
19491949
)
19501950

1951-
private def toSetArr(args: Array[Val], idx: Int, pos: Position, ev: EvalScope) = {
1952-
args(idx) match {
1953-
case arr: Val.Arr => arr.asLazyArray
1954-
case _ => Error.fail(f"Argument $idx must be an array", pos)(ev)
1955-
}
1956-
}
1957-
19581951
private def toArrOrString(arg: Val, pos: Position, ev: EvalScope) = {
19591952
arg match {
19601953
case arr: Val.Arr => arr.asLazyArray
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package sjsonnet
2+
3+
import sjsonnet.TestUtils.eval
4+
import utest.*
5+
6+
object StdSetUnionTests extends TestSuite {
7+
8+
def tests: Tests = Tests {
9+
test {
10+
eval("""
11+
|{
12+
| local options = ['a', 'b', 'c'],
13+
| union: std.setUnion('d', options),
14+
|}
15+
|""".stripMargin).toString ==> """{"union":["a","b","c","d"]}"""
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)