Skip to content

Commit a3e1f6f

Browse files
authored
Tuple docs (#184)
2 parents 54d7942 + 22974df commit a3e1f6f

File tree

13 files changed

+298
-52
lines changed

13 files changed

+298
-52
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ If this project interests you, please drop a 🌟 - these things are worthless b
88

99
## Installation
1010
```scala
11-
libraryDependencies += "io.github.arainko" %% "ducktape" % "0.2.2"
11+
libraryDependencies += "io.github.arainko" %% "ducktape" % "0.2.3"
1212

1313
// or if you're using Scala.js or Scala Native
14-
libraryDependencies += "io.github.arainko" %%% "ducktape" % "0.2.2"
14+
libraryDependencies += "io.github.arainko" %%% "ducktape" % "0.2.3"
1515
```
1616

1717
NOTE: the [version scheme](https://www.scala-lang.org/blog/2021/02/16/preventing-version-conflicts-with-versionscheme.html) is set to `early-semver`

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ lazy val docs =
7979
) ++ (
8080
// Going overboard with this since all selections are connected to each other (eg. you pick an option on of them)
8181
// then all of them will change, this caused screen jumps when looking at the generated code
82-
(1 to 15).map(num =>
82+
(1 to 25).map(num =>
8383
SelectionConfig(
8484
s"underlying-code-$num",
8585
ChoiceConfig("visible", "User visible code"),

docs/fallible_transformations/basics.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ These will be used interchangably throughout the examples below, but if you want
105105
@:select(underlying-code-1)
106106
@:choice(visible)
107107
```scala mdoc
108-
given Mode.Accumulating.Either[String, List] with {}
108+
given Mode.Accumulating.Either[String, List]()
109109

110110
wirePerson.fallibleTo[domain.Person]
111111
```
@@ -124,7 +124,7 @@ Read more about the rules under which the transformations are generated in a cha
124124
@:select(underlying-code-2)
125125
@:choice(visible)
126126
```scala mdoc:nest
127-
given Mode.FailFast.Either[String] with {}
127+
given Mode.FailFast.Either[String]()
128128

129129
wirePerson
130130
.into[domain.Person]
@@ -161,7 +161,7 @@ Read more in the section about [configuring fallible transformations](configurin
161161
@:select(underlying-code-3)
162162
@:choice(visible)
163163
```scala mdoc:nest
164-
given Mode.Accumulating.Either[String, List] with {}
164+
given Mode.Accumulating.Either[String, List]()
165165

166166
wirePerson.fallibleVia(domain.Person.apply)
167167
```
@@ -178,7 +178,7 @@ Docs.printCode(wirePerson.fallibleVia(domain.Person.apply))
178178
@:select(underlying-code-4)
179179
@:choice(visible)
180180
```scala mdoc:nest
181-
given Mode.FailFast.Either[String] with {}
181+
given Mode.FailFast.Either[String]()
182182

183183
wirePerson
184184
.intoVia(domain.Person.apply)

docs/fallible_transformations/configuring_fallible_transformations.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,21 @@ val good = wire.Person(name = "ValidName", age = 24, socialSecurityNo = "SOCIALN
8080

8181
### Product configurations
8282

83+
| **Name** | **Description** |
84+
|:-----------------:|:-------------------:|
85+
| `Field.fallibleConst` | a fallible variant of `Field.const` that allows for supplying values wrapped in an `F` |
86+
| `Field.fallibleComputed` | a fallible variant of `Field.computed` that allows for supplying functions that return values wrapped in an `F` |
87+
88+
---
89+
8390
* `Field.fallibleConst` - a fallible variant of `Field.const` that allows for supplying values wrapped in an `F`
8491

8592
@:select(underlying-code-1)
8693
@:choice(visible)
8794
```scala mdoc:nest
8895
import io.github.arainko.ducktape.*
8996

90-
given Mode.Accumulating.Either[String, List] with {}
97+
given Mode.Accumulating.Either[String, List]()
9198

9299
bad
93100
.into[domain.Person]
@@ -119,7 +126,7 @@ Docs.printCode(
119126
@:select(underlying-code-2)
120127
@:choice(visible)
121128
```scala mdoc:nest
122-
given Mode.Accumulating.Either[String, List] with {}
129+
given Mode.Accumulating.Either[String, List]()
123130

124131
bad
125132
.into[domain.Person]
@@ -145,6 +152,13 @@ Docs.printCode(
145152

146153
### Coproduct configurations
147154

155+
| **Name** | **Description** |
156+
|:-----------------:|:-------------------:|
157+
| `Case.fallibleConst` | a fallible variant of `Case.const` that allows for supplying values wrapped in an `F` |
158+
| `Case.fallibleComputed` | a fallible variant of `Case.computed` that allows for supplying functions that return values wrapped in an `F` |
159+
160+
---
161+
148162
Let's define a wire enum (pretend that it's coming from... somewhere) and a domain enum that doesn't exactly align with the wire one.
149163
```scala mdoc:nest
150164
object wire:
@@ -161,7 +175,7 @@ object domain:
161175
@:select(underlying-code-3)
162176
@:choice(visible)
163177
```scala mdoc:nest
164-
given Mode.FailFast.Either[String] with {}
178+
given Mode.FailFast.Either[String]()
165179

166180
wire.ReleaseKind.Single
167181
.into[domain.ReleaseKind]
@@ -188,7 +202,7 @@ Docs.printCode(
188202
@:select(underlying-code-4)
189203
@:choice(visible)
190204
```scala mdoc:nest
191-
given Mode.FailFast.Either[String] with {}
205+
given Mode.FailFast.Either[String]()
192206

193207
// Type inference is tricky with this one. The function being passed in needs to be typed with the exact expected type.
194208
def handleSingle(value: wire.ReleaseKind): Either[String, domain.ReleaseKind] =
@@ -230,7 +244,7 @@ object domain:
230244
@:select(underlying-code-5)
231245
@:choice(visible)
232246
```scala mdoc:nest:silent
233-
given Mode.Accumulating.Either[String, List] with {}
247+
given Mode.Accumulating.Either[String, List]()
234248

235249
val customAccumulating =
236250
Transformer
@@ -258,7 +272,7 @@ And for the ones that are not keen on writing out method arguments:
258272
@:select(underlying-code-6)
259273
@:choice(visible)
260274
```scala mdoc:nest:silent
261-
given Mode.Accumulating.Either[String, List] with {}
275+
given Mode.Accumulating.Either[String, List]()
262276

263277
val customAccumulatingVia =
264278
Transformer

docs/fallible_transformations/definition_of_transformer_fallible_and_mode.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ So a `Fallible` transformer takes a `Source` and gives back a `Dest` wrapped in
1111

1212
```scala
1313
sealed trait Mode[F[+x]] {
14+
type Self[+A] = F[A]
15+
1416
def pure[A](value: A): F[A]
1517

1618
def map[A, B](fa: F[A], f: A => B): F[B]
@@ -20,6 +22,14 @@ sealed trait Mode[F[+x]] {
2022
transformation: A => F[B]
2123
)(using factory: Factory[B, BColl]): F[BColl]
2224
}
25+
26+
object Mode {
27+
inline def current(using mode: Mode[?]): mode.type = mode
28+
29+
extension [F[+x], M <: Mode[F]](self: M) {
30+
inline def locally[A](inline f: M ?=> A): A = f(using self)
31+
}
32+
}
2333
```
2434

2535
Moving on to `Mode`, what exactly is it and why do we need it? So a `Mode[F]` is typeclass that gives us two bits of information:
@@ -48,3 +58,23 @@ Each one of these exposes one operation that dictates its approach to errors, `f
4858
For accumulating transformations `ducktape` provides instances for `Either` with any subtype of `Iterable` on the left side, so that eg. `Mode.Accumulating[[A] =>> Either[List[String], A]]` is available out of the box (under the subclass of `Mode.Accumulating.Either[String, List]`).
4959

5060
For fail fast transformations, instances for `Option` (`Mode.FailFast.Option`) and `Either` (`Mode.FailFast.Either`) are avaiable out of the box.
61+
62+
As for the purpose of the `Self[+A]` type member, it's to enable use cases like these:
63+
64+
```scala mdoc
65+
import io.github.arainko.ducktape.*
66+
67+
val source =
68+
(
69+
Right(1),
70+
Right("str"),
71+
Right(List(3, 3, 3)),
72+
Right(4)
73+
)
74+
75+
Mode.Accumulating.either[String, List].locally {
76+
source.fallibleTo[Tuple.InverseMap[source.type, Mode.current.Self]]
77+
}
78+
```
79+
80+
...where repeatedly referring to the `F` wrapper becomes really unwieldly - that type is known to the compiler at each call site so we make it work for us in conjunction with `Mode.current` which summons the `Mode[F]` instance in the current implicit scope.

docs/fallible_transformations/making_the_most_out_of.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Fallible transformations wrapped in some type `F` are derived automatically for
6464
@:select(underlying-code-1)
6565
@:choice(visible)
6666
```scala mdoc
67-
given Mode.Accumulating.Either[String, List] with {}
67+
given Mode.Accumulating.Either[String, List]()
6868

6969
bad.fallibleTo[Person]
7070
good.fallibleTo[Person]
@@ -82,7 +82,7 @@ Same goes for instances that do fail fast transformations (you need `Mode.FailFa
8282
@:select(underlying-code-2)
8383
@:choice(visible)
8484
```scala mdoc:nest
85-
given Mode.FailFast.Either[String] with {}
85+
given Mode.FailFast.Either[String]()
8686

8787
bad.fallibleTo[Person]
8888
good.fallibleTo[Person]

docs/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
*ducktape* is a library for boilerplate-less and configurable transformations between case classes and enums/sealed traits for Scala 3. Directly inspired by [chimney](https://github.com/scalalandio/chimney).
66

7-
If this project interests you, please drop a 🌟 - these things are worthless but give me a dopamine rush nonetheless.
7+
If this project interests you, please [drop a 🌟](https://github.com/arainko/ducktape) - these things are worthless but give me a dopamine rush nonetheless.
88

99
## Installation
1010
```scala
@@ -231,3 +231,5 @@ Docs.printCode(
231231
@:@
232232

233233
Read more in the chapter dedicated to [configuring transformations](total_transformations/configuring_transformations.md).
234+
235+
To get an idea of what transformations are actually supported head on over to [transformation rules](transformation_rules.md).

docs/total_transformations/basics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,5 @@ Docs.printCode(
145145
@:@
146146

147147
Read more in the section about [configuring transformations](configuring_transformations.md).
148+
149+
To get an idea of what transformations are actually supported head on over to [transformation rules](../transformation_rules.md).

0 commit comments

Comments
 (0)