/
CollisionAvoidance.scala
380 lines (357 loc) · 12.3 KB
/
CollisionAvoidance.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
/*
* 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.~>
import Type.Alias
import Type.Nullable
import Type.PrimitiveType
import Type.ValidatedAlias
import TypedNode._
import Type.ExternalType
import LineSegment._
private[internals] object CollisionAvoidance {
def apply(compilationUnit: CompilationUnit): CompilationUnit = {
val declarations = compilationUnit.declarations.map {
case Service(serviceId, name, ops, hints, version) =>
val newOps = ops.map {
case Operation(
opId,
name,
methodName,
params,
input,
errors,
output,
streamedInput,
streamedOutput,
hints
) =>
Operation(
opId,
protectKeyword(name.capitalize),
protectKeyword(methodName),
params.map(modField),
modType(input),
errors.map(modType),
modType(output),
streamedInput.map(modStreamingField),
streamedOutput.map(modStreamingField),
hints.map(modHint)
)
}
Service(
serviceId,
protectKeyword(name.capitalize),
newOps,
hints.map(modHint),
version
)
case p: Product =>
modProduct(p)
case Union(shapeId, name, alts, mixins, recursive, hints) =>
Union(
shapeId,
protectKeyword(name.capitalize),
alts.map(modAlt),
mixins.map(modType),
recursive,
hints.map(modHint)
)
case TypeAlias(shapeId, name, tpe, isUnwrapped, rec, hints) =>
val protectedName = protectKeyword(name.capitalize)
// If we had to amend the type
val unwrapped = isUnwrapped | (protectedName != name.capitalize)
TypeAlias(
shapeId,
protectKeyword(name.capitalize),
modType(tpe),
unwrapped,
rec,
hints.map(modHint)
)
case ValidatedTypeAlias(shapeId, name, tpe, recursive, hints) =>
ValidatedTypeAlias(
shapeId,
protectKeyword(name.capitalize),
modType(tpe),
recursive,
hints.map(modHint)
)
case Enumeration(shapeId, name, tag, values, hints) =>
val newValues = values.map {
case EnumValue(value, intValue, name, realName, hints) =>
EnumValue(
value = value,
intValue = intValue,
name = protectKeyword(name),
realName = realName,
hints.map(modHint)
)
}
Enumeration(
shapeId,
protectKeyword(name.capitalize),
tag,
newValues,
hints.map(modHint)
)
}
compilationUnit.copy(declarations = declarations)
}
private def modType(tpe: Type): Type = tpe match {
case Type.Collection(collectionType, member, memberHints) =>
Type.Collection(
collectionType = collectionType,
member = modType(member),
memberHints = memberHints.map(modHint(_))
)
case Type.Map(key, keyHints, value, valueHints) =>
Type.Map(
key = modType(key),
keyHints = keyHints.map(modHint(_)),
value = modType(value),
valueHints = valueHints.map(modHint(_))
)
case Type.Ref(namespace, name) =>
Type.Ref(namespace, protectKeyword(name.capitalize))
case Alias(namespace, name, tpe, isUnwrapped) =>
val protectedName = protectKeyword(name.capitalize)
val unwrapped = isUnwrapped | (protectedName != name.capitalize)
Alias(namespace, protectKeyword(name.capitalize), modType(tpe), unwrapped)
case ValidatedAlias(namespace, name, tpe) =>
ValidatedAlias(namespace, protectKeyword(name.capitalize), modType(tpe))
case PrimitiveType(prim) => PrimitiveType(prim)
case ExternalType(name, fqn, typeParams, pFqn, under, refinementHint) =>
ExternalType(
protectKeyword(name.capitalize),
fqn,
typeParams,
pFqn,
modType(under),
modNativeHint(refinementHint)
)
case Nullable(underlying) => Nullable(modType(underlying))
}
private def modField(field: Field): Field = {
Field(
protectKeyword(uncapitalise(field.name)),
field.name,
modType(field.tpe),
field.modifier,
field.hints.map(modHint)
)
}
private def modStreamingField(
streamingField: StreamingField
): StreamingField = {
StreamingField(
streamingField.name,
modType(streamingField.tpe),
streamingField.hints.map(modHint)
)
}
private def modAlt(alt: Alt): Alt = {
Alt(
protectKeyword(uncapitalise(alt.name)),
alt.name,
alt.member.update(modProduct)(modType),
alt.hints.map(modHint)
)
}
private def modRef(ref: Type.Ref): Type.Ref =
Type.Ref(ref.namespace, protectKeyword(ref.name.capitalize))
private def modNativeHint(hint: Hint.Native): Hint.Native =
Hint.Native(recursion.preprocess(modTypedNode)(hint.typedNode))
private def modDefaultHint(hint: Hint.Default): Hint.Default =
Hint.Default(recursion.preprocess(modTypedNode)(hint.typedNode))
private def modHint(hint: Hint): Hint = hint match {
case n: Hint.Native => modNativeHint(n)
case Hint.Constraint(tr, nat) =>
Hint.Constraint(modRef(tr), modNativeHint(nat))
case df: Hint.Default => modDefaultHint(df)
case other => other
}
private def modProduct(p: Product): Product = {
import p._
Product(
shapeId = p.shapeId,
name = protectKeyword(name.capitalize),
fields = fields.map(modField),
mixins = mixins.map(modType),
recursive = recursive,
hints = hints.map(modHint),
isMixin = isMixin
)
}
private def modTypedNode: TypedNode ~> TypedNode =
new (TypedNode ~> TypedNode) {
def apply[A](fa: TypedNode[A]): TypedNode[A] = fa match {
case EnumerationTN(ref, value, intValue, name) =>
EnumerationTN(modRef(ref), value, intValue, name)
case StructureTN(ref, fields) =>
StructureTN(
ref = modRef(ref),
fields = fields.map { case (k, v) =>
protectKeyword(k) -> v
}
)
case NewTypeTN(ref, target) =>
NewTypeTN(modRef(ref), target)
case ValidatedNewTypeTN(ref, target) =>
ValidatedNewTypeTN(modRef(ref), target)
case AltTN(ref, altName, alt) =>
AltTN(modRef(ref), altName, alt)
case MapTN(values) =>
MapTN(values)
case CollectionTN(collectionType, values) =>
CollectionTN(collectionType, values)
case PrimitiveTN(prim, value) =>
PrimitiveTN(prim, value)
}
}
private[internals] def protectKeyword(str: String): String =
if (reservedKeywords(str)) s"_$str" else str
private val reservedKeywords: Set[String] = Set(
"abstract",
"case",
"catch",
"class",
"def",
"do",
"else",
"extends",
"false",
"final",
"finally",
"for",
"forSome",
"if",
"implicit",
"import",
"lazy",
"match",
"new",
"null",
"object",
"override",
"package",
"private",
"protected",
"return",
"sealed",
"super",
"this",
"throw",
"trait",
"true",
"try",
"type",
"val",
"var",
"while",
"with",
"yield",
// For below, see https://github.com/disneystreaming/smithy4s/issues/1238
// Treating all methods from java.lang.Object as reserved
"clone",
"equals",
"finalize",
"getClass",
"hashCode",
"notify",
"notifyAll",
"toString",
"wait"
)
class Names() {
val Transformation = NameRef("smithy4s", "Transformation")
val PolyFunction5_ = NameRef("smithy4s.kinds", "PolyFunction5")
val Service_ = NameRef("smithy4s", "Service")
val ServiceProduct = NameRef("smithy4s", "ServiceProduct")
val ServiceProductMirror = NameRef("smithy4s", "ServiceProduct.Mirror")
val Endpoint_ = NameRef("smithy4s", "Endpoint")
val NoInput_ = NameRef("smithy4s", "NoInput")
val ShapeId_ = NameRef("smithy4s", "ShapeId")
val Schema_ = NameRef("smithy4s", "Schema")
val OperationSchema_ = NameRef("smithy4s.schema", "OperationSchema")
val FunctorAlgebra_ = NameRef("smithy4s.kinds", "FunctorAlgebra")
val BiFunctorAlgebra_ = NameRef("smithy4s.kinds", "BiFunctorAlgebra")
val StreamingSchema_ = NameRef("smithy4s.schema", "StreamingSchema")
val Enumeration_ = NameRef("smithy4s", "Enumeration")
val EnumValue_ = NameRef("smithy4s.schema", "EnumValue")
val EnumTag_ = NameRef("smithy4s.schema", "EnumTag")
val Newtype_ = NameRef("smithy4s", "Newtype")
val NewtypeValidated_ = NameRef("smithy4s", "NewtypeValidated")
val Hints_ = NameRef("smithy4s", "Hints")
val ShapeTag_ = NameRef("smithy4s", "ShapeTag")
val ErrorSchema_ = NameRef("smithy4s.schema", "ErrorSchema")
val union_ = NameRef("smithy4s.schema.Schema", "union")
val recursive_ = NameRef("smithy4s.schema.Schema", "recursive")
val enumeration_ = NameRef("smithy4s.schema.Schema", "enumeration")
val stringEnumeration_ =
NameRef("smithy4s.schema.Schema", "stringEnumeration")
val intEnumeration_ = NameRef("smithy4s.schema.Schema", "intEnumeration")
val openStringEnumeration_ =
NameRef("smithy4s.schema.Schema", "openStringEnumeration")
val openIntEnumeration_ =
NameRef("smithy4s.schema.Schema", "openIntEnumeration")
val constant_ = NameRef("smithy4s.schema.Schema", "constant")
val struct_ = NameRef("smithy4s.schema.Schema", "struct")
val bijection_ = NameRef("smithy4s.schema.Schema", "bijection")
val Transformed_ = NameDef("Transformed")
val endpoint_ = NameDef("endpoint")
val input_ = NameDef("input")
val ordinal_ = NameDef("ordinal")
val mapK5_ = NameDef("mapK5")
val fromPolyFunction_ = NameDef("fromPolyFunction")
val toPolyFunction_ = NameDef("toPolyFunction")
val transform_ = NameDef("transform")
val apply_ = NameDef("apply")
val Impl_ = NameDef("Impl")
val ErrorAware_ = NameDef("ErrorAware")
val Constant_ = NameDef("Constant")
val Default_ = NameDef("Default")
val const5_ = NameRef("smithy4s.kinds.toPolyFunction5", "const5")
val smithy4sThrowable = NameRef("smithy4s", "Smithy4sThrowable")
// We reserve these keywords as they collide with types that the
// users are bound to manipulate when using Smithy4s .
val short_ = NameRef("scala", "Short")
val int_ = NameRef("scala", "Int")
val javaInt_ = NameRef("java.lang", "Integer")
val long_ = NameRef("scala", "Long")
val double_ = NameRef("scala", "Double")
val float_ = NameRef("scala", "Float")
val bigint_ = NameRef("scala.math", "BigInteger")
val bigdecimal_ = NameRef("scala.math", "BigDecimal")
val string_ = NameRef("java.lang", "String")
val boolean_ = NameRef("scala", "Boolean")
val byte_ = NameRef("scala", "Byte")
val unit_ = NameRef("scala", "Unit")
val timestamp_ = NameRef("smithy4s", "Timestamp")
val document_ = NameRef("smithy4s", "Document")
val uuid_ = NameRef("smithy4s", "UUID")
val list = NameRef("scala", "List")
val indexedSeq = NameRef("scala.collection.immutable", "IndexedSeq")
val set = NameRef("scala.collection.immutable", "Set")
val map = NameRef("scala.collection.immutable", "Map")
val vector = NameRef("scala", "Vector")
val option = NameRef("scala", "Option")
val none = NameRef("scala", "None")
val some = NameRef("scala", "Some")
val noStackTrace = NameRef("scala.util.control", "NoStackTrace")
val throwable = NameRef("java.lang", "Throwable")
}
}