Skip to content

Commit

Permalink
Some tidyup
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Jun 29, 2023
1 parent 494f386 commit 8f6dc8c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/core/schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ import scala.compiletime.*
import unsafeExceptions.canThrowAny

object JsonRecord:
given ValueAccessor[JsonRecord, JsonAst, "boolean", Boolean] = _.asInstanceOf[Boolean]
given ValueAccessor[JsonRecord, JsonAst, "string", Text] = _.asInstanceOf[Text]
given ValueAccessor[JsonRecord, JsonAst, "integer", Int] = _.asInstanceOf[Long].toInt
given ValueAccessor[JsonRecord, JsonAst, "boolean", Boolean] = _.boolean
given ValueAccessor[JsonRecord, JsonAst, "string", Text] = _.string
given ValueAccessor[JsonRecord, JsonAst, "integer", Int] = _.long.toInt

given ValueAccessor[JsonRecord, JsonAst, "number", Double] =
given ValueAccessor[JsonRecord, JsonAst, "number", Double] = _.asMatchable match
case long: Long => long.toDouble
case decimal: BigDecimal => decimal.toDouble
case double: Double => double
case _ => throw JsonSchemaError()

given RecordAccessor[JsonRecord, JsonAst, "array", IArray] = _.asInstanceOf[IArray[JsonAst]].map(_)
given RecordAccessor[JsonRecord, JsonAst, "array", IArray] = _.array.map(_)
given RecordAccessor[JsonRecord, JsonAst, "object", [T] =>> T] = (value, make) => make(value)

class JsonRecord(data: JsonAst, access: String => JsonAst => Any)
Expand All @@ -61,9 +62,8 @@ object JsonSchema:
case other => RecordField.Value(other)

abstract class JsonSchema(val doc: JsonSchemaDoc) extends Schema[JsonAst, JsonRecord]:
def access(name: String, json: JsonAst): JsonAst =
json.asInstanceOf[JsonAst].obj match
case (keys: IArray[String], values: IArray[JsonAst]) => values(keys.indexOf(name))
def access(name: String, json: JsonAst): JsonAst = json.obj match
case (keys: IArray[String], values: IArray[JsonAst]) => values(keys.indexOf(name))

def make(data: JsonAst, access: String => JsonAst => Any): JsonRecord = JsonRecord(data, access)
def fields: Map[String, RecordField] = unsafely(doc.fields)
Expand Down
1 change: 1 addition & 0 deletions src/test/example.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ object ExampleSchema extends JsonSchema(unsafely(Json.parse(t"""{
import RecordField.*

transparent inline def record(json: JsonAst): JsonRecord = ${build('json)}

0 comments on commit 8f6dc8c

Please sign in to comment.