Skip to content

Commit

Permalink
adding docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrapyre committed May 7, 2024
1 parent 779c090 commit a93108c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
8 changes: 8 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ lazy val docs = project
mainModuleName := (zioSchemaJVM / moduleName).value,
projectStage := ProjectStage.Development,
ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(),
mdocVariables ++= Map(
"STANDARD_TYPE_TABLE" -> {
val docGen = DocGenerator.generateStandardTypeTable(
(ThisBuild / baseDirectory).value / "zio-schema" / "jvm" / "src" / "main" / "scala" / "zio" / "schema" / "StandardType.scala"
)
docGen
}
),
readmeContribution +=
"""|
|#### TL;DR
Expand Down
14 changes: 4 additions & 10 deletions docs/basic-building-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,12 @@ case class Primitive[A](standardType: StandardType[A]) extends Schema[A]

Primitive values are represented using the `Primitive[A]` type class and represent the elements, that we cannot further define through other means. If we visualize our data structure as a tree, primitives are the leaves.

ZIO Schema provides a number of built-in primitive types, that we can use to represent our data. These can be found in the [`StandardType`](https://github.com/zio/zio-schema/blob/main/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala) companion-object:
### Standard Type Reference
ZIO Schema provides a number of built-in primitive types, that we can use to represent our data. These can be seen in the following table:

```scala
sealed trait StandardType[A]
object StandardType {
implicit object UnitType extends StandardType[Unit]
implicit object StringType extends StandardType[String]
implicit object BoolType extends StandardType[Boolean]
// ...
}
```
@STANDARD_TYPE_TABLE@

### Primitive Schemas
Inside `Schema`'s companion object, we have an implicit conversion from `StandardType[A]` to `Schema[A]`:

```scala
Expand Down
55 changes: 55 additions & 0 deletions project/DocGenerator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.io.File
import scala.io.Source

object DocGenerator {

final private case class StandardTypeForDoc(
name: String,
isJVMSupported: Boolean = true,
isJSSupported: Boolean = true,
isNativeSupported: Boolean = true
)

private def convertBooleanToText(bool: Boolean): String =
if (bool) {
"Yes"
} else {
"No"
}

def generateStandardTypeTable(standardTypeFile: File): String = {
val source = Source.fromFile(standardTypeFile)
val standardTypeRegex = "StandardType\\[.*".r
val suffixRegex = "].*".r
var unsortedStandardTypes: Vector[StandardTypeForDoc] = Vector.empty
var markdownFile =
"|Standard Type|JVM Support|ScalaJS Support|Scala Native Support|\n|--------------|:--------------:|:--------------:|:--------------:|"

for (line <- source.getLines()) {
if (line.contains("implicit object")) {
val unparsedLine = standardTypeRegex
.findFirstIn(line)
.getOrElse("Expected StandardType to be present in line while parsing standard type doc")
val trimmedLine = unparsedLine.trim()
val lineWithoutPrefix = trimmedLine.replace("StandardType[", "")
val standardTypeStr = suffixRegex.replaceFirstIn(lineWithoutPrefix, "")
val standardTypeForDoc = standardTypeStr match {
case typ @ "java.util.UUID" => StandardTypeForDoc(typ, isJSSupported = false)
case typ @ "java.util.Currency" => StandardTypeForDoc(typ, isJSSupported = false, isNativeSupported = false)
case typ @ "Chunk[Byte" => StandardTypeForDoc("Chunk[Byte]")
case typ => StandardTypeForDoc(typ)
}
unsortedStandardTypes = unsortedStandardTypes :+ standardTypeForDoc
}
}

val sortedStandardTypes = unsortedStandardTypes.sortBy(_.name)
sortedStandardTypes.foreach { standardType =>
val jsSupport = convertBooleanToText(standardType.isJSSupported)
val jvmSupport = convertBooleanToText(standardType.isJVMSupported)
val nativeSupport = convertBooleanToText(standardType.isNativeSupported)
markdownFile += s"\n|`${standardType.name}`|$jvmSupport|$jsSupport|$nativeSupport|"
}
markdownFile
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ object StandardType {
}

//java.time specific types
implicit object DayOfWeekType extends StandardType[DayOfWeek] {
implicit object DayOfWeekType extends StandardType[java.time.DayOfWeek] {
override def tag: String = Tags.DAY_OF_WEEK
override def compare(x: DayOfWeek, y: DayOfWeek): Int = x.getValue.compareTo(y.getValue)
override def defaultValue: Either[String, DayOfWeek] =
Expand Down

0 comments on commit a93108c

Please sign in to comment.