Skip to content

Commit cb6b77f

Browse files
authored
Prefer JSON protocol over other protocols (#100)
1 parent 56d8881 commit cb6b77f

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

Sources/SotoCodeGeneratorLib/AwsService.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,23 @@ struct AwsService {
320320

321321
/// get service protocol from service
322322
static func getServiceProtocol(_ service: ServiceShape) throws -> AwsServiceProtocol {
323-
if let traits = service.traits {
324-
for trait in traits {
325-
if let protocolTrait = trait as? AwsServiceProtocol {
326-
return protocolTrait
327-
}
328-
}
323+
guard let traits = service.traits else {
324+
throw Error(reason: "No service protocol trait")
325+
}
326+
let serviceTraits = traits.compactMap { $0 as? AwsServiceProtocol }
327+
// prefer JSON protocols over other
328+
if let restJson1 = serviceTraits.first(where: { $0 is AwsProtocolsRestJson1Trait }) {
329+
return restJson1
330+
} else if let json1_1 = serviceTraits.first(where: { $0 is AwsProtocolsAwsJson1_1Trait }) {
331+
return json1_1
332+
} else if let json1_0 = serviceTraits.first(where: { $0 is AwsProtocolsAwsJson1_0Trait }) {
333+
return json1_0
329334
}
330-
throw Error(reason: "No service protocol trait")
335+
guard let trait = serviceTraits.first else {
336+
throw Error(reason: "No service protocol trait")
337+
}
338+
return trait
339+
331340
}
332341

333342
/// Get list operations service uses. Slightly more complex than just asking for all the operation shapes in the fle. Instead to do

0 commit comments

Comments
 (0)