-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ciris-generic module using shapeless
- Loading branch information
Viktor Lövgren
committed
May 14, 2017
1 parent
e4802e2
commit b827fcd
Showing
10 changed files
with
258 additions
and
15 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
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
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
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
5 changes: 5 additions & 0 deletions
5
modules/generic/jvm/src/main/scala/ciris/generic/package.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,5 @@ | ||
package ciris | ||
|
||
import ciris.generic.readers.GenericConfigReaders | ||
|
||
package object generic extends GenericConfigReaders |
41 changes: 41 additions & 0 deletions
41
modules/generic/jvm/src/main/scala/ciris/generic/readers/GenericConfigReaders.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,41 @@ | ||
package ciris.generic.readers | ||
|
||
import ciris.{ConfigError, ConfigReader} | ||
import shapeless.{:+:, ::, CNil, Coproduct, Generic, HNil, Inl, Inr, Lazy} | ||
|
||
trait GenericConfigReaders { | ||
implicit val cNilConfigReader: ConfigReader[CNil] = | ||
ConfigReader.pure { (key, source) ⇒ | ||
Left(ConfigError( | ||
s"Could not find any valid coproduct choice while reading ${source.keyType.value} [$key]")) | ||
} | ||
|
||
implicit def coproductConfigReader[A, B <: Coproduct]( | ||
implicit readA: Lazy[ConfigReader[A]], | ||
readB: ConfigReader[B] | ||
): ConfigReader[A :+: B] = { | ||
ConfigReader.pure { (key, source) ⇒ | ||
readA.value.read(key)(source) match { | ||
case Right(a) ⇒ Right(Inl(a)) | ||
case Left(aError) ⇒ | ||
readB.read(key)(source) match { | ||
case Right(b) ⇒ Right(Inr(b)) | ||
case Left(bError) ⇒ Left(aError combine bError) | ||
} | ||
} | ||
} | ||
} | ||
|
||
implicit def hListArityOneConfigReader[A]( | ||
implicit readA: Lazy[ConfigReader[A]] | ||
): ConfigReader[A :: HNil] = { | ||
readA.value.map(_ :: HNil) | ||
} | ||
|
||
implicit def genericConfigReader[A, B]( | ||
implicit gen: Generic.Aux[A, B], | ||
readB: Lazy[ConfigReader[B]] | ||
): ConfigReader[A] = { | ||
readB.value.map(gen.from) | ||
} | ||
} |
Oops, something went wrong.