-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAwsService.swift
More file actions
1057 lines (965 loc) · 42.3 KB
/
Copy pathAwsService.swift
File metadata and controls
1057 lines (965 loc) · 42.3 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===----------------------------------------------------------------------===//
//
// This source file is part of the Soto for AWS open source project
//
// Copyright (c) 2017-2023 the Soto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Soto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import Logging
import Mustache
import SotoSmithy
import SotoSmithyAWS
struct AwsService {
let model: Model
let serviceName: String
let serviceEndpointPrefix: String
let serviceId: ShapeId
let service: ServiceShape
let serviceProtocolTrait: AwsServiceProtocol
let endpoints: Endpoints
var operations: [ShapeId: OperationShape]
let outputHTMLComments: Bool
let logger: Logger
init(_ model: SotoSmithy.Model, endpoints: Endpoints, filter: [String]?, outputHTMLComments: Bool, logger: Logger) throws {
guard let service = model.select(type: SotoSmithy.ServiceShape.self).first else { throw Error(reason: "No service object") }
self.model = model
self.serviceId = service.key
self.service = service.value
var serviceName = try Self.getServiceName(service.value, id: service.key)
try model.patch(serviceName: serviceName)
serviceName = try Self.getServiceName(service.value, id: service.key)
self.serviceName = serviceName
self.serviceEndpointPrefix = try Self.getServiceEndpointPrefix(service: service.value, id: service.key) ?? serviceName.lowercased()
self.serviceProtocolTrait = try Self.getServiceProtocol(service.value)
self.endpoints = endpoints
self.outputHTMLComments = outputHTMLComments
self.logger = logger
let operations = Self.getOperations(service.value, model: model)
if let filter {
self.operations = operations.filter { key, _ in
filter.contains(key.shapeName.toSwiftVariableCase())
}
} else {
self.operations = operations
}
self.markInputOutputShapes(model)
// this is a breaking change (maybe for v8)
// self.removeEmptyInputs(model)
}
/// Return service name from API
static func getServiceName(_ service: SotoSmithy.ServiceShape, id: ShapeId) throws -> String {
guard let awsService = service.trait(type: AwsServiceTrait.self) else {
throw Error(reason: "\(id) does not have a \(AwsServiceTrait.staticName) trait")
}
// https://awslabs.github.io/smithy/1.0/spec/aws/aws-core.html#choosing-an-sdk-service-id
var sdkId = awsService.sdkId
// Strip out prefix names not reflected in service client symbol names.
let stripServiceNamePrefixes: [String] = ["Amazon", "AWS"]
for prefix in stripServiceNamePrefixes {
sdkId.deletePrefix(prefix)
}
// separate by non-alphanumeric character, then capitalize the first letter of each component
// and join back together
let serviceName =
sdkId
.components(separatedBy: CharacterSet.alphanumerics.inverted)
.map { $0.prefix(1).capitalized + $0.dropFirst() }
.joined()
return serviceName
}
/// return service name used in endpoint. Uses filename of Smithy file
static func getServiceEndpointPrefix(service: SotoSmithy.ServiceShape, id: ShapeId) throws -> String? {
let awsService = try Self.getTrait(from: service, trait: AwsServiceTrait.self, id: id)
return awsService.endpointPrefix ?? awsService.arnNamespace
}
/// Generate context for rendering service template
func generateServiceContext() throws -> [String: Any] {
var context: [String: Any] = [:]
guard let serviceEntry = model.select(type: SotoSmithy.ServiceShape.self).first else { throw Error(reason: "No service object") }
let serviceId = serviceEntry.key
let service = serviceEntry.value
let authSigV4 = service.trait(type: AwsAuthSigV4Trait.self)
let operationContexts = try self.generateOperationContexts()
context["name"] = self.serviceName
context["description"] = self.processDocs(from: service)
context["endpointPrefix"] = self.serviceEndpointPrefix
if let authSigV4 = authSigV4, authSigV4.name != self.serviceEndpointPrefix {
context["signingName"] = authSigV4.name
}
context["protocol"] = self.serviceProtocolTrait.output
context["apiVersion"] = service.version
if self.serviceProtocolTrait is AwsProtocolsAwsJson1_0Trait || self.serviceProtocolTrait is AwsProtocolsAwsJson1_1Trait {
context["amzTarget"] = serviceId.shapeName
}
if !self.model.select(with: TraitSelector<ErrorTrait>()).isEmpty {
context["errorTypes"] = self.serviceName + "ErrorType"
}
context["xmlNamespace"] = service.trait(type: XmlNamespaceTrait.self)?.uri
context["middlewareClass"] = self.getMiddleware(for: service)
context["endpointDiscovery"] = service.trait(type: AwsClientEndpointDiscoveryTrait.self)?.operation.shapeName.toSwiftVariableCase()
let endpoints = self.getServiceEndpoints()
.sorted { $0.key < $1.key }
.map { "\"\($0.key)\": \"\($0.value)\"" }
if endpoints.count > 0 {
context["serviceEndpoints"] = endpoints
}
let isRegionalized: Bool? = self.endpoints.partitions.reduce(nil) {
guard let regionalized = $1.services[self.serviceEndpointPrefix]?.isRegionalized else { return $0 }
return ($0 ?? false) || regionalized
}
context["regionalized"] = isRegionalized ?? true
if isRegionalized != true {
context["partitionEndpoints"] = self.getPartitionEndpoints()
.map { (partition: $0.key, endpoint: $0.value.endpoint, region: $0.value.region) }
.sorted { $0.partition < $1.partition }
.map { ".\($0.partition.toSwiftRegionEnumCase()): (endpoint: \"\($0.endpoint)\", region: .\($0.region.toSwiftRegionEnumCase()))" }
}
context["variantEndpoints"] = self.getVariantEndpoints()
.map { (variant: $0.key, endpoints: $0.value) }
.sorted { $0.variant < $1.variant }
context["operations"] = operationContexts.values.sorted { $0.funcName < $1.funcName }
let paginators = try self.generatePaginatorContext(operationContexts)
let waiters = try self.generateWaiterContexts(operationContexts)
if paginators["paginators"] != nil {
context["paginators"] = paginators
}
if waiters["waiters"] != nil {
context["waiters"] = waiters
}
context["logger"] = self.getSymbol(for: "Logger", from: "Logging", model: self.model, namespace: serviceId.namespace ?? "")
return context
}
/// Generate the context information for outputting the error enums
func generateErrorContext() throws -> [String: Any] {
let errorShapes = try model.select(from: "structure [trait|error]")
guard errorShapes.count > 0 else { return [:] }
let isQueryProtocol = self.service.hasTrait(type: AwsProtocolsAwsQueryTrait.self)
var context: [String: Any] = [:]
context["name"] = self.serviceName
context["errorName"] = self.serviceName + "ErrorType"
var errorContexts: [ErrorContext] = []
for error in errorShapes {
let queryError = isQueryProtocol ? error.value.trait(type: AwsProtocolsAwsQueryErrorTrait.self) : nil
let name: String = queryError?.code ?? error.key.shapeName
let errorContext = ErrorContext(
enum: error.key.shapeName.toSwiftVariableCase(),
string: name,
comment: self.processDocs(from: error.value)
)
errorContexts.append(errorContext)
}
errorContexts.sort { $0.enum < $1.enum }
if errorContexts.count > 0 {
context["errors"] = errorContexts
}
var errorMapContexts = errorShapes.compactMap { shape -> ErrorMapContext? in
guard shape.value.hasTrait(type: SotoErrorShapeTrait.self) else { return nil }
let queryError = isQueryProtocol ? shape.value.trait(type: AwsProtocolsAwsQueryErrorTrait.self) : nil
let errorCode: String = queryError?.code ?? shape.key.shapeName
return ErrorMapContext(code: errorCode, error: shape.key.shapeName)
}
errorMapContexts.sort { $0.code < $1.code }
if errorMapContexts.count > 0 {
context["errorMap"] = errorMapContexts
}
return context
}
/// Generate map of operation
func generateOperationContexts() throws -> [ShapeId: OperationContext] {
var operationContexts: [ShapeId: OperationContext] = [:]
let operations = self.operations
for operation in operations {
let operationContext = try generateOperationContext(
operation.value,
operationName: operation.key,
streaming: false
)
operationContexts[operation.key] = operationContext
}
return operationContexts
}
/// Generate context for rendering a single operation. Used by both `generateServiceContext` and `generatePaginatorContext`
func generateOperationContext(
_ operation: OperationShape,
operationName: ShapeId,
streaming: Bool
) throws -> OperationContext {
let httpTrait = operation.trait(type: HttpTrait.self)
let deprecatedTrait = operation.trait(type: DeprecatedTrait.self)
let endpointTrait = operation.trait(type: EndpointTrait.self)
let requireEndpointDiscovery = operation.trait(type: AwsClientDiscoveredEndpointTrait.self)?.required
var inputShapeTarget = operation.input?.target
var outputShapeTarget = operation.output?.target
// Check if target shape is a unit shape and thus Void. I could go and find the shape and check if it is a unit
// shape or I could just check its name. Spec is pretty clear there is only one unit type
// https://awslabs.github.io/smithy/1.0/spec/core/model.html#unit-type
if inputShapeTarget == "smithy.api#Unit" {
inputShapeTarget = nil
}
if outputShapeTarget == "smithy.api#Unit" {
outputShapeTarget = nil
}
// get member contexts from shape
var initParamContext: [OperationInitParamContext] = []
if let inputShapeTarget {
initParamContext = self.generateInitParameterContexts(inputShapeTarget)
}
return OperationContext(
comment: self.processDocs(from: operation),
funcName: operationName.shapeName.toSwiftVariableCase(),
inputShape: inputShapeTarget?.shapeName,
outputShape: outputShapeTarget?.shapeName,
name: operationName.shapeName,
path: httpTrait?.uri ?? "/",
httpMethod: httpTrait?.method ?? "POST",
hostPrefix: endpointTrait?.hostPrefix,
deprecated: deprecatedTrait?.message,
streaming: streaming ? "ByteBuffer" : nil,
documentationUrl: nil, // added to comment
endpointRequired: requireEndpointDiscovery.map { OperationContext.DiscoverableEndpoint(required: $0) },
initParameters: initParamContext,
taskLocals: generateTaskLocals(operation: operation)
)
}
func generateInitParameterContexts(_ inputShapeId: ShapeId) -> [OperationInitParamContext] {
guard let shape = self.model.shape(for: inputShapeId) as? StructureShape else { return [] }
guard let members = shape.members else { return [] }
let sortedMembers = members.map { $0 }.sorted { $0.key.lowercased() < $1.key.lowercased() }
var contexts: [MemberContext] = []
for member in sortedMembers {
guard let targetShape = self.model.shape(for: member.value.target) else { continue }
// member context
let memberContext = self.generateMemberContext(
member.value,
targetShape: targetShape,
name: member.key,
shapeName: inputShapeId.shapeName,
typeIsUnion: false,
isOutputShape: false
)
contexts.append(memberContext)
}
return contexts.compactMap {
if !$0.deprecated {
OperationInitParamContext(
variable: $0.variable,
parameter: $0.parameter,
type: $0.type,
default: $0.default,
comment: $0.comment
)
} else {
nil
}
}
}
func generateTaskLocals(
operation: OperationShape
) -> TaskLocalParameters? {
guard let staticParamsTrait = operation.trait(type: StaticContextParamsTrait.self) else { return nil }
let name: String
let possibleParameters: [String]
switch self.serviceEndpointPrefix {
case "s3":
name = "S3Middleware.$executionContext"
possibleParameters = ["UseS3ExpressControlEndpoint"]
default:
return nil
}
let parameters = staticParamsTrait.value
.filter { possibleParameters.contains($0.key) }
.compactMap { param in
param.value.dictionary?["value"].map { TaskLocalParameters.Parameter(key: param.key.toSwiftLabelCase(), value: $0) }
}
return !parameters.isEmpty ? .init(taskLocalName: name, taskLocalParams: parameters) : nil
}
static func getTrait<T: StaticTrait>(from shape: SotoSmithy.Shape, trait: T.Type, id: ShapeId) throws -> T {
guard let trait = shape.trait(type: T.self) else {
throw Error(reason: "\(id) does not have a \(T.staticName) trait")
}
return trait
}
/// get service protocol from service
static func getServiceProtocol(_ service: ServiceShape) throws -> AwsServiceProtocol {
if let traits = service.traits {
for trait in traits {
if let protocolTrait = trait as? AwsServiceProtocol {
return protocolTrait
}
}
}
throw Error(reason: "No service protocol trait")
}
/// Get list operations service uses. Slightly more complex than just asking for all the operation shapes in the fle. Instead to do
/// this properly you need to ask the services for all its operations and resources and then combine the operations with all the
/// operations from the resources
static func getOperations(_ service: ServiceShape, model: Model) -> [ShapeId: OperationShape] {
var operations: [ShapeId] = service.operations?.map(\.target) ?? []
func addResourceOperations(_ resource: ResourceShape) {
resource.create.map { operations.append($0.target) }
resource.put.map { operations.append($0.target) }
resource.read.map { operations.append($0.target) }
resource.update.map { operations.append($0.target) }
resource.delete.map { operations.append($0.target) }
resource.list.map { operations.append($0.target) }
resource.operations?.forEach { operations.append($0.target) }
resource.collectionOperations?.forEach { operations.append($0.target) }
resource.resources?.forEach { resourceMember in
guard let resource = model.shape(for: resourceMember.target) as? ResourceShape else { return }
addResourceOperations(resource)
}
}
if let resources = service.resources {
resources.forEach { resourceMember in
guard let resource = model.shape(for: resourceMember.target) as? ResourceShape else { return }
addResourceOperations(resource)
}
}
let operationsWithId = operations.compactMap { shapeId -> (ShapeId, OperationShape)? in
if let operationShape = model.shape(for: shapeId) as? OperationShape {
return (shapeId, operationShape)
} else {
return nil
}
}
return .init(operationsWithId) { lhs, _ in lhs }
}
func getListEntryName(member: MemberShape, list: ListShape) -> String? {
guard !member.hasTrait(type: XmlFlattenedTrait.self) else { return nil }
guard let memberName = list.member.trait(named: serviceProtocolTrait.nameTrait.staticName) as? ProtocolAliasTrait else { return "member" }
return memberName.alias
}
func getMapEntryNames(member: MemberShape, map: MapShape) -> (entry: String?, key: String, value: String) {
let flattened = member.hasTrait(type: XmlFlattenedTrait.self)
let keyTrait = map.key.trait(named: self.serviceProtocolTrait.nameTrait.staticName) as? ProtocolAliasTrait
let valueTrait = map.value.trait(named: self.serviceProtocolTrait.nameTrait.staticName) as? ProtocolAliasTrait
return (entry: flattened ? nil : "entry", key: keyTrait?.alias ?? "key", value: valueTrait?.alias ?? "value")
}
/// process documenation string
func processDocs(from shape: Shape) -> [Substring] {
var docs: [Substring]
let documentation = shape.trait(type: DocumentationTrait.self)?.value
if self.outputHTMLComments {
docs = documentation?.split(separator: "\n") ?? []
} else {
docs =
documentation?
.tagStriped()
.replacingOccurrences(of: "\n +", with: " ", options: .regularExpression, range: nil)
.split(separator: "\n")
.compactMap { $0.isEmpty ? nil : $0 }
.map { $0.dropLast(while: { $0.isWhitespace }) } ?? []
}
if let externalDocumentation = shape.trait(type: ExternalDocumentationTrait.self)?.value {
for (key, value) in externalDocumentation {
docs.append("\(key): \(value)")
}
}
return docs
}
/// process documenation string
func processMemberDocs(from shape: MemberShape) -> [Substring] {
guard var documentation = shape.trait(type: DocumentationTrait.self)?.value else { return [] }
if let recommendation = shape.trait(type: RecommendedTrait.self)?.reason {
documentation += "\n\(recommendation)"
}
return
documentation
.tagStriped()
.replacingOccurrences(of: "\n +", with: " ", options: .regularExpression, range: nil)
.split(separator: "\n")
.compactMap { $0.isEmpty ? nil : $0 }
.map { $0.dropLast(while: { $0.isWhitespace }) }
}
/// process documentation string
func processDocs(_ documentation: String?) -> [String.SubSequence] {
documentation?
.tagStriped()
.replacingOccurrences(of: "\n +", with: " ", options: .regularExpression, range: nil)
.split(separator: "\n")
.compactMap { $0.isEmpty ? nil : $0 } ?? []
}
/// return middleware name given a service name
func getMiddleware(for service: ServiceShape) -> String? {
switch self.serviceName {
case "APIGateway":
return "AWSEditHeadersMiddleware(.add(name: \"accept\", value: \"application/json\"))"
case "Glacier":
return """
AWSMiddlewareStack {
AWSEditHeadersMiddleware(.add(name: \"x-amz-glacier-version\", value: \"\(service.version ?? "2012-06-01")\"))
TreeHashMiddleware(header: \"x-amz-sha256-tree-hash\")
}
"""
case "S3":
return "S3Middleware()"
default:
return nil
}
}
/// return symbol name with framework added if required to avoid name clashes
func getSymbol(for symbol: String, from framework: String, model: SotoSmithy.Model, namespace: String) -> String {
if model.shape(for: ShapeId(rawValue: "\(namespace)#\(symbol)")) != nil {
return "\(framework).\(symbol)"
}
return symbol
}
func encodingName(_ name: String) -> String {
"_\(name)Encoding"
}
/// return payload member of structure
func getPayloadMember(from shape: CollectionShape) -> (key: String, value: MemberShape)? {
guard let members = shape.members else { return nil }
for member in members {
if member.value.trait(type: HttpPayloadTrait.self) != nil {
return member
}
}
return nil
}
/// return if shape has a recursive reference (checks 4 layers of references)
func doesShapeHaveRecursiveOwnReference(_ shape: CollectionShape, shapeId: ShapeId) -> Bool {
func hasRecursiveOwnReference(_ shape: CollectionShape, count: Int) -> Bool {
if count > 4 { return false }
guard let members = shape.members else { return false }
return members.values.contains(where: { member in
// does shape have a member of same type as itself
if member.target == shapeId {
return true
} else {
guard let shape = model.shape(for: member.target) else { return false }
switch shape {
case let collection as CollectionShape:
return hasRecursiveOwnReference(collection, count: count + 1)
default:
break
}
return false
}
})
}
return hasRecursiveOwnReference(shape, count: 0)
}
/// mark up model with Soto traits for input and output shapes
func markInputOutputShapes(_ model: Model) {
func addTrait<T: StaticTrait>(to shapeId: ShapeId, trait: T) {
guard let shape = model.shape(for: shapeId) else { return }
// don't mark shapes that are marked as stubs
guard shape.trait(type: SotoStubTrait.self) == nil else { return }
// if shape already has trait then don't apply it again
guard shape.trait(type: T.self) == nil else { return }
shape.add(trait: trait)
if let structure = shape as? CollectionShape {
guard let members = structure.members else { return }
for member in members {
addTrait(to: member.value.target, trait: trait)
}
} else if let list = shape as? ListShape {
addTrait(to: list.member.target, trait: trait)
} else if let set = shape as? SetShape {
addTrait(to: set.member.target, trait: trait)
} else if let map = shape as? MapShape {
addTrait(to: map.key.target, trait: trait)
addTrait(to: map.value.target, trait: trait)
}
}
for operation in self.operations {
if let input = operation.value.input {
if let shape = model.shape(for: input.target) {
shape.add(trait: SotoRequestShapeTrait(operationShape: operation.value))
}
addTrait(to: input.target, trait: SotoInputShapeTrait())
}
if let output = operation.value.output {
if let shape = model.shape(for: output.target) {
shape.add(trait: SotoResponseShapeTrait(operationShape: operation.value))
}
addTrait(to: output.target, trait: SotoOutputShapeTrait())
}
if let errors = operation.value.errors {
for error in errors {
if let shape = model.shape(for: error.target) {
// Only add error trait to errors with more properties than just a message
switch shape {
case let shape as StructureShape:
guard let members = shape.members, members.count > 0 else { continue }
if members.count == 1 && members.keys.first?.lowercased() == "message" { continue }
case is UnionShape:
break
default:
continue
}
shape.add(trait: SotoErrorShapeTrait())
}
addTrait(to: error.target, trait: SotoOutputShapeTrait())
}
}
}
}
func removeEmptyInputs(_ model: Model) {
for operation in self.operations {
if let input = operation.value.input {
if let shape = model.shape(for: input.target) {
if let structureShape = shape as? StructureShape {
if let members = structureShape.members {
if members.count == 0 {
operation.value.input = nil
}
} else {
operation.value.input = nil
}
}
}
}
}
}
/// The JSON decoder requires an array to exist, even if it is empty so we have to make
/// all arrays in output shapes optional
func removeRequiredTraitFromOutputCollections(_ model: Model) {
guard
self.serviceProtocolTrait is AwsProtocolsAwsJson1_0Trait || self.serviceProtocolTrait is AwsProtocolsAwsJson1_1Trait
|| self.serviceProtocolTrait is AwsProtocolsRestJson1Trait
else { return }
for shape in model.shapes {
guard shape.value.hasTrait(type: SotoOutputShapeTrait.self) else { continue }
guard let structure = shape.value as? StructureShape else { continue }
guard let members = structure.members else { continue }
for member in members.values {
let shape = model.shape(for: member.target)
if shape is ListShape || shape is SetShape || shape is MapShape {
member.remove(trait: RequiredTrait.self)
}
}
}
}
/// convert paginator token to KeyPath
func toKeyPath(token: String, structure: StructureShape) -> String {
var split = token.split(separator: ".")
for i in 0..<split.count {
// if string contains [-1] replace with '.last'.
if let negativeIndexRange = split[i].range(of: "[-1]") {
split[i].removeSubrange(negativeIndexRange)
var replacement = "last"
// if a member is mentioned after the '[-1]' then you need to add a ? to the keyPath
if split.count > i + 1 {
replacement += "?"
}
split.insert(Substring(replacement), at: i + 1)
}
}
// if output token is member of an optional struct add ? suffix
if let member = structure.members?[String(split[0])] {
let required =
member.hasTrait(type: RequiredTrait.self)
|| (member.hasTrait(type: HttpPayloadTrait.self) && structure.hasTrait(type: SotoOutputShapeTrait.self))
if !required, split.count > 1 {
split[0] += "?"
}
}
return split.map { String($0).toSwiftVariableCase() }.joined(separator: ".")
}
/// Service endpoints from API and Endpoints structure
func getServiceEndpoints() -> [(key: String, value: String)] {
// create dictionary of endpoint name to Endpoint and partition from across all partitions
struct EndpointInfo {
let endpoint: Endpoints.Endpoint
let partition: String
}
let serviceEndpoints = self.endpoints.partitions.flatMap { partition -> [(key: String, value: EndpointInfo)] in
guard let endpoints = partition.services[self.serviceEndpointPrefix]?.endpoints else { return [] }
let endpointInfo = endpoints.compactMap { endpoint -> (key: String, value: EndpointInfo)? in
if endpoint.value.deprecated == true {
return nil
}
return (key: endpoint.key, value: EndpointInfo(endpoint: endpoint.value, partition: partition.partition))
}
return endpointInfo
}
let partitionEndpoints = self.getPartitionEndpoints()
return serviceEndpoints.compactMap {
// if endpoint has a hostname return that
if let hostname = $0.value.endpoint.hostname {
return (key: $0.key, value: hostname)
} else if partitionEndpoints[$0.value.partition] != nil {
// if there is a partition endpoint, then default this regions endpoint to ensure partition endpoint doesn't override it.
// Only an issue for S3 at the moment.
return (key: $0.key, value: "\(self.serviceEndpointPrefix).\($0.key).amazonaws.com")
}
return nil
}
}
// return dictionary of partition endpoints keyed by endpoint name
func getPartitionEndpoints() -> [String: (endpoint: String, region: String)] {
var partitionEndpoints: [String: (endpoint: String, region: String)] = [:]
self.endpoints.partitions.forEach {
guard let service = $0.services[self.serviceEndpointPrefix] else { return }
guard let partitionEndpoint = service.partitionEndpoint else { return }
guard let endpoint = service.endpoints[partitionEndpoint] else {
self.logger.error(
"Partition endpoint \(partitionEndpoint) for service \(self.serviceEndpointPrefix) in \($0.partitionName) does not exist"
)
return
}
guard let region = endpoint.credentialScope?.region else {
// services with SigV4 authentication require an endpoint
if self.service.trait(type: AwsAuthSigV4Trait.self) != nil {
self.logger.error(
"Partition endpoint \(partitionEndpoint) for service \(self.serviceEndpointPrefix) in \($0.partitionName) has no credential scope region"
)
}
return
}
partitionEndpoints[$0.partition] = (endpoint: partitionEndpoint, region: region)
}
return partitionEndpoints
}
func getVariantEndpoints() -> [String: VariantContext] {
var variantEndpoints: [String: VariantContext] = [:]
self.endpoints.partitions.forEach { partition in
guard let service = partition.services[self.serviceEndpointPrefix] else { return }
return service.endpoints.forEach { endpoint in
guard endpoint.value.deprecated != true else { return }
var endpointValue = endpoint.value
// apply service defaults to endpoint info
if let defaults = service.defaults {
endpointValue = endpointValue.applyingDefaults(defaults)
}
endpointValue = endpointValue.applyingPartitionDefaults(partition.defaults)
guard let variants = endpointValue.variants else { return }
for variant in variants {
let variantString = variant.tags
.map { ".\($0)" }
.sorted()
.joined(separator: ", ")
// get dnsSuffix for this variant
guard
let dnsSuffix = getDefaultValue(
partition: partition,
service: service,
getValue: { defaults in
defaults.variants?.first(where: { $0.tags == variant.tags })?.dnsSuffix
}
)
else {
continue
}
if variantEndpoints[variantString] == nil {
variantEndpoints[variantString] = .init()
}
if let hostname = variant.hostname {
// get hostname and replace any variables (wrapped in {}) in hostname
let finalHostname =
hostname
.replacingOccurrences(of: "{region}", with: endpoint.key)
.replacingOccurrences(of: "{dnsSuffix}", with: dnsSuffix)
.replacingOccurrences(of: "{service}", with: self.serviceEndpointPrefix)
// add variant endpoint
variantEndpoints[variantString]!.endpoints.append((region: endpoint.key, hostname: finalHostname))
}
}
}
}
// return variants with endpoints sorted by region name
return variantEndpoints.mapValues {
.init(defaultEndpoint: $0.defaultEndpoint, endpoints: $0.endpoints.sorted { $0.region < $1.region })
}
}
func getDefaultValue<Value>(
partition: Endpoints.Partition,
service: Endpoints.Service,
getValue: (Endpoints.Defaults) -> Value?
) -> Value? {
if let serviceDefaults = service.defaults, let value = getValue(serviceDefaults) {
return value
} else if let value = getValue(partition.defaults) {
return value
}
return nil
}
/// get protocol needed for shape
func getShapeProtocol(_ shape: Shape, hasPayload: Bool) -> String? {
let usedInInput = shape.hasTrait(type: SotoInputShapeTrait.self)
let usedInOutput = shape.hasTrait(type: SotoOutputShapeTrait.self)
let isError = shape.hasTrait(type: SotoErrorShapeTrait.self)
var shapeProtocol: String
if usedInInput {
shapeProtocol = "AWSEncodableShape"
if usedInOutput {
shapeProtocol += " & AWSDecodableShape"
}
} else if isError {
shapeProtocol = "AWSErrorShape"
} else if usedInOutput {
shapeProtocol = "AWSDecodableShape"
} else {
return nil
}
return shapeProtocol
}
func isMemberInBody(_ member: MemberShape, isOutputShape: Bool) -> Bool {
!(member.hasTrait(type: HttpHeaderTrait.self) || member.hasTrait(type: HttpPrefixHeadersTrait.self)
|| (member.hasTrait(type: HttpQueryTrait.self) && !isOutputShape) || member.hasTrait(type: HttpQueryParamsTrait.self)
|| member.hasTrait(type: HttpLabelTrait.self) || member.hasTrait(type: HttpResponseCodeTrait.self))
}
}
protocol EncodingPropertiesContext {}
extension AwsService {
struct Error: Swift.Error {
let reason: String
}
struct TaskLocalParameters {
struct Parameter {
let key: String
let value: Any
}
let taskLocalName: String
let taskLocalParams: [Parameter]
}
struct OperationContext {
struct DiscoverableEndpoint {
let required: Bool
}
let comment: [String.SubSequence]
let funcName: String
let inputShape: String?
let outputShape: String?
let name: String
let path: String
let httpMethod: String
let hostPrefix: String?
let deprecated: String?
let streaming: String?
let documentationUrl: String?
let endpointRequired: DiscoverableEndpoint?
var initParameters: [OperationInitParamContext]
let taskLocals: TaskLocalParameters?
}
struct OperationInitParamContext {
let variable: String
let parameter: String
let type: String
let `default`: String?
let comment: [String.SubSequence]
}
struct ShapesContext {
enum ShapeType {
case `enum`(EnumContext)
case `struct`(StructureContext)
case enumWithValues(StructureContext)
}
let name: String
let shapes: [ShapeType]
let errors: [String: Any]?
var scope: String
let extraCode: String?
}
struct PaginatorContext {
let operation: OperationContext
let inputKey: String?
let outputKey: String
let moreResultsKey: String?
}
struct PaginatorShapeContext {
let inputShape: String
let initParams: [String]
let paginatorProtocol: String
let tokenType: String
}
struct ErrorContext {
let `enum`: String
let string: String
let comment: [String.SubSequence]
}
struct ErrorMapContext {
let code: String
let error: String
}
struct EnumContext {
struct IntValues {
let values: [IntEnumMemberContext]
}
struct StringValues {
let values: [EnumMemberContext]
}
let name: String
let documentation: [String.SubSequence]
let stringValues: StringValues?
let intValues: IntValues?
let isExtensible: Bool
}
struct EnumMemberContext {
let `case`: String
let documentation: [String.SubSequence]
let rawValue: String
}
struct IntEnumMemberContext {
let `case`: String
let documentation: [String.SubSequence]
let rawValue: Int
}
struct ArrayEncodingPropertiesContext: EncodingPropertiesContext {
let name: String
let member: String
}
struct DictionaryEncodingPropertiesContext: EncodingPropertiesContext {
let name: String
let entry: String?
let key: String
let value: String
}
struct MemberCodableContext {
init(
inHeader: String? = nil,
inQuery: String? = nil,
inURI: String? = nil,
inHostPrefix: String? = nil,
areQueryParams: Bool = false,
isUnit: Bool = false,
isPayload: Bool = false,
isCodable: Bool = false,
isStatusCode: Bool = false,
codableType: String
) {
self.inHeader = inHeader
self.inQuery = inQuery
self.inURI = inURI
self.inHostPrefix = inHostPrefix
self.areQueryParams = areQueryParams
self.isUnit = isUnit
self.isPayload = isPayload
self.isCodable = isCodable
self.isStatusCode = isStatusCode
self.codableType = codableType
}
var inHeader: String?
var inQuery: String?
var inURI: String?
var inHostPrefix: String?
var areQueryParams: Bool
var isUnit: Bool
var isPayload: Bool
var isCodable: Bool
var isStatusCode: Bool
var codableType: String
}
struct MemberContext {
let variable: String
let parameter: String
let required: Bool
let `default`: String?
let propertyWrapper: String?
let type: String
let comment: [String.SubSequence]
let deprecated: Bool
var duplicate: Bool
var memberCoding: MemberCodableContext
}
struct InitParamContext {
let parameter: String
let type: String
let `default`: String?
}
struct MemberEncodingContext {
let name: String
let location: String?
}
class ValidationContext: MustacheTransformable {
let name: String
let shape: Bool
let required: Bool
let reqs: [String: Any]
let member: ValidationContext?
let keyValidation: ValidationContext?
let valueValidation: ValidationContext?
init(
name: String,
shape: Bool = false,
required: Bool = true,
reqs: [String: Any] = [:],
member: ValidationContext? = nil,
keyValidation: ValidationContext? = nil,
valueValidation: ValidationContext? = nil
) {
self.name = name
self.shape = shape
self.required = required
self.reqs = reqs
self.member = member
self.keyValidation = keyValidation
self.valueValidation = valueValidation
}
func transform(_ name: String) -> Any? {
switch name {
case "withDictionaryContexts":
if self.keyValidation != nil || self.valueValidation != nil {
return self
}
default:
break
}
return nil
}
}
struct CodingKeysContext {
let variable: String
let rawValue: String
var duplicate: Bool
}