-
Notifications
You must be signed in to change notification settings - Fork 27
/
circemapping.scala
179 lines (146 loc) · 7.39 KB
/
circemapping.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2016-2020 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause
package edu.gemini.grackle
package circe
import scala.collection.Factory
import cats.Monad
import cats.implicits._
import fs2.Stream
import io.circe.Json
import io.circe.Encoder
import org.tpolecat.sourcepos.SourcePos
import Cursor.{Context, DeferredCursor, Env}
import QueryInterpreter.{mkErrorResult, mkOneError}
import ScalarType._
abstract class CirceMapping[F[_]](implicit val M: Monad[F]) extends Mapping[F] with CirceMappingLike[F]
trait CirceMappingLike[F[_]] extends Mapping[F] {
// Syntax to allow Circe-specific root effects
implicit class CirceMappingRootEffectSyntax(self: RootEffect.type) {
def computeJson(fieldName: String)(effect: (Query, Path, Env) => F[Result[Json]])(implicit pos: SourcePos): RootEffect =
self.computeCursor(fieldName)((q, p, e) => effect(q, p, e).map(_.map(circeCursor(p, e, _))))
def computeJsonStream(fieldName: String)(effect: (Query, Path, Env) => Stream[F, Result[Json]])(implicit pos: SourcePos): RootEffect =
self.computeCursorStream(fieldName)((q, p, e) => effect(q, p, e).map(_.map(circeCursor(p, e, _))))
def computeEncodable[A](fieldName: String)(effect: (Query, Path, Env) => F[Result[A]])(implicit pos: SourcePos, enc: Encoder[A]): RootEffect =
computeJson(fieldName)((q, p, e) => effect(q, p, e).map(_.map(enc(_))))
def computeEncodableStream[A](fieldName: String)(effect: (Query, Path, Env) => Stream[F, Result[A]])(implicit pos: SourcePos, enc: Encoder[A]): RootEffect =
computeJsonStream(fieldName)((q, p, e) => effect(q, p, e).map(_.map(enc(_))))
}
def circeCursor(path: Path, env: Env, value: Json): Cursor =
if(path.isRoot)
CirceCursor(Context(path.rootTpe), value, None, env)
else
DeferredCursor(path, (context, parent) => CirceCursor(context, value, Some(parent), env).rightIor)
override def mkCursorForField(parent: Cursor, fieldName: String, resultName: Option[String]): Result[Cursor] = {
val context = parent.context
val fieldContext = context.forFieldOrAttribute(fieldName, resultName)
(fieldMapping(context, fieldName), parent.focus) match {
case (Some(CirceField(_, json, _)), _) =>
CirceCursor(fieldContext, json, Some(parent), parent.env).rightIor
case (Some(CursorFieldJson(_, f, _, _)), _) =>
f(parent).map(res => CirceCursor(fieldContext, focus = res, parent = Some(parent), env = parent.env))
case (None|Some(RootEffect(_, _)), json: Json) =>
val f = json.asObject.flatMap(_(fieldName))
f match {
case None if fieldContext.tpe.isNullable => CirceCursor(fieldContext, Json.Null, Some(parent), parent.env).rightIor
case Some(json) => CirceCursor(fieldContext, json, Some(parent), parent.env).rightIor
case _ =>
mkErrorResult(s"No field '$fieldName' for type ${context.tpe}")
}
case _ =>
super.mkCursorForField(parent, fieldName, resultName)
}
}
sealed trait CirceFieldMapping extends FieldMapping {
def withParent(tpe: Type): FieldMapping = this
}
case class CirceField(fieldName: String, value: Json, hidden: Boolean = false)(implicit val pos: SourcePos) extends CirceFieldMapping
case class CursorFieldJson(fieldName: String, f: Cursor => Result[Json], required: List[String], hidden: Boolean = false)(
implicit val pos: SourcePos
) extends CirceFieldMapping
case class CirceCursor(
context: Context,
focus: Json,
parent: Option[Cursor],
env: Env
) extends Cursor {
def withEnv(env0: Env): Cursor = copy(env = env.add(env0))
def mkChild(context: Context = context, focus: Json = focus): CirceCursor =
CirceCursor(context, focus, Some(this), Env.empty)
def isLeaf: Boolean =
tpe.dealias match {
case BooleanType => focus.isBoolean
case StringType|IDType => focus.isString
case IntType|FloatType => focus.isNumber
case _: EnumType => focus.isString
case _ => false
}
def asLeaf: Result[Json] =
tpe.dealias match {
case BooleanType if focus.isBoolean => focus.rightIor
case StringType|IDType if focus.isString => focus.rightIor
case IntType if focus.isNumber =>
focus.asNumber.flatMap(_.toLong.map(Json.fromLong))
.toRightIor(mkOneError(s"Expected Int found ${focus.noSpaces}"))
case FloatType if focus.isNumber => focus.rightIor
case e: EnumType if focus.isString =>
if (focus.asString.map(e.hasValue).getOrElse(false)) focus.rightIor
else mkErrorResult(s"Expected Enum ${e.name}, found ${focus.noSpaces}")
case _: ScalarType if !focus.isObject => focus.rightIor // custom Scalar; any non-object type is fine
case _ =>
mkErrorResult(s"Expected Scalar type, found $tpe for focus ${focus.noSpaces} at ${context.path.reverse.mkString("/")} ")
}
def preunique: Result[Cursor] = {
val listTpe = tpe.nonNull.list
if(focus.isArray)
mkChild(context.asType(listTpe), focus).rightIor
else
mkErrorResult(s"Expected List type, found $focus for ${listTpe}")
}
def isList: Boolean = tpe.isList && focus.isArray
def asList[C](factory: Factory[Cursor, C]): Result[C] = tpe match {
case ListType(elemTpe) if focus.isArray =>
focus.asArray.map(_.view.map(e => mkChild(context.asType(elemTpe), e)).to(factory))
.toRightIor(mkOneError(s"Expected List type, found $tpe for focus ${focus.noSpaces}"))
case _ =>
mkErrorResult(s"Expected List type, found $tpe for focus ${focus.noSpaces}")
}
def listSize: Result[Int] = tpe match {
case ListType(_) if focus.isArray =>
focus.asArray.map(_.size)
.toRightIor(mkOneError(s"Expected List type, found $tpe for focus ${focus.noSpaces}"))
case _ =>
mkErrorResult(s"Expected List type, found $tpe for focus ${focus.noSpaces}")
}
def isNullable: Boolean = tpe.isNullable
def asNullable: Result[Option[Cursor]] = tpe match {
case NullableType(tpe) =>
if (focus.isNull) None.rightIor
else Some(mkChild(context.asType(tpe))).rightIor
case _ => mkErrorResult(s"Expected Nullable type, found $focus for $tpe")
}
def isDefined: Result[Boolean] = tpe match {
case NullableType(_) => (!focus.isNull).rightIor
case _ => mkErrorResult(s"Expected Nullable type, found $focus for $tpe")
}
def narrowsTo(subtpe: TypeRef): Boolean =
subtpe <:< tpe &&
((subtpe.dealias, focus.asObject) match {
case (nt: TypeWithFields, Some(obj)) =>
nt.fields.forall { f =>
f.tpe.isNullable || obj.contains(f.name)
} && obj.keys.forall(nt.hasField)
case _ => false
})
def narrow(subtpe: TypeRef): Result[Cursor] =
if (narrowsTo(subtpe))
mkChild(context.asType(subtpe)).rightIor
else
mkErrorResult(s"Focus ${focus} of static type $tpe cannot be narrowed to $subtpe")
def hasField(fieldName: String): Boolean =
fieldMapping(context, fieldName).isDefined ||
tpe.hasField(fieldName) && focus.asObject.map(_.contains(fieldName)).getOrElse(false)
def field(fieldName: String, resultName: Option[String]): Result[Cursor] = {
mkCursorForField(this, fieldName, resultName)
}
}
}