Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] Use Unroll plugin for binary compatibility #555

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .mill-version
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0.11.6
0.11.7

19 changes: 13 additions & 6 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import de.tobiasroeser.mill.vcs.version.VcsVersion
import com.github.lolgab.mill.mima._

val scala212 = "2.12.18"
val scala213 = "2.13.11"
val scala213 = "2.13.12"

val scala3 = "3.3.0"
val scala3 = "3.3.1"
val scalaNative = "0.4.14"
val acyclic = "0.3.8"
val acyclic = "0.3.9"

val sourcecode = "0.3.0"

val dottyCustomVersion = Option(sys.props("dottyVersion"))

val scala2JVMVersions = Seq(scala212, scala213)
val scalaVersions = scala2JVMVersions// ++ Seq(scala3) ++ dottyCustomVersion
val scalaVersions = scala2JVMVersions ++ Seq(scala3) ++ dottyCustomVersion

trait CommonPlatformModule extends ScalaModule with PlatformScalaModule{

Expand Down Expand Up @@ -58,6 +58,10 @@ trait CommonPublishModule
)
)

def scalacPluginIvyDeps =
super.scalacPluginIvyDeps() ++ Agg(ivy"com.lihaoyi::unroll-plugin:0.1.12")


def publishProperties: Target[Map[String, String]] = super.publishProperties() ++ Map(
"info.releaseNotesURL" -> "https://com-lihaoyi.github.io/upickle/#VersionHistory"
)
Expand Down Expand Up @@ -219,7 +223,10 @@ object ujson extends Module{
object upickle extends Module{
object core extends Module {
trait CommonCoreModule extends CommonPublishModule {
def ivyDeps = Agg(ivy"com.lihaoyi::geny::1.0.0")
def ivyDeps = Agg(
ivy"com.lihaoyi::geny::1.0.0",
ivy"com.lihaoyi::unroll-annotation:0.1.12"
)
}

object js extends Cross[CoreJsModule](scalaVersions)
Expand Down Expand Up @@ -379,7 +386,7 @@ trait BenchModule extends CommonPlatformModule{

object bench extends Module {
object js extends BenchModule with ScalaJSModule {
def scalaJSVersion = "1.13.0"
def scalaJSVersion = "1.13.1"
def moduleDeps = Seq(upickle.js(scala213).test)
}

Expand Down
80 changes: 10 additions & 70 deletions ujson/src/ujson/package.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import upickle.core.NoOpVisitor
import upickle.core.BufferedValue

import scala.annotation.unroll
package object ujson{
def transform[T](t: Readable,
v: upickle.core.Visitor[_, T],
sortKeys: Boolean = false): T = {
@unroll sortKeys: Boolean = false): T = {
BufferedValue.maybeSortKeysTransform(Readable, t, sortKeys, v)
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def transform[T](t: Readable,
v: upickle.core.Visitor[_, T]): T = transform(t, v, sortKeys = false)
/**
* Read the given JSON input as a JSON struct
*/
Expand All @@ -25,70 +22,40 @@ package object ujson{
def write(t: Value.Value,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): String = {
@unroll sortKeys: Boolean = false): String = {
val writer = new java.io.StringWriter
writeTo(t, writer, indent, escapeUnicode, sortKeys)
writer.toString
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def write(t: Value.Value,
indent: Int,
escapeUnicode: Boolean): String = {
write(t, indent, escapeUnicode, sortKeys = false)
}

/**
* Write the given JSON struct as a JSON String to the given Writer
*/
def writeTo(t: Value.Value,
out: java.io.Writer,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Unit = {
@unroll sortKeys: Boolean = false): Unit = {
transform(t, Renderer(out, indent, escapeUnicode), sortKeys)
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeTo(t: Value.Value,
out: java.io.Writer,
indent: Int,
escapeUnicode: Boolean): Unit = {
writeTo(t, out, indent, escapeUnicode, sortKeys = false)
}

def writeToOutputStream(t: Value.Value,
out: java.io.OutputStream,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Unit = {
@unroll sortKeys: Boolean = false): Unit = {
transform(t, new BaseByteRenderer(out, indent, escapeUnicode), sortKeys)
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeToOutputStream(t: Value.Value,
out: java.io.OutputStream,
indent: Int,
escapeUnicode: Boolean): Unit = {
writeToOutputStream(t, out, indent, escapeUnicode, sortKeys = false)
}

def writeToByteArray(t: Value.Value,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Array[Byte] = {
@unroll sortKeys: Boolean = false): Array[Byte] = {
val baos = new java.io.ByteArrayOutputStream
writeToOutputStream(t, baos, indent, escapeUnicode, sortKeys)
baos.toByteArray
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeToByteArray(t: Value.Value,
indent: Int,
escapeUnicode: Boolean): Array[Byte] = {
writeToByteArray(t, indent, escapeUnicode, sortKeys = false)
}

/**
* Parse the given JSON input, failing if it is invalid
*/
Expand All @@ -100,18 +67,12 @@ package object ujson{
def reformat(s: Readable,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): String = {
@unroll sortKeys: Boolean = false): String = {
val writer = new java.io.StringWriter()
reformatTo(s, writer, indent, escapeUnicode, sortKeys)
writer.toString
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def reformat(s: Readable,
indent: Int,
escapeUnicode: Boolean): String = {
reformat(s, indent, escapeUnicode, sortKeys = false)
}
/**
* Parse the given JSON input and write it to a string with
* the configured formatting to the given Writer
Expand All @@ -120,17 +81,10 @@ package object ujson{
out: java.io.Writer,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Unit = {
@unroll sortKeys: Boolean = false): Unit = {
transform(s, Renderer(out, indent, escapeUnicode), sortKeys)
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def reformatTo(s: Readable,
out: java.io.Writer,
indent: Int,
escapeUnicode: Boolean): Unit = {
reformatTo(s, out, indent, escapeUnicode, sortKeys = false)
}
/**
* Parse the given JSON input and write it to a string with
* the configured formatting to the given Writer
Expand All @@ -139,33 +93,19 @@ package object ujson{
out: java.io.OutputStream,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Unit = {
@unroll sortKeys: Boolean = false): Unit = {
transform(s, new BaseByteRenderer(out, indent, escapeUnicode), sortKeys)
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def reformatToOutputStream(s: Readable,
out: java.io.OutputStream,
indent: Int,
escapeUnicode: Boolean): Unit = {
reformatToOutputStream(s, out, indent, escapeUnicode, sortKeys = false)
}

def reformatToByteArray(s: Readable,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Array[Byte] = {
@unroll sortKeys: Boolean = false): Array[Byte] = {
val baos = new java.io.ByteArrayOutputStream
reformatToOutputStream(s, baos, indent, escapeUnicode, sortKeys)
baos.toByteArray
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def reformatToByteArray(s: Readable,
indent: Int,
escapeUnicode: Boolean): Array[Byte] = {
reformatToByteArray(s, indent, escapeUnicode, sortKeys = false)
}
// End ujson
@deprecated("use ujson.Value")
type Js = Value
Expand Down
70 changes: 10 additions & 60 deletions upickle/src/upickle/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import language.experimental.macros
import language.higherKinds
import upickle.core._
import scala.reflect.ClassTag
import scala.annotation.unroll

/**
* An instance of the upickle API. There's a default instance at
Expand Down Expand Up @@ -50,28 +51,18 @@ trait Api
def write[T: Writer](t: T,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): String = {
@unroll sortKeys: Boolean = false): String = {
maybeSortKeysTransform(t, sortKeys, ujson.StringRenderer(indent, escapeUnicode)).toString
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def write[T: Writer](t: T,
indent: Int,
escapeUnicode: Boolean): String = {
write(t, indent, escapeUnicode, sortKeys = false)
}

/**
* Write the given Scala value as a MessagePack binary
*/
def writeBinary[T: Writer](t: T,
sortKeys: Boolean = false): Array[Byte] = {
@unroll sortKeys: Boolean = false): Array[Byte] = {
maybeSortKeysTransform(t, sortKeys, new upack.MsgPackWriter(new ByteArrayOutputStream())).toByteArray
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeBinary[T: Writer](t: T): Array[Byte] = writeBinary(t, sortKeys = false)

/**
* Write the given Scala value as a JSON struct
*/
Expand All @@ -89,105 +80,64 @@ trait Api
out: java.io.Writer,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Unit = {
@unroll sortKeys: Boolean = false): Unit = {
maybeSortKeysTransform(t, sortKeys, new ujson.Renderer(out, indent = indent, escapeUnicode))
}


// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeTo[T: Writer](t: T,
out: java.io.Writer,
indent: Int,
escapeUnicode: Boolean): Unit = writeTo(t, out, indent, escapeUnicode, sortKeys = false)

def writeToOutputStream[T: Writer](t: T,
out: java.io.OutputStream,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Unit = {
@unroll sortKeys: Boolean = false): Unit = {
maybeSortKeysTransform(t, sortKeys, new ujson.BaseByteRenderer(out, indent = indent, escapeUnicode))
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeToOutputStream[T: Writer](t: T,
out: java.io.OutputStream,
indent: Int,
escapeUnicode: Boolean): Unit = {
writeToOutputStream(t, out, indent, escapeUnicode, sortKeys = false)
}

def writeToByteArray[T: Writer](t: T,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): Array[Byte] = {
@unroll sortKeys: Boolean = false): Array[Byte] = {
val out = new java.io.ByteArrayOutputStream()
writeToOutputStream(t, out, indent, escapeUnicode, sortKeys)
out.toByteArray
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeToByteArray[T: Writer](t: T,
indent: Int,
escapeUnicode: Boolean): Array[Byte] = {
writeToByteArray[T](t, indent, escapeUnicode, sortKeys = false)
}
/**
* Write the given Scala value as a JSON string via a `geny.Writable`
*/
def stream[T: Writer](t: T,
indent: Int = -1,
escapeUnicode: Boolean = false,
sortKeys: Boolean = false): geny.Writable = new geny.Writable{
@unroll sortKeys: Boolean = false): geny.Writable = new geny.Writable{
override def httpContentType = Some("application/json")
def writeBytesTo(out: java.io.OutputStream) = {
maybeSortKeysTransform(t, sortKeys, new ujson.BaseByteRenderer(out, indent = indent, escapeUnicode))
}
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def stream[T: Writer](t: T,
indent: Int,
escapeUnicode: Boolean): geny.Writable = {
stream(t, indent, escapeUnicode, sortKeys = false)
}
/**
* Write the given Scala value as a MessagePack binary to the given OutputStream
*/
def writeBinaryTo[T: Writer](t: T,
out: java.io.OutputStream,
sortKeys: Boolean = false): Unit = {
@unroll sortKeys: Boolean = false): Unit = {
streamBinary[T](t, sortKeys = sortKeys).writeBytesTo(out)
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeBinaryTo[T: Writer](t: T,
out: java.io.OutputStream): Unit = {
writeBinaryTo(t, out, sortKeys = false)
}

def writeBinaryToByteArray[T: Writer](t: T,
sortKeys: Boolean = false): Array[Byte] = {
@unroll sortKeys: Boolean = false): Array[Byte] = {
val out = new java.io.ByteArrayOutputStream()
streamBinary[T](t, sortKeys = sortKeys).writeBytesTo(out)
out.toByteArray
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def writeBinaryToByteArray[T: Writer](t: T): Array[Byte] = {
writeBinaryToByteArray(t, sortKeys = false)
}
/**
* Write the given Scala value as a MessagePack binary via a `geny.Writable`
*/
def streamBinary[T: Writer](t: T, sortKeys: Boolean = false): geny.Writable = new geny.Writable{
def streamBinary[T: Writer](t: T, @unroll sortKeys: Boolean = false): geny.Writable = new geny.Writable{
override def httpContentType = Some("application/octet-stream")
def writeBytesTo(out: java.io.OutputStream) = maybeSortKeysTransform(t, sortKeys, new upack.MsgPackWriter(out))
}

// @deprecated("Binary Compatibility Stub", "upickle after 3.1.4")
def streamBinary[T: Writer](t: T): geny.Writable = {
streamBinary(t, sortKeys = false)
}

def writer[T: Writer] = implicitly[Writer[T]]

Expand Down
Loading