|
14 | 14 | import OpenAPIKit |
15 | 15 |
|
16 | 16 | extension TypesFileTranslator { |
17 | | - |
18 | 17 | /// Returns a declaration of a server URL static method defined in |
19 | | - /// the OpenAPI document. |
| 18 | + /// the OpenAPI document. Also appends server variables enums |
| 19 | + /// into the variables namespace parameter, if any enums were |
| 20 | + /// generated. |
20 | 21 | /// - Parameters: |
21 | 22 | /// - index: The index of the server in the list of servers defined |
22 | 23 | /// in the OpenAPI document. |
23 | 24 | /// - server: The server URL information. |
| 25 | + /// - variablesNamespace: The enum namespace which is to contain |
| 26 | + /// any variable enum definitions that may be required. |
24 | 27 | /// - Returns: A static method declaration, and a name for the variable to |
25 | 28 | /// declare the method under. |
26 | | - func translateServer(index: Int, server: OpenAPI.Server) -> Declaration { |
27 | | - let methodName = "\(Constants.ServerURL.propertyPrefix)\(index+1)" |
28 | | - let safeVariables = server.variables.map { (key, value) in |
29 | | - (originalKey: key, swiftSafeKey: swiftSafeName(for: key), value: value) |
30 | | - } |
31 | | - let parameters: [ParameterDescription] = safeVariables.map { (originalKey, swiftSafeKey, value) in |
32 | | - let memberPath: [String] = [ |
33 | | - Constants.ServerURL.variablesNamespace, |
34 | | - translateServerVariablesEnumName(for: index), |
35 | | - translateVariableToEnumName((originalKey, value)) |
36 | | - ] |
37 | | - return .init( |
38 | | - label: swiftSafeName(for: originalKey), |
39 | | - type: .member(memberPath), |
40 | | - defaultValue: .identifierType(.member(memberPath + CollectionOfOne(Constants.ServerURL.defaultPropertyName))) |
41 | | - ) |
42 | | - } |
43 | | - let variableInitializers: [Expression] = safeVariables.map { (originalKey, swiftSafeKey, value) in |
44 | | - return .dot("init") |
45 | | - .call( |
46 | | - [ |
47 | | - .init(label: "name", expression: .literal(originalKey)), |
48 | | - .init( |
49 | | - label: "value", |
50 | | - expression: .memberAccess(.init( |
51 | | - left: .identifierPattern(swiftSafeKey), |
52 | | - right: "rawValue" |
53 | | - )) |
54 | | - ), |
55 | | - ] |
56 | | - ) |
| 29 | + func translateServer(index: Int, server: OpenAPI.Server, variablesNamespace: inout EnumDescription) -> Declaration { |
| 30 | + let serverVariables = translateServerVariableNamespace(index: index, server: server, variablesNamespaceName: variablesNamespace.name) |
| 31 | + |
| 32 | + if let serverVariablesNamespace = serverVariables.decl { |
| 33 | + variablesNamespace.members.append(serverVariablesNamespace) |
57 | 34 | } |
| 35 | + |
| 36 | + let parameters = serverVariables.variables.map(\.parameter) |
| 37 | + let variableInitializers = serverVariables.variables.map(\.initializer) |
| 38 | + let functionComments = serverVariables.variables.map(\.functionComment) |
| 39 | + |
| 40 | + let methodName = "\(Constants.ServerURL.propertyPrefix)\(index + 1)" |
| 41 | + |
58 | 42 | let methodDecl = Declaration.commentable( |
59 | | - .functionComment(abstract: server.description, parameters: safeVariables.map { ($1, $2.description) }), |
| 43 | + .functionComment(abstract: server.description, parameters: functionComments), |
60 | 44 | .function( |
61 | 45 | accessModifier: config.access, |
62 | 46 | kind: .function(name: methodName, isStatic: true), |
@@ -87,10 +71,24 @@ extension TypesFileTranslator { |
87 | 71 | /// - Parameter servers: The servers to include in the extension. |
88 | 72 | /// - Returns: A declaration of an enum namespace of the server URLs type. |
89 | 73 | func translateServers(_ servers: [OpenAPI.Server]) -> Declaration { |
90 | | - var serverDecls = servers.enumerated().map(translateServer) |
| 74 | + var variablesNamespace = EnumDescription( |
| 75 | + accessModifier: config.access, |
| 76 | + name: Constants.ServerURL.variablesNamespace |
| 77 | + ) |
| 78 | + |
| 79 | + var serverDecls: [Declaration] = [] |
| 80 | + |
| 81 | + |
| 82 | + for (index, decl) in servers.enumerated() { |
| 83 | + let serverDecl = translateServer(index: index, server: decl, variablesNamespace: &variablesNamespace) |
| 84 | + serverDecls.append(serverDecl) |
| 85 | + } |
91 | 86 |
|
92 | | - if let variablesDecl = translateServersVariables(servers) { |
93 | | - serverDecls.insert(variablesDecl, at: 0) |
| 87 | + if !variablesNamespace.members.isEmpty { |
| 88 | + serverDecls.insert(.commentable( |
| 89 | + .doc("Server URL variables defined in the OpenAPI document."), |
| 90 | + .enum(variablesNamespace) |
| 91 | + ), at: 0) |
94 | 92 | } |
95 | 93 |
|
96 | 94 | return .commentable( |
|
0 commit comments