| 
 | 1 | +//===----------------------------------------------------------------------===//  | 
 | 2 | +//  | 
 | 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project  | 
 | 4 | +//  | 
 | 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors  | 
 | 6 | +// Licensed under Apache License v2.0  | 
 | 7 | +//  | 
 | 8 | +// See LICENSE.txt for license information  | 
 | 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors  | 
 | 10 | +//  | 
 | 11 | +// SPDX-License-Identifier: Apache-2.0  | 
 | 12 | +//  | 
 | 13 | +//===----------------------------------------------------------------------===//  | 
 | 14 | +import OpenAPIKit  | 
 | 15 | + | 
 | 16 | +extension TypesFileTranslator {  | 
 | 17 | + | 
 | 18 | +    /// Returns the name used for the enum which represents a server variable defined in the OpenAPI  | 
 | 19 | +    /// document.  | 
 | 20 | +    /// - Parameter variable: The variable information.  | 
 | 21 | +    /// - Returns: A name that can be safely used for the enum.  | 
 | 22 | +    func translateVariableToEnumName(_ variable: (key: String, value: OpenAPI.Server.Variable)) -> String {  | 
 | 23 | +        return swiftSafeName(for: variable.key.localizedCapitalized)  | 
 | 24 | +    }  | 
 | 25 | + | 
 | 26 | +    /// Returns the name used for the namespace (enum) which contains a specific server's variables.  | 
 | 27 | +    /// - Parameter index: The array index of the server.  | 
 | 28 | +    /// - Returns: A name that can be safely used for the namespace.  | 
 | 29 | +    func translateServerVariablesEnumName(for index: Int) -> String {  | 
 | 30 | +        return "\(Constants.ServerURL.serverVariablesNamespacePrefix)\(index + 1)"  | 
 | 31 | +    }  | 
 | 32 | + | 
 | 33 | +    /// Returns a declaration of a variable enum case for the provided value. If the value can be  | 
 | 34 | +    /// safely represented as an identifier then the enum case is name only, otherwise the case  | 
 | 35 | +    /// will have a raw value set to the provided value to satisfy the OpenAPI document  | 
 | 36 | +    /// requirements.  | 
 | 37 | +    /// - Parameter value: The variable's enum value.  | 
 | 38 | +    /// - Returns: A enum case declaration named by the supplied value.  | 
 | 39 | +    func translateVariableCase(_ value: String) -> Declaration {  | 
 | 40 | +        let caseName = swiftSafeName(for: value)  | 
 | 41 | +        if caseName == value {  | 
 | 42 | +            return .enumCase(name: caseName, kind: .nameOnly)  | 
 | 43 | +        } else {  | 
 | 44 | +            return .enumCase(name: caseName, kind: .nameWithRawValue(.string(value)))  | 
 | 45 | +        }  | 
 | 46 | +    }  | 
 | 47 | + | 
 | 48 | +    /// Returns a declaration of a variable enum defined in the OpenAPI document. Including  | 
 | 49 | +    /// a static computed property named default which returns the default defined in the  | 
 | 50 | +    /// document.  | 
 | 51 | +    /// - Parameter variable: The variable information.  | 
 | 52 | +    /// - Returns: An enum declaration.  | 
 | 53 | +    func translateServerVariable(_ variable: (key: String, value: OpenAPI.Server.Variable)) -> Declaration {  | 
 | 54 | +        let enumName = translateVariableToEnumName(variable)  | 
 | 55 | +        var casesDecls: [Declaration]  | 
 | 56 | + | 
 | 57 | +        if let enums = variable.value.enum {  | 
 | 58 | +            casesDecls = enums.map(translateVariableCase)  | 
 | 59 | +        } else {  | 
 | 60 | +            casesDecls = [translateVariableCase(variable.value.default)]  | 
 | 61 | +        }  | 
 | 62 | +        casesDecls.append(.commentable(  | 
 | 63 | +            .doc("The default variable."),  | 
 | 64 | +            .variable(  | 
 | 65 | +                accessModifier: config.access,  | 
 | 66 | +                isStatic: true,  | 
 | 67 | +                kind: .var,  | 
 | 68 | +                left: .identifierPattern("`\(Constants.ServerURL.defaultPropertyName)`"),  | 
 | 69 | +                type: .member(enumName),  | 
 | 70 | +                getter: [  | 
 | 71 | +                    .expression(  | 
 | 72 | +                        .return(  | 
 | 73 | +                            .memberAccess(.init(  | 
 | 74 | +                                left: .identifierPattern(enumName),  | 
 | 75 | +                                right: swiftSafeName(for: variable.value.default)  | 
 | 76 | +                            ))  | 
 | 77 | +                        )  | 
 | 78 | +                    ),  | 
 | 79 | +                ]  | 
 | 80 | +            )  | 
 | 81 | +        ))  | 
 | 82 | + | 
 | 83 | +        return .commentable(  | 
 | 84 | +            .doc("""  | 
 | 85 | +            The "\(variable.key)" variable defined in the OpenAPI document.  | 
 | 86 | +
  | 
 | 87 | +            The default value is "\(variable.value.default)".  | 
 | 88 | +            """),  | 
 | 89 | +            .enum(isFrozen: true, accessModifier: config.access, name: enumName, conformances: [TypeName.string.fullyQualifiedSwiftName], members: casesDecls)  | 
 | 90 | +        )  | 
 | 91 | +    }  | 
 | 92 | + | 
 | 93 | +    /// Returns a declaration of a namespace (enum) for a specific server and will define  | 
 | 94 | +    /// one enum member for each of the server's variables in the OpenAPI Document.  | 
 | 95 | +    /// If the server does not define variables, no declaration will be generated.  | 
 | 96 | +    /// - Parameters:  | 
 | 97 | +    ///   - index: The index of the server in the list of servers defined  | 
 | 98 | +    ///   in the OpenAPI document.  | 
 | 99 | +    ///   - server: The server variables information.  | 
 | 100 | +    /// - Returns: A declaration of the server variables namespace, or `nil` if no  | 
 | 101 | +    /// variables are declared.  | 
 | 102 | +    func translateServerVariables(index: Int, server: OpenAPI.Server) -> Declaration? {  | 
 | 103 | +        if server.variables.isEmpty {  | 
 | 104 | +            return nil  | 
 | 105 | +        }  | 
 | 106 | + | 
 | 107 | +        let typeName = translateServerVariablesEnumName(for: index)  | 
 | 108 | +        let variableDecls = server.variables.map(translateServerVariable)  | 
 | 109 | +        return .commentable(  | 
 | 110 | +            .doc("The variables for Server\(index + 1) defined in the OpenAPI document."),  | 
 | 111 | +            .enum(accessModifier: config.access, name: typeName, members: variableDecls)  | 
 | 112 | +        )  | 
 | 113 | +    }  | 
 | 114 | + | 
 | 115 | +    /// Returns a declaration of a namespace (enum) called "Variables" that  | 
 | 116 | +    /// defines one namespace (enum) per server URL that defines variables  | 
 | 117 | +    /// in the OpenAPI document. If no server URL defines variables then no  | 
 | 118 | +    /// declaration is generated.  | 
 | 119 | +    /// - Parameter servers: The servers to include in the extension.  | 
 | 120 | +    /// - Returns: A declaration of an enum namespace of the server URLs type.  | 
 | 121 | +    func translateServersVariables(_ servers: [OpenAPI.Server]) -> Declaration? {  | 
 | 122 | +        let variableDecls = servers.enumerated().compactMap(translateServerVariables)  | 
 | 123 | +        if variableDecls.isEmpty {  | 
 | 124 | +            return nil  | 
 | 125 | +        }  | 
 | 126 | + | 
 | 127 | +        return .commentable(  | 
 | 128 | +            .doc("Server URL variables defined in the OpenAPI document."),  | 
 | 129 | +            .enum(accessModifier: config.access, name: Constants.ServerURL.variablesNamespace, members: variableDecls)  | 
 | 130 | +        )  | 
 | 131 | +    }  | 
 | 132 | +}  | 
0 commit comments