Skip to content

Commit 6218f4b

Browse files
committed
[Refactor] Introduce TranslatorContext
1 parent 0eb9afa commit 6218f4b

28 files changed

+103
-112
lines changed

Sources/_OpenAPIGeneratorCore/Translator/ClientTranslator/ClientTranslator.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ struct ClientFileTranslator: FileTranslator {
3737
let imports =
3838
Constants.File.clientServerImports + config.additionalImports.map { ImportDescription(moduleName: $0) }
3939

40-
let clientMethodDecls =
41-
try OperationDescription.all(from: doc.paths, in: components, asSwiftSafeName: swiftSafeName)
40+
let clientMethodDecls = try OperationDescription.all(from: doc.paths, in: components, context: context)
4241
.map(translateClientMethod(_:))
4342

4443
let clientStructPropertyDecl: Declaration = .commentable(

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/SwiftSafeNames.swift

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414
import Foundation
1515

16-
extension FileTranslator {
17-
18-
/// Returns a copy of the string modified to be a valid Swift identifier.
19-
///
20-
/// - Parameter string: The string to convert to be safe for Swift.
21-
/// - Returns: A Swift-safe version of the input string.
22-
func swiftSafeName(for string: String) -> String { string.safeForSwiftCode }
23-
}
24-
25-
fileprivate extension String {
16+
extension String {
2617

2718
/// Returns a string sanitized to be usable as a Swift identifier.
2819
///

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateAllAnyOneOf.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extension TypesFileTranslator {
7878
originalName: key,
7979
typeUsage: propertyType,
8080
associatedDeclarations: associatedDeclarations,
81-
asSwiftSafeName: swiftSafeName
81+
context: context
8282
)
8383
var referenceStack = ReferenceStack.empty
8484
let isKeyValuePairSchema = try TypeMatcher.isKeyValuePair(
@@ -209,7 +209,7 @@ extension TypesFileTranslator {
209209
let decoder: Declaration
210210
if let discriminator {
211211
let originalName = discriminator.propertyName
212-
let swiftName = swiftSafeName(for: originalName)
212+
let swiftName = context.asSwiftSafeName(originalName)
213213
codingKeysDecls = [
214214
.enum(
215215
accessModifier: config.access,

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateObjectStruct.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ extension TypesFileTranslator {
100100
originalName: key,
101101
typeUsage: propertyType,
102102
associatedDeclarations: associatedDeclarations,
103-
asSwiftSafeName: swiftSafeName
103+
context: context
104104
)
105105
}
106106

@@ -175,7 +175,7 @@ extension TypesFileTranslator {
175175
default: .emptyInit,
176176
isSerializedInTopLevelDictionary: false,
177177
associatedDeclarations: associatedDeclarations,
178-
asSwiftSafeName: swiftSafeName
178+
context: context
179179
)
180180
return (.allowingAdditionalProperties, extraProperty)
181181
}

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/translateStringEnum.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ extension FileTranslator {
4949
// In nullable enum schemas, empty strings are parsed as Void.
5050
// This is unlikely to be fixed, so handling that case here.
5151
// https://github.com/apple/swift-openapi-generator/issues/118
52-
if isNullable && anyValue is Void { return (swiftSafeName(for: ""), .string("")) }
52+
if isNullable && anyValue is Void { return (context.asSwiftSafeName(""), .string("")) }
5353
guard let rawValue = anyValue as? String else {
5454
throw GenericError(message: "Disallowed value for a string enum '\(typeName)': \(anyValue)")
5555
}
56-
let caseName = swiftSafeName(for: rawValue)
56+
let caseName = context.asSwiftSafeName(rawValue)
5757
return (caseName, .string(rawValue))
5858
case .integer:
5959
let rawValue: Int

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/DiscriminatorExtensions.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ extension FileTranslator {
7979
/// component.
8080
/// - Parameter type: The `OneOfMappedType` for which to determine the case name.
8181
/// - Returns: A string representing the safe Swift name for the specified `OneOfMappedType`.
82-
func safeSwiftNameForOneOfMappedType(_ type: OneOfMappedType) -> String { swiftSafeName(for: type.rawNames[0]) }
82+
func safeSwiftNameForOneOfMappedType(_ type: OneOfMappedType) -> String {
83+
context.asSwiftSafeName(type.rawNames[0])
84+
}
8385
}
8486

8587
extension OpenAPI.Discriminator {

Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/StructBlueprint.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,14 @@ struct PropertyBlueprint {
146146
/// referring to them in the property.
147147
var associatedDeclarations: [Declaration] = []
148148

149-
/// A converted function from user-provided strings to strings
150-
/// safe to be used as a Swift identifier.
151-
var asSwiftSafeName: (String) -> String
149+
/// A set of configuration values that inform translation.
150+
var context: TranslatorContext
152151
}
153152

154153
extension PropertyBlueprint {
155154

156155
/// A name that is verified to be a valid Swift identifier.
157-
var swiftSafeName: String { asSwiftSafeName(originalName) }
156+
var swiftSafeName: String { context.asSwiftSafeName(originalName) }
158157

159158
/// The JSON path to the property.
160159
///

Sources/_OpenAPIGeneratorCore/Translator/FileTranslator.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,19 @@ protocol FileTranslator {
4343
/// - Throws: An error if translation encounters issues or errors during the process.
4444
func translateFile(parsedOpenAPI: ParsedOpenAPIRepresentation) throws -> StructuredSwiftRepresentation
4545
}
46+
47+
extension FileTranslator {
48+
49+
/// A new context from the file translator.
50+
var context: TranslatorContext { TranslatorContext(asSwiftSafeName: { $0.safeForSwiftCode }) }
51+
}
52+
53+
/// A set of configuration values for concrete file translators.
54+
struct TranslatorContext {
55+
56+
/// A closure that returns a copy of the string modified to be a valid Swift identifier.
57+
///
58+
/// - Parameter string: The string to convert to be safe for Swift.
59+
/// - Returns: A Swift-safe version of the input string.
60+
var asSwiftSafeName: (String) -> String
61+
}

Sources/_OpenAPIGeneratorCore/Translator/Multipart/MultipartContentInspector.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ extension FileTranslator {
120120
}
121121
var parts: [MultipartSchemaTypedContent] = try topLevelObject.properties.compactMap {
122122
(key, value) -> MultipartSchemaTypedContent? in
123-
let swiftSafeName = swiftSafeName(for: key)
123+
let swiftSafeName = context.asSwiftSafeName(key)
124124
let typeName = typeName.appending(
125125
swiftComponent: swiftSafeName + Constants.Global.inlineTypeSuffix,
126126
jsonComponent: key

Sources/_OpenAPIGeneratorCore/Translator/Multipart/translateMultipart.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension TypesFileTranslator {
6464
typeUsage: headersTypeName.asUsage,
6565
default: headersStructBlueprint.hasEmptyInit ? .emptyInit : nil,
6666
associatedDeclarations: [headersStructDecl],
67-
asSwiftSafeName: swiftSafeName
67+
context: context
6868
)
6969
} else {
7070
headersProperty = nil
@@ -90,7 +90,7 @@ extension TypesFileTranslator {
9090
originalName: Constants.Operation.Body.variableName,
9191
typeUsage: bodyTypeUsage,
9292
associatedDeclarations: associatedDeclarations,
93-
asSwiftSafeName: swiftSafeName
93+
context: context
9494
)
9595
let structDecl = translateStructBlueprint(
9696
.init(
@@ -137,7 +137,7 @@ extension TypesFileTranslator {
137137
switch part {
138138
case .documentedTyped(let documentedPart):
139139
let caseDecl: Declaration = .enumCase(
140-
name: swiftSafeName(for: documentedPart.originalName),
140+
name: context.asSwiftSafeName(documentedPart.originalName),
141141
kind: .nameWithAssociatedValues([.init(type: .init(part.wrapperTypeUsage))])
142142
)
143143
let decl = try translateMultipartPartContent(
@@ -404,7 +404,7 @@ extension FileTranslator {
404404
switch part {
405405
case .documentedTyped(let part):
406406
let originalName = part.originalName
407-
let identifier = swiftSafeName(for: originalName)
407+
let identifier = context.asSwiftSafeName(originalName)
408408
let contentType = part.partInfo.contentType
409409
let partTypeName = part.typeName
410410
let schema = part.schema
@@ -613,7 +613,7 @@ extension FileTranslator {
613613
switch part {
614614
case .documentedTyped(let part):
615615
let originalName = part.originalName
616-
let identifier = swiftSafeName(for: originalName)
616+
let identifier = context.asSwiftSafeName(originalName)
617617
let contentType = part.partInfo.contentType
618618
let headersTypeName = part.typeName.appending(
619619
swiftComponent: Constants.Operation.Output.Payload.Headers.typeName,

0 commit comments

Comments
 (0)