-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArgParsing.scala
240 lines (217 loc) · 10.1 KB
/
ArgParsing.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
/*
* Copyright 2023 Valdemar Grange
*
* 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 gql.preparation
import cats._
import cats.data._
import cats.implicits._
import cats.mtl._
import gql._
import gql.ast._
import gql.parser.AnyValue
import gql.parser.NonVar
import gql.parser.{Value => V}
trait ArgParsing[F[_], C] {
def decodeIn[A](
a: In[A],
value: V[AnyValue, List[C]],
ambigiousEnum: Boolean
): F[A]
def decodeArg[A](
arg: Arg[A],
values: Map[String, V[AnyValue, List[C]]],
ambigiousEnum: Boolean,
context: List[C]
): F[A]
}
object ArgParsing {
type UsedVariables = Set[String]
def apply[F[_]: Parallel, C](
variables: VariableMap[C]
)(implicit
F: Monad[F],
L: Local[F, Cursor],
H: Handle[F, NonEmptyChain[PositionalError[C]]],
T: Tell[F, UsedVariables]
): ArgParsing[F, C] = new ArgParsing[F, C] {
val E = ErrorAlg.errorAlgForHandle[F, NonEmptyChain, C]
val P = PathAlg[F]
import E._
import P._
override def decodeIn[A](a: In[A], value: V[AnyValue, List[C]], ambigiousEnum: Boolean): F[A] = {
(a, value) match {
case (_, V.VariableValue(vn, cs)) =>
T.tell(Set(vn)) *> {
variables.get(vn) match {
case None =>
raise(
s"Variable '$$$vn' was not declared and provided as a possible variable for this operation. Hint add the variable to the variables list of the operation '(..., $$$vn: ${ModifierStack
.fromIn(a)
.show(_.name)})' and provide a value in the variables parameter.",
cs
)
case Some(v) =>
val parseInnerF: F[A] = v.value match {
case Right(pval) => decodeIn(a, pval.map(c2 => c2 :: cs), ambigiousEnum = false)
case Left(j) => decodeIn(a, V.fromJson(j).as(cs), ambigiousEnum = true)
}
val vt: ModifierStack[String] = ModifierStack.fromType(v.tpe)
val at = ModifierStack.fromIn(a)
def showType(xs: List[Modifier], name: String): String =
ModifierStack(xs, name).show(identity)
def showVarType(xs: List[Modifier]): String =
showType(xs, vt.inner)
def showArgType(xs: List[Modifier]): String =
showType(xs, at.inner.name)
lazy val prefix =
s"Variable '$$${vn}' of type `${vt.show(identity)}` was not compatible with expected argument type `${at.map(_.name).show(identity)}`"
def remaining(vs: List[Modifier], as: List[Modifier]): String =
if (vs.size == vt.modifiers.size && as.size == at.modifiers.size) "."
else
s". The remaining type for the variable `${showVarType(vs)}` is not compatible with the remaining type for the argument `${showArgType(as)}`"
def showModifier(m: Option[Modifier]): String = m match {
case None => "no modifiers"
case Some(Modifier.NonNull) => "a non-null modifier"
case Some(Modifier.List) => "a list modifier"
}
/*
* We must verify if the variable may occur here by comparing the type of the variable with the type of the arg
* If we don't do this, variables will be structurally typed
* Var should be more constrained than the arg
* a ::= [a] | a! | A
* v ::= [v] | v! | V
* a compat v ::= ok | fail
*
* a compat v -> outcome
* --------------------------
* A compat V -> ok
* a! compat v! -> a compat v
* a! compat [v] -> fail
* a! compat V -> fail
* [a] compat v! -> [a] compat v
* A compat v! -> A compat v
* [a] compat [v] -> [a] compat [v]
* [a] compat V -> fail
* A compat [v] -> fail
*/
def verifyTypeShape(argShape: List[Modifier], varShape: List[Modifier]): F[Unit] =
(argShape, varShape) match {
// A compat V
case (Nil, Nil) => F.unit
// a! compat v! -> ok
case (Modifier.NonNull :: xs, Modifier.NonNull :: ys) => verifyTypeShape(xs, ys)
// a! compat ([v] | V) -> fail
case (Modifier.NonNull :: _, (Modifier.List :: _) | Nil) =>
raise(
s"${prefix}, because the argument expected a not-null (!) modifier, but was given ${showModifier(
varShape.headOption
)}${remaining(varShape, argShape)}",
cs
)
// ([a] | A) compat v! -> ok
case (xs, Modifier.NonNull :: ys) => verifyTypeShape(xs, ys)
// [a] compat [v] -> ok
case (Modifier.List :: xs, Modifier.List :: ys) => verifyTypeShape(xs, ys)
// [a] compat V -> fail
case (Modifier.List :: _, Nil) =>
raise(
s"${prefix}, because the argumented expected a list modifier ([A]) but no more modifiers were provided${remaining(varShape, argShape)}",
cs
)
// A compat [v] -> fail
case (Nil, Modifier.List :: _) =>
raise(
s"${prefix}, because the argumented expected no more modifiers but was given a list modifier ([A])${remaining(varShape, argShape)}",
cs
)
}
val verifiedF: F[Unit] = verifyTypeShape(at.modifiers, vt.modifiers)
val verifiedTypenameF: F[Unit] =
if (vt.inner === at.inner.name) F.unit
else
raise(
s"${prefix}, typename of the variable '${vt.inner}' was not the same as the argument typename '${at.inner.name}'",
cs
)
verifiedF *> verifiedTypenameF *> parseInnerF
}
}
case (e @ Enum(name, _, _), v) =>
val fa: F[(String, List[C])] = v match {
case V.EnumValue(s, cs) => F.pure((s, cs))
case V.StringValue(s, cs) if ambigiousEnum => F.pure((s, cs))
case _ => raise(s"Enum value expected for `$name`, but got ${pValueName(v)}.", v.c)
}
fa.flatMap[A] { case (s, cs) =>
e.m.lookup(s) match {
case Some(x) => F.pure(x)
case None =>
val names = e.m.keys.toList
raise(
s"Enum value `$s` does not occur in enum type `$name`, possible enum values are ${names.map(s => s"`$s`").mkString_(", ")}.",
cs
)
}
}
case (Scalar(name, _, decoder, _), x: NonVar[List[C]]) =>
ambientField(name) {
raiseEither(decoder(x.map(_ => ())), x.c)
}
case (Input(_, fields, _), V.ObjectValue(xs, cs)) => decodeArg(fields, xs.toMap, ambigiousEnum, cs)
case (a: InArr[a, c], V.ListValue(vs, cs)) =>
vs.zipWithIndex
.parTraverse { case (v, i) =>
ambientIndex(i) {
decodeIn(a.of, v, ambigiousEnum)
}
}
.flatMap[c](a.fromSeq(_).fold(raise(_, cs), F.pure(_)))
case (_: InOpt[a], V.NullValue(_)) => F.pure(Option.empty[a])
case (opt: InOpt[a], v) => decodeIn(opt.of, v, ambigiousEnum).map(Option(_))
case (i, v) => raise(s"Expected type `${ModifierStack.fromIn(i).show(_.name)}`, but got value ${pValueName(value)}.", v.c)
}
}
override def decodeArg[A](arg: Arg[A], values: Map[String, V[AnyValue, List[C]]], ambigiousEnum: Boolean, context: List[C]): F[A] = {
val expected = arg.impl.enumerate.toList.map(_.av.name).toSet
val provided = values.keySet
val tooMuch = provided -- expected
val tooMuchF: F[Unit] =
if (tooMuch.isEmpty) F.unit
else raise(s"Too many fields provided, unknown fields are ${tooMuch.toList.map(x => s"'$x'").mkString_(", ")}.", context)
val fv = arg.impl.foldMap[F, ValidatedNec[String, A]](new (Arg.Impl ~> F) {
def apply[A](fa: Arg.Impl[A]): F[A] = fa match {
case fa: ArgDecoder[a, A] =>
ambientField(fa.av.name) {
def compileWith(x: V[AnyValue, List[C]], default: Boolean) =
decodeIn[a](fa.av.input.value, x, ambigiousEnum)
.flatMap(a => raiseEither(fa.decode(ArgParam(default, a)), x.c))
values
.get(fa.av.name)
.map(compileWith(_, false))
.orElse(fa.av.defaultValue.map(dv => compileWith(dv.as(Nil), true)))
.getOrElse {
fa.av.input.value match {
case _: gql.ast.InOpt[a] => raiseEither(fa.decode(ArgParam(true, None)), context)
case _ =>
raise(s"Missing argument for '${fa.av.name}' and no default value was found.", context)
}
}
}
}
})
tooMuchF &> fv.flatMap(v => raiseEither(v.toEither.leftMap(_.mkString_(", ")), context))
}
}
}