-
Notifications
You must be signed in to change notification settings - Fork 27
/
circemapping.scala
191 lines (160 loc) · 7.83 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
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA)
// Copyright (c) 2016-2023 Grackle Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grackle
package circe
import scala.collection.Factory
import cats.MonadThrow
import cats.implicits._
import fs2.Stream
import io.circe.Json
import io.circe.Encoder
import org.tpolecat.sourcepos.SourcePos
import syntax._
import Cursor.DeferredCursor
import ScalarType._
abstract class CirceMapping[F[_]](implicit val M: MonadThrow[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: (Path, Env) => F[Result[Json]])(implicit pos: SourcePos): RootEffect =
self.computeCursor(fieldName)((p, e) => effect(p, e).map(_.map(circeCursor(p, e, _))))
def computeEncodable[A](fieldName: String)(effect: (Path, Env) => F[Result[A]])(implicit pos: SourcePos, enc: Encoder[A]): RootEffect =
computeJson(fieldName)((p, e) => effect(p, e).map(_.map(enc(_))))
}
implicit class CirceMappingRootStreamSyntax(self: RootStream.type) {
def computeJson(fieldName: String)(effect: (Path, Env) => Stream[F, Result[Json]])(implicit pos: SourcePos): RootStream =
self.computeCursor(fieldName)((p, e) => effect(p, e).map(_.map(circeCursor(p, e, _))))
def computeEncodable[A](fieldName: String)(effect: (Path, Env) => Stream[F, Result[A]])(implicit pos: SourcePos, enc: Encoder[A]): RootStream =
computeJson(fieldName)((p, e) => effect(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).success)
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).success
case (Some(CursorFieldJson(_, f, _, _)), _) =>
f(parent).map(res => CirceCursor(fieldContext, focus = res, parent = Some(parent), env = parent.env))
case (None|Some(_: EffectMapping), json: Json) =>
val f = json.asObject.flatMap(_(fieldName))
f match {
case None if fieldContext.tpe.isNullable => CirceCursor(fieldContext, Json.Null, Some(parent), parent.env).success
case Some(json) => CirceCursor(fieldContext, json, Some(parent), parent.env).success
case _ =>
Result.failure(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.success
case StringType|IDType if focus.isString => focus.success
case IntType if focus.isNumber =>
focus.asNumber.flatMap(_.toLong.map(Json.fromLong))
.toResultOrError(s"Expected Int found ${focus.noSpaces}")
case FloatType if focus.isNumber => focus.success
case e: EnumType if focus.isString =>
if (focus.asString.exists(e.hasValue)) focus.success
else Result.internalError(s"Expected Enum ${e.name}, found ${focus.noSpaces}")
case _: ScalarType if !focus.isObject => focus.success // custom Scalar; any non-object type is fine
case _ =>
Result.internalError(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).success
else
Result.internalError(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))
.toResultOrError(s"Expected List type, found $tpe for focus ${focus.noSpaces}")
case _ =>
Result.internalError(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)
.toResultOrError(s"Expected List type, found $tpe for focus ${focus.noSpaces}")
case _ =>
Result.internalError(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.success
else Some(mkChild(context.asType(tpe))).success
case _ => Result.internalError(s"Expected Nullable type, found $focus for $tpe")
}
def isDefined: Result[Boolean] = tpe match {
case NullableType(_) => (!focus.isNull).success
case _ => Result.internalError(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)).success
else
Result.internalError(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.exists(_.contains(fieldName))
def field(fieldName: String, resultName: Option[String]): Result[Cursor] = {
mkCursorForField(this, fieldName, resultName)
}
}
}