Skip to content
Permalink
Browse files

Merge branch 'master' into add_maven_spark_pureconfig_faq

  • Loading branch information
ruippeixotog committed Jun 22, 2017
2 parents f9353e5 + d1ae133 commit 5333e88317d24a37ee39c126230362a7ebc8400e
@@ -3,6 +3,7 @@
- New features
- `ConfigFieldMapping` now has a `withOverrides` method that allows users to easily define exceptional cases to an
existing mapping;
- `ConfigReader` and `ConfigWriter` for `java.math.BigDecimal`, `java.math.BigInteger`,
- Bug fixes
- A breaking change introduced in v0.7.1 where `loadConfigFromFiles` stopped allowing missing files was reverted.

@@ -120,6 +120,8 @@ Currently supported types for fields are:
- [`java.util.UUID`](https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html);
- [`java.nio.file.Path`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html);
- [`java.util.regex.Pattern`](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/regex/Pattern.html) and [`scala.util.matching.Regex`](https://www.scala-lang.org/files/archive/api/current/scala/util/matching/Regex.html);
- [`java.math.BigDecimal`](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html), [`java.math.BigInteger`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html),
[`scala.math.BigDecimal`](https://www.scala-lang.org/api/2.12.2/index.html#scala.math.BigDecimal), and [`scala.math.BigInt'](https://www.scala-lang.org/api/2.12.2/index.html#scala.math.BigInt);
- Typesafe `ConfigValue`, `ConfigObject` and `ConfigList`;
- value classes for which readers and writers of the inner type are used;
- case classes;
@@ -1,6 +1,7 @@
package pureconfig

import java.io.File
import java.math.{ BigInteger, MathContext, BigDecimal => JavaBigDecimal }
import java.net.{ URI, URL }
import java.nio.file.{ Path, Paths }
import java.time._
@@ -9,6 +10,7 @@ import java.util.UUID
import java.util.regex.Pattern

import scala.concurrent.duration.{ Duration, FiniteDuration }
import scala.math.{ BigDecimal, BigInt }
import scala.reflect.ClassTag
import scala.util.matching.Regex

@@ -113,6 +115,35 @@ trait DurationReaders {
}
}

/**
* Trait containing `ConfigReader` instances for Java and Scala arbitrary-precision numeric types.
*/
trait NumericReaders {

// BigDecimal String parsing copied from Scala 2.11.
// The Scala 2.10 bigDecimal class is hard-coded to use 128-bits of prevision when parsing BigDecimal from String.
// This causes truncation for large values.
// Scala 2.11+ fixed this bug by adapting precision to accommodate all digits in the string.
private def exact(repr: JavaBigDecimal): BigDecimal = {
val mc =
if (repr.precision <= BigDecimal.defaultMathContext.getPrecision) BigDecimal.defaultMathContext
else new MathContext(repr.precision, java.math.RoundingMode.HALF_EVEN)
new BigDecimal(repr, mc)
}

implicit val javaBigIntegerReader: ConfigReader[BigInteger] =
ConfigReader.fromNonEmptyString[BigInteger](catchReadError(new BigInteger(_)))

implicit val javaBigDecimalReader: ConfigReader[JavaBigDecimal] =
ConfigReader.fromNonEmptyString[JavaBigDecimal](catchReadError(new JavaBigDecimal(_)))

implicit val scalaBigIntReader: ConfigReader[BigInt] =
ConfigReader.fromNonEmptyString[BigInt](catchReadError(BigInt(_)))

implicit val scalaBigDecimalReader: ConfigReader[BigDecimal] =
ConfigReader.fromNonEmptyString[BigDecimal](catchReadError(s => exact(new JavaBigDecimal(s))))
}

/**
* Trait containing `ConfigReader` instances for Typesafe config models.
*/
@@ -155,6 +186,7 @@ trait BasicReaders
with RegexReaders
with JavaTimeReaders
with DurationReaders
with NumericReaders
with TypesafeConfigReaders

object BasicReaders extends BasicReaders
@@ -1,6 +1,7 @@
package pureconfig

import java.io.File
import java.math.{ BigDecimal => JavaBigDecimal, BigInteger }
import java.net.{ URI, URL }
import java.nio.file.Path
import java.time._
@@ -9,6 +10,7 @@ import java.util.UUID
import java.util.regex.Pattern

import scala.concurrent.duration.{ Duration, FiniteDuration }
import scala.math.{ BigDecimal, BigInt }
import scala.util.matching.Regex

import com.typesafe.config._
@@ -84,6 +86,17 @@ trait DurationWriters {
implicit val finiteDurationConfigWriter = ConfigWriter.toString[FiniteDuration](DurationConvert.fromDuration)
}

/**
* Trait containing `ConfigWriter` instances for Java and Scala arbitrary-precision numeric types.
*/
trait NumericWriters {

implicit val javaBigDecimalWriter: ConfigWriter[JavaBigDecimal] = ConfigWriter.toDefaultString[JavaBigDecimal]
implicit val bigIntegerWriter: ConfigWriter[BigInteger] = ConfigWriter.toDefaultString[BigInteger]
implicit val scalaBigDecimalWriter: ConfigWriter[BigDecimal] = ConfigWriter.toDefaultString[BigDecimal]
implicit val scalaBigIntWriter: ConfigWriter[BigInt] = ConfigWriter.toDefaultString[BigInt]
}

/**
* Trait containing `ConfigWriter` instances for Typesafe config models.
*/
@@ -117,6 +130,7 @@ trait BasicWriters
with RegexWriters
with JavaTimeWriters
with DurationWriters
with NumericWriters
with TypesafeConfigWriters

object BasicWriters extends BasicWriters
@@ -120,6 +120,8 @@ Currently supported types for fields are:
- [`java.util.UUID`](https://docs.oracle.com/javase/8/docs/api/java/util/UUID.html);
- [`java.nio.file.Path`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html);
- [`java.util.regex.Pattern`](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/regex/Pattern.html) and [`scala.util.matching.Regex`](https://www.scala-lang.org/files/archive/api/current/scala/util/matching/Regex.html);
- [`java.math.BigDecimal`](https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html), [`java.math.BigInteger`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html),
[`scala.math.BigDecimal`](https://www.scala-lang.org/api/2.12.2/index.html#scala.math.BigDecimal), and [`scala.math.BigInt'](https://www.scala-lang.org/api/2.12.2/index.html#scala.math.BigInt);
- Typesafe `ConfigValue`, `ConfigObject` and `ConfigList`;
- value classes for which readers and writers of the inner type are used;
- case classes;
@@ -1,6 +1,7 @@
package pureconfig

import java.io.File
import java.math.{ BigDecimal => JavaBigDecimal, BigInteger }
import java.net.{ URI, URL }
import java.nio.file.Path
import java.time._
@@ -67,6 +68,11 @@ class BasicConvertersSuite extends BaseSuite {

checkArbitrary[Short]

checkArbitrary[BigInt]
checkArbitrary[BigDecimal]
checkArbitrary[BigInteger]
checkArbitrary[JavaBigDecimal]

checkArbitrary[UUID]

checkArbitrary[Path]
@@ -18,4 +18,6 @@ package object arbitrary {
implicit val arbFile = Arbitrary(genFile)
implicit val arbPercentage = Arbitrary(genPercentage)
implicit val arbJodaDateTime = Arbitrary(genJodaDateTime)
implicit val arbJavaBigDecimal = Arbitrary(genJavaBigDecimal)
implicit val arbJavaBigInteger = Arbitrary(genBigInt)
}
@@ -1,6 +1,7 @@
package pureconfig

import java.io.File
import java.math.{ BigInteger, BigDecimal => JavaBigDecimal }
import java.nio.file.{ Path, Paths }
import java.time._
import java.time.{ Duration => JavaDuration }
@@ -66,4 +67,8 @@ package object gen {
dateTime <- ConfigurableSuite.localDateTimeArbitrary.arbitrary
} yield new org.joda.time.DateTime(dateTime.getYear, dateTime.getMonthValue, dateTime.getDayOfMonth,
dateTime.getHour, dateTime.getMinute, dateTime.getSecond, org.joda.time.DateTimeZone.UTC)

val genJavaBigDecimal: Gen[JavaBigDecimal] = Arbitrary.arbitrary[BigDecimal].map(_.bigDecimal)

val genBigInt: Gen[BigInteger] = Arbitrary.arbitrary[BigInt].map(_.bigInteger)
}

0 comments on commit 5333e88

Please sign in to comment.
You can’t perform that action at this time.