Skip to content

Commit

Permalink
Update scala-cli-md-spec to 0.1.0, fix workarounds for index.md with …
Browse files Browse the repository at this point in the history
…multi-file Scala CLI snippet
  • Loading branch information
MateuszKubuszok committed May 8, 2024
1 parent 8b7ef49 commit bd356ac
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 60 deletions.
72 changes: 39 additions & 33 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ What does it mean? Imagine you'd have to convert between this Protobuf-like defi
!!! example

```scala
// file: dto.scala - part of the demo
//> using scala {{ scala.3 }}
case class UserDTO(
name: String, // 1. primitive
addresses: Seq[AddressDTO], // 2. Seq collection
Expand All @@ -42,6 +44,7 @@ and this domain model:
!!! example

```scala
// file: domain.scala - part of the demo
case class User(
name: Username, // 1. value class
addresses: List[Address], // 2. List collection
Expand Down Expand Up @@ -71,19 +74,19 @@ From now on, forget about it! Encoding domain object with an infallible transfor
!!! example

```scala
// file: conversion.sc - part of the demo
//> using dep io.scalaland::chimney::{{ chimney_version() }}
import io.scalaland.chimney.dsl._

User(
Username("John"),
List(Address("Paper St", "Somewhere")),
RecoveryMethod.Email("[email protected]")
).transformInto[UserDTO]
// UserDTO(
// "John",
// Seq(AddressDTO("Paper St", "Somewhere")),
// Some(RecoveryMethodDTO.Email(EmailDTO("[email protected]")))
// )
println(
User(
Username("John"),
List(Address("Paper St", "Somewhere")),
RecoveryMethod.Email("[email protected]")
).transformInto[UserDTO]
)
// expected output:
// UserDTO(John,List(AddressDTO(Paper St,Somewhere)),Some(Email(EmailDTO([email protected]))))
```

??? example "Curious about the generated code?"
Expand Down Expand Up @@ -122,34 +125,37 @@ Done! Decoding Protobuf into domain object with a fallible transformation, like
!!! example

```scala
// file: conversion.sc - part of the demo
//> using dep io.scalaland::chimney::{{ chimney_version() }}
import io.scalaland.chimney.dsl._

UserDTO(
"John",
Seq(AddressDTO("Paper St", "Somewhere")),
Option(RecoveryMethodDTO.Email(EmailDTO("[email protected]")))
println(
UserDTO(
"John",
Seq(AddressDTO("Paper St", "Somewhere")),
Option(RecoveryMethodDTO.Email(EmailDTO("[email protected]")))
)
.transformIntoPartial[User]
.asEither
.left
.map(_.asErrorPathMessages)
)
.transformIntoPartial[User]
.asEither
.left
.map(_.asErrorPathMessages)
// Right(User(
// Username("John"),
// List(Address("Paper St", "Somewhere")),
// RecoveryMethod.Email("[email protected]")
// ))

UserDTO(
"John",
Seq(AddressDTO("Paper St", "Somewhere")),
None
// expected output:
// Right(User(Username(John),List(Address(Paper St,Somewhere)),Email([email protected])))

println(
UserDTO(
"John",
Seq(AddressDTO("Paper St", "Somewhere")),
None
)
.transformIntoPartial[User]
.asEither
.left
.map(_.asErrorPathMessages)
)
.transformIntoPartial[User]
.asEither
.left
.map(_.asErrorPathMessages)
// Left(List("recovery" -> EmptyValue))
// expected output:
// Left(List((recovery,EmptyValue)))
```

??? example "Curious about the generated code?"
Expand Down
78 changes: 51 additions & 27 deletions scripts/test-snippets.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala 3.3.3
//> using dep com.kubuszok::scala-cli-md-spec:0.0.2
//> using dep com.kubuszok::scala-cli-md-spec:0.1.0
//> using dep org.virtuslab::scala-yaml:0.0.8

import com.kubuszok.scalaclimdspec.*
Expand Down Expand Up @@ -52,47 +52,71 @@ class ChimneyExtendedRunner(runner: Runner)(
}

private val manuallyIgnored = ListMap(
"index.md[2]" -> "landing page",
"index.md[3]" -> "landing page",
"index.md[4]" -> "landing page",
"index.md[5]" -> "landing page",
"index.md[6]" -> "landing page",
"supported-transformations.md#Between `sealed`/`enum`s[2]" -> "snippet fails!!! investigate later", // FIXME
"supported-transformations.md#Between `sealed`/`enum`s[3]" -> "snippet throws exception!!! investigate later", // FIXME
"supported-transformations.md#Between `sealed`/`enum`s[4]" -> "snippet throws exception!!! investigate later", // FIXME
"supported-transformations.md#Java's `enum`s[1]" -> "requires previous snipper with Java code",
"supported-transformations.md#Java's `enum`s[2]" -> "requires previous snipper with Java code",
"supported-transformations.md#Handling a specific `sealed` subtype with a computed value[3]" -> "snippet throws exception!!! investigate later", // FIXME
"supported-transformations.md#Handling a specific `sealed` subtype with a computed value[4]" -> "requires previous snipper with Java code",
"supported-transformations.md#Handling a specific `sealed` subtype with a computed value[5]" -> "requires previous snipper with Java code",
"supported-transformations.md#Handling a specific `sealed` subtype with a computed value[6]" -> "requires previous snipper with Java code",
"supported-transformations.md#Between `sealed`/`enum`s[2]" -> "snippet fails!!! investigate later", // FIXME: https://github.com/scala/scala3/issues/20349
"supported-transformations.md#Between `sealed`/`enum`s[3]" -> "snippet throws exception!!! investigate later", // FIXME: https://github.com/scala/scala3/issues/20349
"supported-transformations.md#Between `sealed`/`enum`s[4]" -> "snippet throws exception!!! investigate later", // FIXME: https://github.com/scala/scala3/issues/20349
"supported-transformations.md#Java's `enum`s[1]" -> "requires previous snippet with Java code",
"supported-transformations.md#Java's `enum`s[2]" -> "requires previous snippet with Java code",
"supported-transformations.md#Handling a specific `sealed` subtype with a computed value[3]" -> "snippet throws exception!!! investigate later", // FIXME: https://github.com/scala/scala3/issues/20349
"supported-transformations.md#Handling a specific `sealed` subtype with a computed value[4]" -> "requires previous snippet with Java code",
"supported-transformations.md#Handling a specific `sealed` subtype with a computed value[5]" -> "requires previous snippet with Java code",
"supported-transformations.md#Handling a specific `sealed` subtype with a computed value[6]" -> "requires previous snippet with Java code",
"supported-transformations.md#Types with manually provided constructors[3]" -> "example split into multiple files",
"supported-transformations.md#Types with manually provided constructors[4]" -> "contunuation from the previous snippet",
"supported-transformations.md#Types with manually provided constructors[5]" -> "example split into multiple files",
"supported-transformations.md#Types with manually provided constructors[6]" -> "contunuation from the previous snippet",
"supported-transformations.md#Defining custom name matching predicate[1]" -> "example split into multiple files",
"supported-transformations.md#Defining custom name matching predicate[2]" -> "contunuation from the previous snippet",
"troubleshooting.md#Ducktape[2]" -> "snippet throws exception!!! investigate later", // FIXME
"troubleshooting.md#Ducktape[4]" -> "snippet throws exception!!! investigate later", // FIXME
"troubleshooting.md#Ducktape[8]" -> "snippet throws exception!!! investigate later", // FIXME
"troubleshooting.md#Ducktape[10]" -> "snippet throws exception!!! investigate later" // FIXME
"troubleshooting.md#Ducktape[2]" -> "snippet throws exception!!! investigate later", // FIXME: https://github.com/scala/scala3/issues/20349
"troubleshooting.md#Ducktape[4]" -> "snippet throws exception!!! investigate later", // FIXME: https://github.com/scala/scala3/issues/20349
"troubleshooting.md#Ducktape[8]" -> "snippet throws exception!!! investigate later", // FIXME: https://github.com/scala/scala3/issues/20349
"troubleshooting.md#Ducktape[10]" -> "snippet throws exception!!! investigate later" // FIXME: https://github.com/scala/scala3/issues/20349
)

val addDefaultScala: Snippet.Content => Snippet.Content = {
case Snippet.Content.Single(content) =>
Snippet.Content.Single(
if content.contains("//> using scala") then content
else s"//> using scala $defaultScalaVersion\n$content"
)
case Snippet.Content.Multiple(files) =>
Snippet.Content.Multiple(
if files.values.exists(_.content.contains("//> using scala")) then files
else {
val (fileName, Snippet.Content.Single(content0)) = files.head
val content1: Snippet.Content.Single =
Snippet.Content.Single(s"//> using scala $defaultScalaVersion\n$content0")
ListMap(fileName -> content1) ++ files.tail
}
)
}

val interpolateTemplates: Snippet.Content => Snippet.Content = {
case Snippet.Content.Single(content) =>
Snippet.Content.Single(replacePatterns.foldLeft(content) { case (s, (k, v)) => s.replaceAll(k, v) })
case Snippet.Content.Multiple(files) =>
Snippet.Content.Multiple(
ListMap.from(files.mapValues(interpolateTemplates(_).asInstanceOf[Snippet.Content.Single]))
)
}

export runner.{docsDir, filter, tmpDir}

extension (snippet: Snippet)
def adjusted: Snippet = runner.adjusted(
snippet.copy(content =
replacePatterns.foldLeft(
if snippet.content.contains("//> using scala") then snippet.content
else s"//> using scala $defaultScalaVersion\n${snippet.content}"
) { case (s, (k, v)) => s.replaceAll(k, v) }
)
)
def adjusted: Snippet =
// interpolate templates before Default adjustments
runner.adjusted(snippet.copy(content = interpolateTemplates(snippet.content)))

def howToRun: Runner.Strategy = manuallyIgnored.get(snippet.stableName) match
case None => runner.howToRun(snippet)
case Some(reason) => Runner.Strategy.Ignore(reason)

extension (snippets: List[Snippet])
def adjusted: List[Snippet] = runner.adjusted(snippets).map { snippet =>
howToRun(snippet) match
case Runner.Strategy.Ignore(_) => snippet
case _ => snippet.copy(content = addDefaultScala(snippet.content))
}
}

/** Usage:
Expand Down

0 comments on commit bd356ac

Please sign in to comment.