Skip to content

Commit

Permalink
Merge pull request #319 from eed3si9n/wip/json
Browse files Browse the repository at this point in the history
Add custom handling for Disabled companion object
  • Loading branch information
eed3si9n committed Aug 29, 2019
2 parents 4b4087a + b01b613 commit 686bd9c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.

This file was deleted.

34 changes: 25 additions & 9 deletions core/src/main/scala/sbt/librarymanagement/CrossVersion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
14 changes: 14 additions & 0 deletions core/src/test/scala/sbt/librarymanagement/ModuleIdTest.scala
Original file line number Diff line number Diff line change
@@ -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")
}
Expand All @@ -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"))
}
}

0 comments on commit 686bd9c

Please sign in to comment.