Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ extension TypesFileTranslator {
/// declare the method under.
func translateServer(index: Int, server: OpenAPI.Server) -> Declaration {
let methodName = "\(Constants.ServerURL.propertyPrefix)\(index+1)"
let parameters: [ParameterDescription] = server.variables.map { (key, value) in
.init(label: key, type: .init(TypeName.string), defaultValue: .literal(value.default))
let safeVariables = server.variables.map { (key, value) in
(originalKey: key, swiftSafeKey: swiftSafeName(for: key), value: value)
}
let variableInitializers: [Expression] = server.variables.map { (key, value) in
let parameters: [ParameterDescription] = safeVariables.map { (originalKey, swiftSafeKey, value) in
.init(label: swiftSafeKey, type: .init(TypeName.string), defaultValue: .literal(value.default))
}
let variableInitializers: [Expression] = safeVariables.map { (originalKey, swiftSafeKey, value) in
let allowedValuesArg: FunctionArgumentDescription?
if let allowedValues = value.enum {
allowedValuesArg = .init(
Expand All @@ -41,13 +44,13 @@ extension TypesFileTranslator {
return .dot("init")
.call(
[
.init(label: "name", expression: .literal(key)),
.init(label: "value", expression: .identifierPattern(key)),
.init(label: "name", expression: .literal(originalKey)),
.init(label: "value", expression: .identifierPattern(swiftSafeKey)),
] + (allowedValuesArg.flatMap { [$0] } ?? [])
)
}
let methodDecl = Declaration.commentable(
.functionComment(abstract: server.description, parameters: server.variables.map { ($0, $1.description) }),
.functionComment(abstract: server.description, parameters: safeVariables.map { ($1, $2.description) }),
.function(
accessModifier: config.access,
kind: .function(name: methodName, isStatic: true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ servers:
- url: https://example.com/api
description: Example Petstore implementation service
- url: /api
- url: https://{subdomain}.example.com:{port}/{basePath}
- url: '{protocol}://{subdomain}.example.com:{port}/{basePath}'
description: A custom domain.
variables:
protocol:
default: https
subdomain:
default: test
description: A subdomain name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,23 @@ public enum Servers {
/// A custom domain.
///
/// - Parameters:
/// - _protocol:
/// - subdomain: A subdomain name.
/// - port:
/// - basePath: The base API path.
public static func server3(
_protocol: Swift.String = "https",
subdomain: Swift.String = "test",
port: Swift.String = "443",
basePath: Swift.String = "v1"
) throws -> Foundation.URL {
try Foundation.URL(
validatingOpenAPIServerURL: "https://{subdomain}.example.com:{port}/{basePath}",
validatingOpenAPIServerURL: "{protocol}://{subdomain}.example.com:{port}/{basePath}",
variables: [
.init(
name: "protocol",
value: _protocol
),
.init(
name: "subdomain",
value: subdomain
Expand Down