/
IR.scala
477 lines (426 loc) · 14.7 KB
/
IR.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright 2021-2024 Disney Streaming
*
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://disneystreaming.github.io/TOST-1.0.txt
*
* 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 smithy4s.codegen.internals
import cats.Applicative
import cats.Eval
import cats.Traverse
import cats.data.NonEmptyList
import cats.kernel.Eq
import cats.syntax.all._
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.shapes.ShapeId
import TypedNode.FieldTN.OptionalNoneTN
import TypedNode.FieldTN.OptionalSomeTN
import TypedNode.FieldTN.RequiredTN
import TypedNode.AltValueTN.ProductAltTN
import TypedNode.AltValueTN.TypeAltTN
import UnionMember._
import LineSegment.{NameDef, NameRef}
private[internals] case class CompilationUnit(
rawNamespace: String,
declarations: List[Decl],
rendererConfig: Renderer.Config
) {
val namespace: String =
rawNamespace
.split('.')
.map(CollisionAvoidance.protectKeyword(_))
.mkString(".")
}
private[internals] sealed trait Decl {
def shapeId: ShapeId
def name: String
def hints: List[Hint]
def nameDef: NameDef = NameDef(name)
def nameRef: NameRef = NameRef(List.empty, name, List.empty)
}
private[internals] case class Service(
shapeId: ShapeId,
name: String,
ops: List[Operation],
hints: List[Hint],
version: String
) extends Decl
private[internals] case class Operation(
shapeId: ShapeId,
name: String,
methodName: String,
params: List[Field],
input: Type,
errors: List[Type],
output: Type,
streamedInput: Option[StreamingField],
streamedOutput: Option[StreamingField],
hints: List[Hint] = Nil
)
private[internals] case class Product(
shapeId: ShapeId,
name: String,
fields: List[Field],
mixins: List[Type],
recursive: Boolean = false,
hints: List[Hint] = Nil,
isMixin: Boolean = false
) extends Decl
private[internals] case class Union(
shapeId: ShapeId,
name: String,
alts: NonEmptyList[Alt],
mixins: List[Type],
recursive: Boolean = false,
hints: List[Hint] = Nil
) extends Decl
private[internals] case class TypeAlias(
shapeId: ShapeId,
name: String,
tpe: Type,
isUnwrapped: Boolean,
recursive: Boolean = false,
hints: List[Hint] = Nil
) extends Decl
private[internals] case class ValidatedTypeAlias(
shapeId: ShapeId,
name: String,
tpe: Type,
recursive: Boolean = false,
hints: List[Hint] = Nil
) extends Decl
private[internals] case class Enumeration(
shapeId: ShapeId,
name: String,
tag: EnumTag,
values: List[EnumValue],
hints: List[Hint]
) extends Decl
private[internals] case class EnumValue(
value: String,
intValue: Int,
name: String,
realName: String,
hints: List[Hint]
)
private[internals] sealed trait EnumTag
private[internals] object EnumTag {
case object StringEnum extends EnumTag
case object IntEnum extends EnumTag
case object OpenStringEnum extends EnumTag
case object OpenIntEnum extends EnumTag
}
private[internals] case class Field(
name: String,
realName: String,
tpe: Type,
modifier: Field.Modifier,
hints: List[Hint]
)
private[internals] case class StreamingField(
name: String,
tpe: Type,
hints: List[Hint]
)
private[internals] object Field {
sealed trait Modifier {
def default: Option[Node] = this match {
case Modifier.RequiredDefaultMod(node, _) => Some(node)
case Modifier.DefaultMod(node, _) => Some(node)
case Modifier.RequiredMod => None
case Modifier.NoModifier => None
}
def none: Boolean = this match {
case Modifier.RequiredDefaultMod(_, _) => false
case Modifier.DefaultMod(_, _) => false
case Modifier.RequiredMod => false
case Modifier.NoModifier => true
}
}
object Modifier {
case object NoModifier extends Modifier
case object RequiredMod extends Modifier
case class RequiredDefaultMod(node: Node, typedNode: Option[Fix[TypedNode]])
extends Modifier
case class DefaultMod(node: Node, typedNode: Option[Fix[TypedNode]])
extends Modifier
}
def apply(
name: String,
tpe: Type,
modifier: Modifier,
hints: List[Hint] = Nil
): Field =
Field(name, name, tpe, modifier, hints)
}
private[internals] sealed trait UnionMember {
def update(f: Product => Product)(g: Type => Type): UnionMember = this match {
case TypeCase(tpe) => TypeCase(g(tpe))
case ProductCase(product) => ProductCase(f(product))
case UnitCase => UnitCase
}
}
private[internals] object UnionMember {
case class ProductCase(product: Product) extends UnionMember
case object UnitCase extends UnionMember
case class TypeCase(tpe: Type) extends UnionMember
}
private[internals] case class Alt(
name: String,
realName: String,
member: UnionMember,
hints: List[Hint]
)
private[internals] object Alt {
def apply(
name: String,
member: UnionMember,
hints: List[Hint] = Nil
): Alt = Alt(name, name, member, hints)
}
private[internals] sealed trait Type {
def dealiased: Type = this match {
case Type.Alias(_, _, tpe, _) => tpe.dealiased
case other => other
}
def isExternal: Boolean = this.dealiased match {
case _: Type.ExternalType => true
case _ => false
}
def isResolved: Boolean = {
val isUnwrapped = this match {
case Type.Alias(_, _, _, unwrapped) => unwrapped
case _ => false
}
val isDealiased = dealiased == this
isUnwrapped || isDealiased
}
}
private[internals] sealed trait Primitive {
type T
}
private[internals] object Primitive {
type Aux[TT] = Primitive { type T = TT }
case object Unit extends Primitive { type T = Unit }
case object Blob extends Primitive { type T = Array[Byte] }
case object Bool extends Primitive { type T = Boolean }
case object String extends Primitive { type T = String }
case object Timestamp extends Primitive { type T = java.time.Instant }
case object Uuid extends Primitive { type T = java.util.UUID }
case object Byte extends Primitive { type T = Byte }
case object Int extends Primitive { type T = Int }
case object Short extends Primitive { type T = Short }
case object Long extends Primitive { type T = Long }
case object Float extends Primitive { type T = Float }
case object Double extends Primitive { type T = Double }
case object BigDecimal extends Primitive { type T = scala.math.BigDecimal }
case object BigInteger extends Primitive { type T = scala.math.BigInt }
case object Document extends Primitive { type T = Node }
case object Nothing extends Primitive { type T = Nothing }
}
private[internals] object Type {
val unit = PrimitiveType(Primitive.Unit)
case class Collection(
collectionType: CollectionType,
member: Type,
memberHints: List[Hint]
) extends Type
case class Nullable(
underlying: Type
) extends Type
case class Map(
key: Type,
keyHints: List[Hint],
value: Type,
valueHints: List[Hint]
) extends Type
case class Ref(namespace: String, name: String) extends Type {
def show = namespace + "." + name
}
case class Alias(
namespace: String,
name: String,
tpe: Type,
isUnwrapped: Boolean
) extends Type
case class ValidatedAlias(namespace: String, name: String, tpe: Type)
extends Type
case class PrimitiveType(prim: Primitive) extends Type
case class ExternalType(
name: String,
fullyQualifiedName: String,
typeParameters: List[Type],
providerImport: Option[String],
underlyingTpe: Type,
refinementHint: Hint.Native
) extends Type
}
private[internals] sealed abstract class CollectionType(val tpe: NameRef)
private[internals] object CollectionType {
case object List extends CollectionType(NameRef("scala.List"))
case object Set
extends CollectionType(NameRef("scala.collection.immutable.Set"))
case object Vector extends CollectionType(NameRef("scala.Vector"))
case object IndexedSeq extends CollectionType(NameRef("scala.IndexedSeq"))
}
private[internals] sealed trait Hint
private[internals] object Hint {
case object Trait extends Hint
case object Error extends Hint
case object NoStackTrace extends Hint
case object PackedInputs extends Hint
case object NoDefault extends Hint
case object ErrorMessage extends Hint
case class Constraint(tr: Type.Ref, native: Native) extends Hint
case class Protocol(traits: List[Type.Ref]) extends Hint
case class Default(typedNode: Fix[TypedNode]) extends Hint
case class Documentation(
docLines: List[String],
memberDocLines: Map[String, List[String]]
) extends Hint
case class Deprecated(message: Option[String], since: Option[String])
extends Hint
// traits that get rendered generically
case class Native(typedNode: Fix[TypedNode]) extends Hint
case object IntEnum extends Hint
case object OpenEnum extends Hint
sealed trait SpecializedList extends Hint
object SpecializedList {
case object Vector extends SpecializedList
case object IndexedSeq extends SpecializedList
}
case object UniqueItems extends Hint
case class Typeclass(id: ShapeId, targetType: String, interpreter: String)
extends Hint
case object GenerateServiceProduct extends Hint
case object GenerateOptics extends Hint
implicit val eq: Eq[Hint] = Eq.fromUniversalEquals
}
private[internals] sealed trait Segment extends scala.Product with Serializable
private[internals] object Segment {
case class Label(value: String) extends Segment
case class GreedyLabel(value: String) extends Segment
case class Static(value: String) extends Segment
}
private[internals] sealed trait NodeF[+A]
private[internals] object NodeF {
case class ArrayF[A](tpe: Type, values: List[A]) extends NodeF[A]
case class ObjectF[A](tpe: Type, values: Vector[(String, A)]) extends NodeF[A]
case class BooleanF(tpe: Type, bool: Boolean) extends NodeF[Nothing]
case object NullF extends NodeF[Nothing]
case class NumberF(tpe: Type, number: Number) extends NodeF[Nothing]
case class StringF(tpe: Type, string: String) extends NodeF[Nothing]
}
private[internals] sealed trait TypedNode[+A]
private[internals] object TypedNode {
sealed trait FieldTN[+A] {
def map[B](f: A => B): FieldTN[B] = this match {
case RequiredTN(value) => RequiredTN(f(value))
case OptionalSomeTN(value) => OptionalSomeTN(f(value))
case OptionalNoneTN => OptionalNoneTN
}
}
object FieldTN {
implicit val fieldTNTraverse: Traverse[FieldTN] = new Traverse[FieldTN] {
def traverse[G[_]: Applicative, A, B](
fa: FieldTN[A]
)(f: A => G[B]): G[FieldTN[B]] =
fa match {
case RequiredTN(value) => f(value).map(RequiredTN(_))
case OptionalSomeTN(value) => f(value).map(OptionalSomeTN(_))
case OptionalNoneTN => Applicative[G].pure(OptionalNoneTN)
}
def foldLeft[A, B](fa: FieldTN[A], b: B)(f: (B, A) => B): B = ???
def foldRight[A, B](fa: FieldTN[A], lb: Eval[B])(
f: (A, Eval[B]) => Eval[B]
): Eval[B] = ???
}
case class RequiredTN[A](value: A) extends FieldTN[A]
case class OptionalSomeTN[A](value: A) extends FieldTN[A]
case object OptionalNoneTN extends FieldTN[Nothing]
}
sealed trait AltValueTN[+A] {
def map[B](f: A => B): AltValueTN[B] = this match {
case ProductAltTN(value) => ProductAltTN(f(value))
case TypeAltTN(value) => TypeAltTN(f(value))
}
}
object AltValueTN {
implicit val altValueTNTraverse: Traverse[AltValueTN] =
new Traverse[AltValueTN] {
def traverse[G[_]: Applicative, A, B](
fa: AltValueTN[A]
)(f: A => G[B]): G[AltValueTN[B]] =
fa match {
case ProductAltTN(value) => f(value).map(ProductAltTN(_))
case TypeAltTN(value) => f(value).map(TypeAltTN(_))
}
def foldLeft[A, B](fa: AltValueTN[A], b: B)(f: (B, A) => B): B = ???
def foldRight[A, B](fa: AltValueTN[A], lb: Eval[B])(
f: (A, Eval[B]) => Eval[B]
): Eval[B] = ???
}
case class ProductAltTN[A](value: A) extends AltValueTN[A]
case class TypeAltTN[A](value: A) extends AltValueTN[A]
}
implicit val typedNodeTraverse: Traverse[TypedNode] =
new Traverse[TypedNode] {
def traverse[G[_], A, B](
fa: TypedNode[A]
)(f: A => G[B])(implicit F: Applicative[G]): G[TypedNode[B]] = fa match {
case EnumerationTN(ref, value, intValue, name) =>
F.pure(EnumerationTN(ref, value, intValue, name))
case StructureTN(ref, fields) =>
fields.traverse(_.traverse(_.traverse(f))).map(StructureTN(ref, _))
case NewTypeTN(ref, target) =>
f(target).map(NewTypeTN(ref, _))
case ValidatedNewTypeTN(ref, target) =>
f(target).map(ValidatedNewTypeTN(ref, _))
case AltTN(ref, altName, alt) =>
alt.traverse(f).map(AltTN(ref, altName, _))
case MapTN(values) =>
values
.traverse { case (k, v) =>
(f(k), f(v)).tupled
}
.map(MapTN(_))
case CollectionTN(collectionType, values) =>
values.traverse(f).map(CollectionTN(collectionType, _))
case PrimitiveTN(prim, value) =>
F.pure(PrimitiveTN(prim, value))
}
def foldLeft[A, B](fa: TypedNode[A], b: B)(f: (B, A) => B): B = ???
def foldRight[A, B](fa: TypedNode[A], lb: Eval[B])(
f: (A, Eval[B]) => Eval[B]
): Eval[B] = ???
}
case class EnumerationTN(
ref: Type.Ref,
value: String,
intValue: Int,
name: String
) extends TypedNode[Nothing]
case class StructureTN[A](
ref: Type.Ref,
fields: List[(String, FieldTN[A])]
) extends TypedNode[A]
case class NewTypeTN[A](ref: Type.Ref, target: A) extends TypedNode[A]
case class ValidatedNewTypeTN[A](ref: Type.Ref, target: A)
extends TypedNode[A]
case class AltTN[A](ref: Type.Ref, altName: String, alt: AltValueTN[A])
extends TypedNode[A]
case class MapTN[A](values: List[(A, A)]) extends TypedNode[A]
case class CollectionTN[A](collectionType: CollectionType, values: List[A])
extends TypedNode[A]
case class PrimitiveTN[T](prim: Primitive.Aux[T], value: T)
extends TypedNode[Nothing]
}