Skip to content

Commit

Permalink
Changes from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurelineSwissBorg committed Nov 22, 2024
2 parents d99d7e8 + 759cea7 commit 081e164
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 81 deletions.
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.9.4
sbt.version=1.10.0
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ to ensure the output bytecode remains compatible with users on older JVMs.

## Changelog

### 0.4.11
- Implement `std.isEmpty`, `std.xor`, `std.xnor`, `std.trim`,
`std.equalsIgnoreCase`, `std.sha1`, `std.sha256`, `std.sha512`, `std.sha3` [#204](https://github.com/databricks/sjsonnet/pull/210)
- fix: std.manifestJsonMinified and empty arrays/objects [#207](https://github.com/databricks/sjsonnet/pull/207)
- fix: Use different chars for synthetic paths. [#208](https://github.com/databricks/sjsonnet/pull/208)
- Fix sorting algorithm to work for all array types [#211](https://github.com/databricks/sjsonnet/pull/211)
- Add better error handling for format [#212](https://github.com/databricks/sjsonnet/pull/212)

### 0.4.10

- Implement `std.get` [#202](https://github.com/databricks/sjsonnet/pull/202),
Expand Down
12 changes: 12 additions & 0 deletions sjsonnet/src-js/sjsonnet/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ object Platform {
def md5(s: String): String = {
throw new Exception("MD5 not implemented in Scala.js")
}
def sha1(s: String): String = {
throw new Exception("SHA1 not implemented in Scala.js")
}
def sha256(s: String): String = {
throw new Exception("SHA256 not implemented in Scala.js")
}
def sha512(s: String): String = {
throw new Exception("SHA512 not implemented in Scala.js")
}
def sha3(s: String): String = {
throw new Exception("SHA3 not implemented in Scala.js")
}
def hashFile(file: File): String = {
throw new Exception("hashFile not implemented in Scala.js")
}
Expand Down
2 changes: 1 addition & 1 deletion sjsonnet/src-jvm-native/sjsonnet/SjsonnetMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ object SjsonnetMain {
std: Val.Obj = new Std().Std): Either[String, String] = {

val (jsonnetCode, path) =
if (config.exec.value) (file, wd / "<exec>")
if (config.exec.value) (file, wd / "\uFE64exec\uFE65")
else {
val p = os.Path(file, wd)
(os.read(p), p)
Expand Down
41 changes: 28 additions & 13 deletions sjsonnet/src-jvm/sjsonnet/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ object Platform {
def gzipBytes(b: Array[Byte]): String = {
val outputStream: ByteArrayOutputStream = new ByteArrayOutputStream(b.length)
val gzip: GZIPOutputStream = new GZIPOutputStream(outputStream)
gzip.write(b)
gzip.close()
val gzippedBase64: String = Base64.getEncoder.encodeToString(outputStream.toByteArray)
outputStream.close()
gzippedBase64
try {
gzip.write(b)
Base64.getEncoder.encodeToString(outputStream.toByteArray)
} finally {
gzip.close()
outputStream.close()
}
}

def gzipString(s: String): String = {
Expand All @@ -34,11 +36,13 @@ object Platform {
// Set compression to specified level
val level = compressionLevel.getOrElse(LZMA2Options.PRESET_DEFAULT)
val xz: XZOutputStream = new XZOutputStream(outputStream, new LZMA2Options(level))
xz.write(b)
xz.close()
val xzedBase64: String = Base64.getEncoder.encodeToString(outputStream.toByteArray)
outputStream.close()
xzedBase64
try {
xz.write(b)
Base64.getEncoder.encodeToString(outputStream.toByteArray)
} finally {
xz.close()
outputStream.close()
}
}

def xzString(s: String, compressionLevel: Option[Int]): String = {
Expand All @@ -51,13 +55,24 @@ object Platform {
new JSONObject(yaml).toString()
}

def md5(s: String): String = {
java.security.MessageDigest.getInstance("MD5")
private def computeHash(algorithm: String, s: String) = {
java.security.MessageDigest.getInstance(algorithm)
.digest(s.getBytes("UTF-8"))
.map { b => String.format("%02x", Integer.valueOf(b & 0xff)) }
.mkString
}

def md5(s: String): String = computeHash("MD5", s)

def sha1(s: String): String = computeHash("SHA-1", s)

def sha256(s: String): String = computeHash("SHA-256", s)

def sha512(s: String): String = computeHash("SHA-512", s)

// Same as go-jsonnet https://github.com/google/go-jsonnet/blob/2b4d7535f540f128e38830492e509a550eb86d57/builtins.go#L959
def sha3(s: String): String = computeHash("SHA3-512", s)

private val xxHashFactory = XXHashFactory.fastestInstance()

def hashFile(file: File): String = {
Expand All @@ -78,6 +93,6 @@ object Platform {
fis.close()
}

hash.getValue().toString
hash.getValue.toString
}
}
12 changes: 12 additions & 0 deletions sjsonnet/src-native/sjsonnet/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ object Platform {
def md5(s: String): String = {
throw new Exception("MD5 not implemented in Scala Native")
}
def sha1(s: String): String = {
throw new Exception("SHA1 not implemented in Scala Native")
}
def sha256(s: String): String = {
throw new Exception("SHA256 not implemented in Scala Native")
}
def sha512(s: String): String = {
throw new Exception("SHA512 not implemented in Scala Native")
}
def sha3(s: String): String = {
throw new Exception("SHA3 not implemented in Scala Native")
}

def hashFile(file: File): String = {
// File hashes in Scala Native are just the file content
Expand Down
3 changes: 1 addition & 2 deletions sjsonnet/src/sjsonnet/Expr.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sjsonnet

import java.util.{Arrays, BitSet}
import scala.collection.mutable
import java.util.Arrays

/**
* [[Expr]]s are the parsed syntax trees of a Jsonnet program. They model the
Expand Down
36 changes: 27 additions & 9 deletions sjsonnet/src/sjsonnet/Format.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ object Format{
val cooked0 = formatted.conversion match{
case '%' => widenRaw(formatted, "%")
case _ =>

if (values.isInstanceOf[Val.Arr] && i >= values.cast[Val.Arr].length) {
Error.fail("Too few values to format: %d, expected at least %d".format(values.cast[Val.Arr].length, i + 1))
}
val raw = formatted.label match{
case None => values.cast[Val.Arr].force(i)
case Some(key) =>
Expand All @@ -117,9 +119,12 @@ object Format{
}
i += 1
value match{
case ujson.Str(s) => widenRaw(formatted, s)
case ujson.Str(s) =>
if (formatted.conversion != 's' && formatted.conversion != 'c')
Error.fail("Format required a number at %d, got string".format(i))
widenRaw(formatted, s)
case ujson.Num(s) =>
formatted.conversion match{
formatted.conversion match {
case 'd' | 'i' | 'u' => formatInteger(formatted, s)
case 'o' => formatOctal(formatted, s)
case 'x' => formatHexadecimal(formatted, s)
Expand All @@ -133,20 +138,33 @@ object Format{
case 's' =>
if (s.toLong == s) widenRaw(formatted, s.toLong.toString)
else widenRaw(formatted, s.toString)
case _ => Error.fail("Format required a %s at %d, got string".format(raw.prettyName, i))
}
case ujson.Bool(s) =>
formatted.conversion match {
case 'd' | 'i' | 'u' => formatInteger(formatted, s.compareTo(false))
case 'o' => formatOctal(formatted, s.compareTo(false))
case 'x' => formatHexadecimal(formatted, s.compareTo(false))
case 'X' => formatHexadecimal(formatted, s.compareTo(false)).toUpperCase
case 'e' => formatExponent(formatted, s.compareTo(false)).toLowerCase
case 'E' => formatExponent(formatted, s.compareTo(false))
case 'f' | 'F' => formatFloat(formatted, s.compareTo(false))
case 'g' => formatGeneric(formatted, s.compareTo(false)).toLowerCase
case 'G' => formatGeneric(formatted, s.compareTo(false))
case 'c' => widenRaw(formatted, Character.forDigit(s.compareTo(false), 10).toString)
case 's' => widenRaw(formatted, s.toString)
case _ => Error.fail("Format required a %s at %d, got string".format(raw.prettyName, i))
}
case ujson.True => widenRaw(formatted, "true")
case ujson.False => widenRaw(formatted, "false")
case v => widenRaw(formatted, v.toString)
}

}

output.append(cooked0)
output.append(literal)


}

if (values.isInstanceOf[Val.Arr] && i < values.cast[Val.Arr].length) {
Error.fail("Too many values to format: %d, expected %d".format(values.cast[Val.Arr].length, i))
}
output.toString()
}

Expand Down
2 changes: 1 addition & 1 deletion sjsonnet/src/sjsonnet/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Interpreter(extVars: Map[String, String],


def parseVar(k: String, v: String) = {
resolver.parse(wd / s"<$k>", StaticResolvedFile(v))(evaluator).fold(throw _, _._1)
resolver.parse(wd / s"\uFE64$k\uFE65", StaticResolvedFile(v))(evaluator).fold(throw _, _._1)
}

lazy val evaluator: Evaluator = createEvaluator(
Expand Down
8 changes: 3 additions & 5 deletions sjsonnet/src/sjsonnet/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class PythonRenderer(out: Writer = new java.io.StringWriter(),
}
}

/** Renderer used by std.manifestJson and std.manifestJsonEx */
/** 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) {

Expand All @@ -201,10 +201,9 @@ 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) elemBuilder.append('\n') else renderIndent()
if (length == 0 && indent != -1) elemBuilder.append('\n') else renderIndent()

def subVisitor: MaterializeJsonRenderer = MaterializeJsonRenderer.this

def visitValue(v: StringWriter, index: Int): Unit = {
flushBuffer()
commaBuffered = true
Expand All @@ -225,12 +224,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) elemBuilder.append('\n') else renderIndent()
if (length == 0 && indent != -1) elemBuilder.append('\n') else renderIndent()

def subVisitor: MaterializeJsonRenderer = MaterializeJsonRenderer.this

def visitKey(index: Int): MaterializeJsonRenderer = MaterializeJsonRenderer.this

def visitKeyValue(s: Any): Unit = {
elemBuilder.append(':')
if (indent != -1) elemBuilder.append(' ')
Expand Down
Loading

0 comments on commit 081e164

Please sign in to comment.