From a4fee0b5725937f38f73300ed45899b726e723a4 Mon Sep 17 00:00:00 2001 From: Stephen Amar Date: Wed, 11 Dec 2024 18:37:17 -0800 Subject: [PATCH] Implement missing parameters to manifestJson (#232) Specifically newline & key_val_sep. --- sjsonnet/src/sjsonnet/BaseCharRenderer.scala | 5 +++-- sjsonnet/src/sjsonnet/Renderer.scala | 13 +++++++------ sjsonnet/src/sjsonnet/Std.scala | 8 ++++---- sjsonnet/test/resources/test_suite/stdlib.jsonnet | 14 +++++++------- .../resources/test_suite/stdlib_native.jsonnet | 14 +++++++------- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/sjsonnet/src/sjsonnet/BaseCharRenderer.scala b/sjsonnet/src/sjsonnet/BaseCharRenderer.scala index 414b6366..1108feb2 100644 --- a/sjsonnet/src/sjsonnet/BaseCharRenderer.scala +++ b/sjsonnet/src/sjsonnet/BaseCharRenderer.scala @@ -9,7 +9,8 @@ import upickle.core.{ArrVisitor, ObjVisitor} class BaseCharRenderer[T <: upickle.core.CharOps.Output] (out: T, indent: Int = -1, - escapeUnicode: Boolean = false) extends JsVisitor[T, T]{ + escapeUnicode: Boolean = false, + newline: Array[Char] = Array('\n')) extends JsVisitor[T, T]{ protected[this] val elemBuilder = new upickle.core.CharBuilder protected[this] val unicodeCharBuilder = new upickle.core.CharBuilder() def flushCharBuilder() = { @@ -154,7 +155,7 @@ class BaseCharRenderer[T <: upickle.core.CharOps.Output] else { var i = indent * depth elemBuilder.ensureLength(i + 1) - elemBuilder.appendUnsafe('\n') + elemBuilder.appendAll(newline, newline.length) while(i > 0) { elemBuilder.appendUnsafe(' ') i -= 1 diff --git a/sjsonnet/src/sjsonnet/Renderer.scala b/sjsonnet/src/sjsonnet/Renderer.scala index 7c70da8e..68bb58fe 100644 --- a/sjsonnet/src/sjsonnet/Renderer.scala +++ b/sjsonnet/src/sjsonnet/Renderer.scala @@ -178,8 +178,10 @@ class PythonRenderer(out: Writer = new java.io.StringWriter(), } /** Renderer used by std.manifestJson, std.manifestJsonMinified, and std.manifestJsonEx */ -case class MaterializeJsonRenderer(indent: Int = 4, escapeUnicode: Boolean = false, out: StringWriter = new StringWriter()) - extends BaseCharRenderer(out, indent, escapeUnicode) { +case class MaterializeJsonRenderer(indent: Int = 4, escapeUnicode: Boolean = false, out: StringWriter = new StringWriter(), newline: String = "\n", keyValueSeparator: String = ": ") + extends BaseCharRenderer(out, indent, escapeUnicode, newline.toCharArray) { + private val newLineCharArray = newline.toCharArray + private val keyValueSeparatorCharArray = keyValueSeparator.toCharArray override def visitArray(length: Int, index: Int) = new ArrVisitor[StringWriter, StringWriter] { flushBuffer() @@ -187,7 +189,7 @@ case class MaterializeJsonRenderer(indent: Int = 4, escapeUnicode: Boolean = fal depth += 1 // account for rendering differences of whitespaces in ujson and jsonnet manifestJson - if (length == 0 && indent != -1) elemBuilder.append('\n') else renderIndent() + if (length == 0 && indent != -1) elemBuilder.appendAll(newLineCharArray, newLineCharArray.length) else renderIndent() def subVisitor = MaterializeJsonRenderer.this def visitValue(v: StringWriter, index: Int): Unit = { flushBuffer() @@ -208,12 +210,11 @@ case class MaterializeJsonRenderer(indent: Int = 4, escapeUnicode: Boolean = fal elemBuilder.append('{') depth += 1 // account for rendering differences of whitespaces in ujson and jsonnet manifestJson - if (length == 0 && indent != -1) elemBuilder.append('\n') else renderIndent() + if (length == 0 && indent != -1) elemBuilder.appendAll(newLineCharArray, newLineCharArray.length) else renderIndent() def subVisitor = MaterializeJsonRenderer.this def visitKey(index: Int) = MaterializeJsonRenderer.this def visitKeyValue(s: Any): Unit = { - elemBuilder.append(':') - if (indent != -1) elemBuilder.append(' ') + elemBuilder.appendAll(keyValueSeparatorCharArray, keyValueSeparatorCharArray.length) } def visitValue(v: StringWriter, index: Int): Unit = { commaBuffered = true diff --git a/sjsonnet/src/sjsonnet/Std.scala b/sjsonnet/src/sjsonnet/Std.scala index 588d8d83..dbe62b01 100644 --- a/sjsonnet/src/sjsonnet/Std.scala +++ b/sjsonnet/src/sjsonnet/Std.scala @@ -702,13 +702,13 @@ class Std { private object ManifestJsonMinified extends Val.Builtin1("manifestJsonMinified", "v") { def evalRhs(v: Val, ev: EvalScope, pos: Position): Val = - Val.Str(pos, Materializer.apply0(v, new MaterializeJsonRenderer(indent = -1))(ev).toString) + Val.Str(pos, Materializer.apply0(v, new MaterializeJsonRenderer(indent = -1, newline = "", keyValueSeparator = ":"))(ev).toString) } - private object ManifestJsonEx extends Val.Builtin2("manifestJsonEx", "value", "indent") { - def evalRhs(v: Val, i: Val, ev: EvalScope, pos: Position): Val = + private object ManifestJsonEx extends Val.Builtin4("manifestJsonEx", "value", "indent", "newline", "key_val_sep", Array(null, null, Val.Str(dummyPos, "\n"), Val.Str(dummyPos, ": "))) { + def evalRhs(v: Val, i: Val, newline: Val, keyValSep: Val, ev: EvalScope, pos: Position): Val = Val.Str(pos, Materializer - .apply0(v, new MaterializeJsonRenderer(indent = i.asString.length))(ev) + .apply0(v, MaterializeJsonRenderer(indent = i.asString.length, newline = newline.asString, keyValueSeparator = keyValSep.asString))(ev) .toString) } diff --git a/sjsonnet/test/resources/test_suite/stdlib.jsonnet b/sjsonnet/test/resources/test_suite/stdlib.jsonnet index bbc72740..1305d0d5 100644 --- a/sjsonnet/test/resources/test_suite/stdlib.jsonnet +++ b/sjsonnet/test/resources/test_suite/stdlib.jsonnet @@ -724,13 +724,13 @@ std.assertEqual( ||| ) && -//std.assertEqual( -// std.manifestJsonEx(some_json, '', ' ', ' : '), -// '{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], ' -// + '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], ' -// + '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], ' -// + '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }' -//) && +std.assertEqual( + std.manifestJsonEx(some_json, '', ' ', ' : '), + '{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], ' + + '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], ' + + '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], ' + + '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }' +) && std.assertEqual( std.manifestJsonMinified(some_json), diff --git a/sjsonnet/test/resources/test_suite/stdlib_native.jsonnet b/sjsonnet/test/resources/test_suite/stdlib_native.jsonnet index 6078aee8..d0a137c2 100644 --- a/sjsonnet/test/resources/test_suite/stdlib_native.jsonnet +++ b/sjsonnet/test/resources/test_suite/stdlib_native.jsonnet @@ -724,13 +724,13 @@ std.assertEqual( ||| ) && -//std.assertEqual( -// std.manifestJsonEx(some_json, '', ' ', ' : '), -// '{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], ' -// + '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], ' -// + '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], ' -// + '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }' -//) && +std.assertEqual( + std.manifestJsonEx(some_json, '', ' ', ' : '), + '{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], ' + + '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], ' + + '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], ' + + '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }' +) && std.assertEqual( std.manifestJsonMinified(some_json),