Merge pull request #319 from eed3si9n/wip/json

Add custom handling for Disabled companion object
This commit is contained in:
eugene yokota 2019-08-28 20:46:13 -04:00 committed by GitHub
commit 686bd9c9be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 36 deletions

View File

@ -1,27 +0,0 @@
/**
* This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
*/
// DO NOT EDIT MANUALLY
package sbt.librarymanagement
import _root_.sjsonnew.{ Unbuilder, Builder, JsonFormat, deserializationError }
trait DisabledFormats { self: sjsonnew.BasicJsonProtocol =>
implicit lazy val DisabledFormat: JsonFormat[sbt.librarymanagement.Disabled] = new JsonFormat[sbt.librarymanagement.Disabled] {
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): sbt.librarymanagement.Disabled = {
jsOpt match {
case Some(js) =>
unbuilder.beginObject(js)
unbuilder.endObject()
sbt.librarymanagement.Disabled()
case None =>
deserializationError("Expected JsObject but found None")
}
}
override def write[J](obj: sbt.librarymanagement.Disabled, builder: Builder[J]): Unit = {
builder.beginObject()
builder.endObject()
}
}
}

View File

@ -331,14 +331,30 @@ trait CrossVersionFormats {
with sbt.librarymanagement.ConstantFormats
with sbt.librarymanagement.PatchFormats
with sbt.librarymanagement.FullFormats =>
implicit lazy val CrossVersionFormat: JsonFormat[sbt.librarymanagement.CrossVersion] =
flatUnionFormat6[
sbt.librarymanagement.CrossVersion,
sbt.librarymanagement.Disabled,
sbt.librarymanagement.Disabled.type,
sbt.librarymanagement.Binary,
sbt.librarymanagement.Constant,
sbt.librarymanagement.Patch,
sbt.librarymanagement.Full
implicit lazy val CrossVersionFormat: JsonFormat[CrossVersion] = {
val format = flatUnionFormat6[
CrossVersion,
Disabled,
Disabled.type,
Binary,
Constant,
Patch,
Full
]("type")
// This is a hand-crafted formatter to avoid Disabled$ showing up in JSON
new JsonFormat[CrossVersion] {
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): CrossVersion =
format.read(jsOpt, unbuilder)
override def write[J](obj: CrossVersion, builder: Builder[J]): Unit = {
if (obj == Disabled) {
builder.beginPreObject()
builder.addFieldName("type")
builder.writeString("Disabled")
builder.endPreObject()
builder.beginObject()
builder.endObject()
} else format.write(obj, builder)
}
}
}
}

View File

@ -1,8 +1,11 @@
package sbt.librarymanagement
import sbt.internal.librarymanagement.UnitSpec
import sjsonnew.support.scalajson.unsafe.{ Converter, CompactPrinter, Parser }
class ModuleIdTest extends UnitSpec {
val expectedJson =
"""{"organization":"com.acme","name":"foo","revision":"1","isChanging":false,"isTransitive":true,"isForce":false,"explicitArtifacts":[],"inclusions":[],"exclusions":[],"extraAttributes":{},"crossVersion":{"type":"Disabled"}}"""
"Module Id" should "return cross-disabled module id as equal to a copy" in {
ModuleID("com.acme", "foo", "1") shouldBe ModuleID("com.acme", "foo", "1")
}
@ -14,4 +17,15 @@ class ModuleIdTest extends UnitSpec {
(ModuleID("com.acme", "foo", "1") cross CrossVersion.binary) shouldBe
(ModuleID("com.acme", "foo", "1") cross CrossVersion.binary)
}
it should "format itself into JSON" in {
import LibraryManagementCodec._
val json = Converter.toJson(ModuleID("com.acme", "foo", "1")).get
assert(CompactPrinter(json) == expectedJson)
}
it should "thaw back from JSON" in {
import LibraryManagementCodec._
val json = Parser.parseUnsafe(expectedJson)
val m = Converter.fromJsonUnsafe[ModuleID](json)
assert(m == ModuleID("com.acme", "foo", "1"))
}
}