-
Notifications
You must be signed in to change notification settings - Fork 3
/
Modifier.scala
126 lines (109 loc) · 4.28 KB
/
Modifier.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
/*
* 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
import gql.ast._
sealed trait Modifier
object Modifier {
case object List extends Modifier
case object NonNull extends Modifier
}
final case class ModifierStack[+T](modifiers: List[Modifier], inner: T) {
def push(m: Modifier): ModifierStack[T] =
ModifierStack(m :: modifiers, inner)
// Base case is optional
def invert: InverseModifierStack[T] = {
def optional: InverseModifierStack[T] => InverseModifierStack[T] =
_.push(InverseModifier.Optional)
def go(
ms: List[Modifier],
parentOp: InverseModifierStack[T] => InverseModifierStack[T]
): InverseModifierStack[T] =
ms match {
case Nil => parentOp(InverseModifierStack(Nil, inner))
case Modifier.List :: rest => parentOp(go(rest, optional).push(InverseModifier.List))
case Modifier.NonNull :: rest => go(rest, identity)
}
go(modifiers, optional)
}
def show(showInner: T => String): String = modifiers match {
case Nil => showInner(inner)
case Modifier.List :: rest => s"[${ModifierStack(rest, inner).show(showInner)}]"
case Modifier.NonNull :: rest => s"${ModifierStack(rest, inner).show(showInner)}!"
}
}
object ModifierStack {
def fromOut[F[_]](t: Out[F, ?]): ModifierStack[OutToplevel[F, ?]] =
InverseModifierStack.fromOut(t).invert
def fromIn(t: In[?]): ModifierStack[InToplevel[?]] =
InverseModifierStack.fromIn(t).invert
import gql.parser.Type
def fromType(t: Type): ModifierStack[String] = {
import Type._
t match {
case Named(name) => ModifierStack(Nil, name)
case List(of) => fromType(of).push(Modifier.List)
case NonNull(of) => fromType(of).push(Modifier.NonNull)
}
}
}
sealed trait InverseModifier
object InverseModifier {
case object List extends InverseModifier
case object Optional extends InverseModifier
}
final case class InverseModifierStack[+T](modifiers: List[InverseModifier], inner: T) {
def push(m: InverseModifier): InverseModifierStack[T] =
InverseModifierStack(m :: modifiers, inner)
// Converts from scala view to graphql view
// Note that nested options will be flattened
// Base case is non-null in the scala world
def invert: ModifierStack[T] = {
def nonNull: ModifierStack[T] => ModifierStack[T] =
_.push(Modifier.NonNull)
def go(
ms: List[InverseModifier],
parentOp: ModifierStack[T] => ModifierStack[T]
): ModifierStack[T] = {
ms match {
case Nil => parentOp(ModifierStack(Nil, inner))
case InverseModifier.List :: rest => parentOp(go(rest, nonNull).push(Modifier.List))
case InverseModifier.Optional :: rest => go(rest, identity)
}
}
go(modifiers, nonNull)
}
def showScala(showInner: T => String): String = modifiers match {
case Nil => showInner(inner)
case InverseModifier.List :: rest =>
s"List[${InverseModifierStack(rest, inner).showScala(showInner)}]"
case InverseModifier.Optional :: rest =>
s"Option[${InverseModifierStack(rest, inner).showScala(showInner)}]"
}
}
object InverseModifierStack {
def fromOut[F[_]](t: Out[F, ?]): InverseModifierStack[OutToplevel[F, ?]] =
t match {
case t: OutToplevel[F, ?] => InverseModifierStack(Nil, t)
case OutArr(of, _, _) => fromOut(of).push(InverseModifier.List)
case o: OutOpt[F, ?, ?] => fromOut(o.of).push(InverseModifier.Optional)
}
def fromIn(t: In[?]): InverseModifierStack[InToplevel[?]] =
t match {
case t: InToplevel[?] => InverseModifierStack(Nil, t)
case InArr(of, _) => fromIn(of).push(InverseModifier.List)
case o: InOpt[?] => fromIn(o.of).push(InverseModifier.Optional)
}
}