-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Atomic instance for circe BiggerDecimal
See #36.
- Loading branch information
Showing
5 changed files
with
165 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2017 Daniel Urban | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sigs.seals | ||
package circe | ||
|
||
import java.util.UUID | ||
|
||
import io.circe.numbers.BiggerDecimal | ||
|
||
trait Atoms { | ||
|
||
implicit val atomicForBiggerDecimal: Atomic[BiggerDecimal] = | ||
Atoms.AtomicForBiggerDecimal | ||
} | ||
|
||
object Atoms extends Atoms { | ||
|
||
// Note: this is defined in the object (and | ||
// not the trait), because this way serialization | ||
// and deserialization of this Atomic instance | ||
// preserves object identity. | ||
private[Atoms] object AtomicForBiggerDecimal | ||
extends Atomic[BiggerDecimal] | ||
with Atomic.FallbackBinary[BiggerDecimal] { | ||
|
||
val description: String = | ||
"BiggerDecimal" | ||
|
||
val uuid: UUID = | ||
UUID.fromString("5c3d07fd-2f45-4f7b-af52-eeba62b95aa1") | ||
|
||
def stringRepr(a: BiggerDecimal): String = | ||
a.toString | ||
|
||
def fromString(s: String): Either[Atomic.Error, BiggerDecimal] = { | ||
BiggerDecimal.parseBiggerDecimal(s).toRight( | ||
left = Atomic.InvalidData(s"not a BiggerDecimal: '${s}'") | ||
) | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
circe/src/test/scala/io/sigs/seals/circe/AtomicLawsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2017 Daniel Urban | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sigs.seals | ||
package circe | ||
|
||
import cats.Eq | ||
|
||
import io.circe.numbers.BiggerDecimal | ||
|
||
import org.scalacheck.{ Arbitrary, Gen } | ||
|
||
class AtomicLawsSpec extends tests.BaseLawsSpec { | ||
|
||
import Atoms._ | ||
|
||
implicit val eqBiggerDecimal: Eq[BiggerDecimal] = | ||
Eq.fromUniversalEquals | ||
|
||
implicit def arbBiggerDecimal( | ||
implicit | ||
arbBd: Arbitrary[BigDecimal], | ||
arbBi: Arbitrary[BigInt], | ||
arbDbl: Arbitrary[Double] | ||
): Arbitrary[BiggerDecimal] = Arbitrary { | ||
Gen.oneOf( | ||
// fits into a BigDecimal: | ||
arbBd.arbitrary.map { x => | ||
BiggerDecimal.fromBigDecimal(x.underlying) | ||
}, | ||
// doesn't fit into a BigDecimal: | ||
arbBi.arbitrary.map { n => | ||
val str = s"${n}e${n max Int.MaxValue.toLong + 1L}" | ||
BiggerDecimal.parseBiggerDecimal(str).getOrElse { | ||
core.impossible(s"cannot parse BiggerDecimal from '${str}'") | ||
} | ||
}, | ||
// can contain negative zero: | ||
arbDbl.arbitrary.map(d => BiggerDecimal.fromDouble(- d)) | ||
) | ||
} | ||
|
||
checkAtomicLaws[BiggerDecimal]("BiggerDecimal") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2017 Daniel Urban | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.sigs.seals | ||
package circe | ||
|
||
import io.circe.numbers.BiggerDecimal | ||
|
||
import circe.Atoms._ | ||
import circe.Codecs._ | ||
|
||
class AtomsSpec extends BaseJsonSpec { | ||
|
||
"Atomic[BiggerDecimal]" in { | ||
val bds = List( | ||
BiggerDecimal.fromLong(0L), | ||
BiggerDecimal.NegativeZero, | ||
// this is too big for a BigDecimal: | ||
BiggerDecimal.parseBiggerDecimal(s"9999e${Int.MaxValue.toLong * 2}").getOrElse(fail) | ||
) | ||
for (bd <- bds) { | ||
checkJson(bd) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters