-
Notifications
You must be signed in to change notification settings - Fork 27
/
mappingvalidator.scala
303 lines (263 loc) · 11.1 KB
/
mappingvalidator.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// 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
import cats._
import cats.data.{ Chain, NonEmptyList }
import cats.implicits._
import scala.io.AnsiColor
import scala.util.control.NoStackTrace
trait MappingValidator {
type F[_]
type M <: Mapping[F]
val mapping: M
import MappingValidator._
import mapping._
/** Can't validate this kind of `FieldMapping`. */
case class CannotValidateTypeMapping(typeMapping: TypeMapping)
extends Failure(Severity.Info, typeMapping.tpe, None) {
override def toString: String =
s"$productPrefix(${typeMapping.tpe}, ${typeMapping.productPrefix})"
override def formattedMessage: String =
s"""|The ${typeMapping.productPrefix} for ${typeMapping.tpe} cannot be validated.
|""".stripMargin
}
/** Can't validate this kind of `FieldMapping`. */
case class CannotValidateFieldMapping(owner: ObjectType, field: Field, fieldMapping: FieldMapping)
extends Failure(Severity.Info, owner, Some(fieldMapping.fieldName)) {
override def toString: String =
s"$productPrefix($owner.${field.name}:${field.tpe}, ${fieldMapping.productPrefix})"
override def formattedMessage: String =
s"""|Field mapping cannot be validated.
|
|- Field ${graphql(s"$owner.${field.name}: ${field.tpe}")} is defined by a Schema at (1).
|- Its mapping to Scala is defined by a ${scala(fieldMapping.productPrefix)} at (2).
|- ${UNDERLINED}This kind of mapping canont be validated.$RESET Ensure you have unit tests.
|
|(1) ${schema.pos}
|(2) ${fieldMapping.pos}
|""".stripMargin
}
/** Object type `owner` declares `field` but no such mapping exists. */
case class MissingFieldMapping(owner: ObjectMapping, field: Field)
extends Failure(Severity.Error, owner.tpe, Some(field.name)) {
override def toString: String =
s"$productPrefix(${owner.tpe}.${field.name}:${field.tpe})"
override def formattedMessage: String =
s"""|Missing field mapping.
|
|- Field ${graphql(s"${owner.tpe}.${field.name}: ${field.tpe}")} is defined by a Schema at (1).
|- The ${scala(owner.productPrefix)} for ${graphql(owner.tpe)} at (2) ${UNDERLINED}does not define a mapping for this field$RESET.
|
|(1) ${schema.pos}
|(2) ${owner.pos}
|""".stripMargin
}
/** GraphQL type isn't applicable for mapping type. */
case class InapplicableGraphQLType(typeMapping: TypeMapping, expected: String)
extends Failure(Severity.Error, typeMapping.tpe, None) {
override def toString: String =
s"$productPrefix(${typeMapping.productPrefix}, ${typeMapping.tpe.productPrefix})"
override def formattedMessage: String =
s"""|Inapplicable GraphQL type.
|
|- Type ${graphql(typeMapping.tpe)} (${scala(typeMapping.tpe.dealias.productPrefix)}) is defined by a Schema at (1).
|- It is mapped by a ${graphql(typeMapping.productPrefix)} at (2), which expects ${scala(expected)}.
|- ${UNDERLINED}Use a different kind of mapping for this type.$RESET
|
|(1) ${schema.pos}
|(2) ${typeMapping.pos}
|""".stripMargin
}
/** Referenced type does not exist. */
case class ReferencedTypeDoesNotExist(typeMapping: TypeMapping)
extends Failure(Severity.Error, typeMapping.tpe, None) {
override def toString: String =
s"$productPrefix(${typeMapping.productPrefix}, ${typeMapping.tpe})"
override def formattedMessage: String =
s"""|Referenced type does not exist.
|
|- A ${graphql(typeMapping.productPrefix)} at (1) references type ${graphql(typeMapping.tpe)}.
|- ${UNDERLINED}This type is undeclared$RESET in referenced Schema at (2).
|
|(1) ${typeMapping.pos}
|(2) ${schema.pos}
|""".stripMargin
}
/** Referenced field does not exist. */
case class ReferencedFieldDoesNotExist(objectMapping: ObjectMapping, fieldMapping: FieldMapping)
extends Failure(Severity.Error, objectMapping.tpe, Some(fieldMapping.fieldName)) {
override def toString: String =
s"$productPrefix(${objectMapping.tpe}.${fieldMapping.fieldName})"
override def formattedMessage: String =
s"""|Referenced field does not exist.
|
|- ${objectMapping.tpe} is defined in a Schema at (1).
|- A ${graphql(objectMapping.productPrefix)} at (2) references field ${graphql(fieldMapping.fieldName)}.
|- ${UNDERLINED}This field does not exist in the Schema.$RESET
|
|(1) ${schema.pos}
|(1) ${objectMapping.pos}
|""".stripMargin
}
/** Missing type mapping. */
case class MissingTypeMapping(tpe: Type)
extends Failure(Severity.Error, tpe, None) {
override def toString: String =
s"$productPrefix($tpe)"
override def formattedMessage: String =
s"""|Missing type mapping.
|
|- ${tpe} is defined in a Schema at (1).
|- ${UNDERLINED}No mapping was found for this type.$RESET
|
|(1) ${schema.pos}
|""".stripMargin
}
/**
* Run this validator, yielding a chain of `Failure`s of severity equal to or greater than the
* specified `Severity`.
*/
def validateMapping(severity: Severity = Severity.Warning): List[Failure] =
List(
missingTypeMappings,
typeMappings.foldMap(validateTypeMapping).filter(_.severity >= severity)
).foldMap(_.toList)
/**
* Run this validator, raising a `ValidationException` in `G` if there are any failures of
* severity equal to or greater than the specified `Severity`.
*/
def validate[G[_]](severity: Severity = Severity.Warning)(
implicit ev: ApplicativeError[G, Throwable]
): G[Unit] =
NonEmptyList.fromList(validateMapping(severity)).foldMapA(nec => ev.raiseError(ValidationException(nec)))
/**
* Run this validator, raising a `ValidationException` if there are any failures of severity equal
* to or greater than the specified `Severity`.
*/
def unsafeValidate(severity: Severity = Severity.Warning): Unit =
validate[Either[Throwable, *]](severity).fold(throw _, _ => ())
protected def missingTypeMappings: Chain[Failure] =
schema.types.filter {
case _: InputObjectType => false
case tpe => typeMapping(tpe).isEmpty
}.foldMap { tpe =>
Chain(MissingTypeMapping(tpe))
}
protected def validateTypeMapping(tm: TypeMapping): Chain[Failure] = {
if (!tm.tpe.dealias.exists) Chain(ReferencedTypeDoesNotExist(tm))
else tm match {
case om: ObjectMapping => validateObjectMapping(om)
case lm: LeafMapping[_] => validateLeafMapping(lm)
case _: PrimitiveMapping => Chain.empty
case tm => Chain(CannotValidateTypeMapping(tm))
}
}
protected def validateLeafMapping(lm: LeafMapping[_]): Chain[Failure] =
lm.tpe.dealias match {
case (_: ScalarType)|(_: EnumType)|(_: ListType) =>
Chain.empty // these are valid on construction. Nothing to do.
case _ => Chain(InapplicableGraphQLType(lm, "Leaf Type"))
}
protected def validateFieldMapping(owner: ObjectType, field: Field, fieldMapping: FieldMapping): Chain[Failure] =
Chain(CannotValidateFieldMapping(owner, field, fieldMapping))
/** All interfaces for `t`, transtiviely, including `t` if it is an interface. */
protected def interfaces(t: Type): List[InterfaceType] =
t.dealias match {
case it: InterfaceType => it :: it.interfaces.flatMap(interfaces)
case ot: ObjectType => ot.interfaces.flatMap(interfaces)
case _ => Nil
}
/**
* Mappings for all fields defined transitively for interfaces of `tpe`, including `tpe` if it
* is an interface.
*/
protected def transitiveInterfaceFieldMappings(tpe: Type): List[FieldMapping] =
interfaces(tpe).flatMap { iface =>
typeMapping(iface).toList.collect {
case om: ObjectMapping => om.fieldMappings
}.flatten
}
protected def validateObjectFieldMappings(m: ObjectMapping, tpe: ObjectType): Chain[Failure] = {
val fms = m.fieldMappings ++ transitiveInterfaceFieldMappings(tpe)
val missing = tpe.fields.foldMap { f =>
fms.find(_.fieldName == f.name) match {
case Some(fm) => validateFieldMapping(tpe, f, fm)
case None => Chain(MissingFieldMapping(m, f))
}
}
val unknown = fms.foldMap { fm =>
tpe.fields.find(_.name == fm.fieldName || fm.hidden) match {
case Some(_) => Chain.empty
case None => Chain(ReferencedFieldDoesNotExist(m, fm))
}
}
missing ++ unknown
}
protected def validateObjectMapping(m: ObjectMapping): Chain[Failure] =
m.tpe.dealias match {
case ot: ObjectType => validateObjectFieldMappings(m, ot)
case _: InterfaceType => Chain(CannotValidateTypeMapping(m))
case _ => Chain(InapplicableGraphQLType(m, "ObjectType"))
}
}
object MappingValidator {
def apply[G[_]](m: Mapping[G]): MappingValidator =
new MappingValidator {
type F[a] = G[a]
type M = Mapping[F]
val mapping = m
}
sealed trait Severity extends Product
object Severity {
case object Error extends Severity
case object Warning extends Severity
case object Info extends Severity
implicit val OrderSeverity: Order[Severity] =
Order.by {
case Error => 3
case Warning => 2
case Info => 1
}
}
abstract class Failure(
val severity: Severity,
val graphQLTypeName: String,
val fieldName: Option[String],
) extends AnsiColor {
def this(
severity: Severity,
tpe: Type,
fieldName: Option[String],
) = this(severity, tpe.toString, fieldName)
protected def formattedMessage: String = s"$toString (no detail given)"
private val prefix: String =
severity match {
case Severity.Error => "🛑 "
case Severity.Warning => "⚠️ "
case Severity.Info => "ℹ️ "
}
protected def graphql(a: Any) = s"$BLUE$a$RESET"
protected def scala(a: Any) = s"$RED$a$RESET"
protected def sql(a: Any) = s"$GREEN$a$RESET"
final def toErrorMessage: String =
s"""|$formattedMessage
|Color Key: ${scala("◼")} Scala | ${graphql("◼")} GraphQL | ${sql("◼")} SQL
|""".stripMargin.linesIterator.mkString(s"$prefix\n$prefix", s"\n$prefix", s"\n$prefix\n")
}
final case class ValidationException(failures: NonEmptyList[Failure]) extends RuntimeException with NoStackTrace {
override def getMessage(): String =
s"\n\n${failures.foldMap(_.toErrorMessage)}\n"
}
}