Skip to content

Commit

Permalink
Merge pull request pureconfig#429 from pureconfig/for-product-n-docs
Browse files Browse the repository at this point in the history
Add `forProductN` helpers to the docs
  • Loading branch information
jcazevedo authored Oct 25, 2018
2 parents 7b31553 + 0190e50 commit 55eb5d1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
15 changes: 4 additions & 11 deletions docs/src/main/tut/docs/complex-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,12 @@ Similarly to adding support for simple types, it is possible to manually create
import pureconfig._
import pureconfig.error._
def extractId(objCur: ConfigObjectCursor): Either[ConfigReaderFailures, String] =
objCur.atKey("id").flatMap(_.asString)
def extractValue(objCur: ConfigObjectCursor): Either[ConfigReaderFailures, Int] =
objCur.atKey("value").flatMap(ConfigReader[Int].from(_))
val class1Reader = ConfigReader.forProduct1("id")(new Class1(_))
val class2Reader = ConfigReader.forProduct2("id", "value")(new Class2(_, _))
def extractByType(typ: String, objCur: ConfigObjectCursor): Either[ConfigReaderFailures, Identifiable] = typ match {
case "class1" => extractId(objCur).map(new Class1(_))
case "class2" =>
for {
id <- extractId(objCur)
value <- extractValue(objCur)
} yield new Class2(id, value)
case "class1" => class1Reader.from(objCur)
case "class2" => class2Reader.from(objCur)
case t =>
objCur.failed(CannotConvert(objCur.value.toString, "Identifiable",
s"type has value $t instead of class1 or class2"))
Expand Down
11 changes: 3 additions & 8 deletions docs/src/main/tut/docs/non-automatic-derivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pureconfig.loadConfig[Person](conf)
### Manual

When case class and sealed trait derivation is not needed or wanted, we can simply not import anything and define our
reader using any of ways explained in [Supporting New Types](supporting-new-types.html):
reader using any of ways explained in [Supporting New Types](supporting-new-types.html). The `forProductN` helper
methods are convenient for creating readers and writers for case class-like types without generic derivation:

```tut:invisible:reset
import com.typesafe.config.ConfigFactory
Expand All @@ -89,13 +90,7 @@ val conf = ConfigFactory.parseString("{ name: John, surname: Doe }")
```tut:silent
import pureconfig._
implicit val personReader = ConfigReader.fromCursor[Person] { cur =>
for {
objCur <- cur.asObjectCursor
name <- objCur.atKey("name").right.flatMap(_.asString)
surname <- objCur.atKey("surname").right.flatMap(_.asString)
} yield Person(name, surname)
}
implicit val personReader = ConfigReader.forProduct2("name", "surname")(Person(_, _))
```

```tut:book
Expand Down

0 comments on commit 55eb5d1

Please sign in to comment.