Skip to content

Commit

Permalink
Simplify imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lowmelvin committed Sep 13, 2024
1 parent 6768c5d commit c102200
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 68 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Encode a product:
```scala
import com.melvinlow.json.schema.*
import com.melvinlow.json.schema.generic.auto.given
import com.melvinlow.json.schema.instances.given

case class Foo(x: Int, y: String)

Expand Down
1 change: 0 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Encode a product:
```scala mdoc
import com.melvinlow.json.schema.*
import com.melvinlow.json.schema.generic.auto.given
import com.melvinlow.json.schema.instances.given

case class Foo(x: Int, y: String)

Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/com/melvinlow/json/schema/generic/auto.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import scala.deriving.Mirror

import io.circe.Json

import com.melvinlow.json.schema.JsonSchemaEncoder
import com.melvinlow.json.schema.annotation.JsonSchemaField
import com.melvinlow.json.schema.{JsonSchemaEncoder, instances}

object auto {
trait auto {
inline private def summonLabels[Elems <: Tuple]: List[String] =
inline erasedValue[Elems] match {
case _: (elem *: elems) =>
Expand Down Expand Up @@ -38,7 +38,7 @@ object auto {
case _: Elem =>
error("infinite recursive derivation")
case _ =>
auto.derived[Elem](using summonInline[Mirror.Of[Elem]])
derived[Elem](using summonInline[Mirror.Of[Elem]])
}

private def sumEncoder[T: Mirror.SumOf](
Expand Down Expand Up @@ -110,3 +110,5 @@ object auto {
inline given derivedSum[T: Mirror.SumOf]: JsonSchemaEncoder[T] =
derived[T]
}

object auto extends auto with semiauto with instances
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.melvinlow.json.schema.generic

import com.melvinlow.json.schema.instances

trait semiauto

object semiauto extends semiauto with instances
58 changes: 58 additions & 0 deletions src/main/scala/com/melvinlow/json/schema/instances.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.melvinlow.json.schema

import io.circe.Json

trait instances_low_priority {
given intJsonSchemaInstance[T <: Int]: JsonSchemaEncoder[T] with {
def schema: Json = Json.obj("type" -> Json.fromString("integer"))
}

given stringJsonSchemaInstance[T <: String]: JsonSchemaEncoder[T] with {
def schema: Json = Json.obj("type" -> Json.fromString("string"))
}

given longJsonSchemaInstance[T <: Long]: JsonSchemaEncoder[T] with {
def schema: Json = Json.obj("type" -> Json.fromString("integer"))
}

given doubleJsonSchemaInstance[T <: Double]: JsonSchemaEncoder[T] with {
def schema: Json = Json.obj("type" -> Json.fromString("number"))
}

given floatJsonSchemaEncoder[T <: Float]: JsonSchemaEncoder[T] with {
def schema: Json = Json.obj("type" -> Json.fromString("number"))
}

given booleanJsonSchemaEncoder[T <: Boolean]: JsonSchemaEncoder[T] with {
def schema: Json = Json.obj("type" -> Json.fromString("boolean"))
}

given listJsonSchemaEncoder[T: JsonSchemaEncoder]: JsonSchemaEncoder[List[T]]
with {
def schema: Json =
Json
.obj(
"type" -> Json.fromString("array"),
"items" -> JsonSchemaEncoder[T].schema
)
}

given arrayJsonSchemaEncoder[T: JsonSchemaEncoder]
: JsonSchemaEncoder[Array[T]]
with {
def schema: Json =
Json
.obj(
"type" -> Json.fromString("array"),
"items" -> JsonSchemaEncoder[T].schema
)
}
}

trait instances extends instances_low_priority {
given nullJsonSchemaEncoder: JsonSchemaEncoder[Null] with {
def schema: Json = Json.obj("type" -> Json.fromString("null"))
}
}

object instances extends instances
53 changes: 0 additions & 53 deletions src/main/scala/com/melvinlow/json/schema/instances/pkg.scala

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/scala/com/melvinlow/json/schema/syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.melvinlow.json.schema

import io.circe.Json

object syntax {
extension [T: JsonSchemaEncoder](inline dummy: T) {
inline def jsonSchema: Json = JsonSchemaEncoder[T].schema
}
}
9 changes: 0 additions & 9 deletions src/main/scala/com/melvinlow/json/schema/syntax/pkg.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.circe.Json
import com.melvinlow.json.schema.Resources.*
import com.melvinlow.json.schema.annotation.JsonSchemaField
import com.melvinlow.json.schema.generic.auto.given
import com.melvinlow.json.schema.instances.given
import com.melvinlow.json.schema.syntax.*

class JsonSchemaEncoderSuite extends munit.FunSuite {
Expand Down

0 comments on commit c102200

Please sign in to comment.