Skip to content
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
5 changes: 5 additions & 0 deletions sjsonnet/src-jvm-native/sjsonnet/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final case class Config(
doc = "Write output as a YAML stream of JSON documents"
)
yamlStream: Flag = Flag(),
@arg(
name = "legacy-yaml-stream",
doc = "Use the pre-0.7.2 --yaml-stream formatting (implies --yaml-stream)"
)
legacyYamlStream: Flag = Flag(),
@arg(
name = "string",
short = 'S',
Expand Down
43 changes: 42 additions & 1 deletion sjsonnet/src-jvm-native/sjsonnet/SjsonnetMainBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ object SjsonnetMainBase {
)
.left
.map(_ + envVarsDoc)
config = normalizedArgs.forcedFile.fold(config0)(f => config0.copy(file = f))
config1 = normalizedArgs.forcedFile.fold(config0)(f => config0.copy(file = f))
// --legacy-yaml-stream implies --yaml-stream.
config =
if (config1.legacyYamlStream.value) config1.copy(yamlStream = mainargs.Flag(true))
else config1
_ <- {
if (config.noTrailingNewline.value && config.yamlStream.value)
Left("ERROR: cannot use --no-trailing-newline with --yaml-stream")
Expand Down Expand Up @@ -530,6 +534,8 @@ object SjsonnetMainBase {
}
}

private def isScalar(v: ujson.Value) = !v.isInstanceOf[ujson.Arr] && !v.isInstanceOf[ujson.Obj]

private def jsonTypeName(v: ujson.Value): String = v match {
case _: ujson.Obj => "object"
case _: ujson.Arr => "array"
Expand Down Expand Up @@ -741,6 +747,41 @@ object SjsonnetMainBase {
"whose keys are filenames and values hold the JSON for that file."
)
}
case (None, true) if config.legacyYamlStream.value =>
// Pre-0.7.2 --yaml-stream formatting. Retained behind --legacy-yaml-stream:
// array elements render as YAML documents (not JSON), and a non-array
// top-level falls back to normal rendering instead of erroring.
interp.interpret(jsonnetCode, path).flatMap {
case arr: ujson.Arr =>
writeToFile(config, wd) { writer =>
handleRenderError(config.maxTrace) {
arr.value.toSeq match {
case Nil => // do nothing
case Seq(single) =>
val renderer = rendererForConfig(writer, config, () => currentPos)
single.transform(renderer)
writer.write(if (isScalar(single)) "\n..." else "")
case multiple =>
for ((v, i) <- multiple.zipWithIndex) {
if (i > 0) writer.write('\n')
if (isScalar(v)) writer.write("--- ")
else if (i != 0) writer.write("---\n")
val renderer = rendererForConfig(
writer,
config.copy(yamlOut = mainargs.Flag(true)),
() => currentPos
)
v.transform(renderer)
}
}
writer.write('\n')
""
}
}

case _ =>
renderNormal(config, interp, jsonnetCode, path, wd, () => currentPos, stdoutStream)
}
case (None, true) =>
// YAML stream (--no-trailing-newline is already rejected above for yaml-stream)

Expand Down
19 changes: 19 additions & 0 deletions sjsonnet/test/src-jvm/sjsonnet/MainTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,25 @@ object MainTests extends TestSuite {
assert(err.contains("stream mode: top-level object was a object"))
}

test("legacyYamlStreamUsesPre072Formatting") {
// Array elements render as YAML documents, not JSON documents.
val (res, out, err) = runMain("[{a: 1}, {b: 2}]", "--exec", "--legacy-yaml-stream")
assert((res, out, err) == ((0, "a: 1\n---\nb: 2\n", "")))
}

test("legacyYamlStreamFallsBackForNonArrayTopLevel") {
// Unlike --yaml-stream, a non-array top-level renders normally instead of erroring.
val (res, out, err) = runMain("{a: 1}", "--exec", "--legacy-yaml-stream")
assert((res, out, err) == ((0, "{\n \"a\": 1\n}\n", "")))
}

test("legacyYamlStreamImpliesYamlStream") {
// --legacy-yaml-stream is exclusive with --no-trailing-newline, just like --yaml-stream.
val (res, out, err) = runMain("[{a: 1}]", "--exec", "--legacy-yaml-stream", "--no-trailing-newline")
assert(res == 1)
assert(err.contains("cannot use --no-trailing-newline with --yaml-stream"))
}

// -- No trailing newline behavior --

test("noTrailingNewline") {
Expand Down
Loading