Skip to content

Commit

Permalink
Merge branch 'main' into hd-fix-imports-6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
czechboy0 authored Jan 6, 2025
2 parents 34311fd + 6593b1b commit 2c54d60
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
7 changes: 5 additions & 2 deletions Sources/_OpenAPIGeneratorCore/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//

/// A strategy for turning OpenAPI identifiers into Swift identifiers.
public enum NamingStrategy: String, Sendable, Codable, Equatable {
public enum NamingStrategy: String, Sendable, Codable, Equatable, CaseIterable {

/// A defensive strategy that can handle any OpenAPI identifier and produce a non-conflicting Swift identifier.
///
Expand Down Expand Up @@ -54,6 +54,9 @@ public struct Config: Sendable {
/// Defaults to `defensive`.
public var namingStrategy: NamingStrategy

/// The default naming strategy.
public static let defaultNamingStrategy: NamingStrategy = .defensive

/// A map of OpenAPI identifiers to desired Swift identifiers, used instead of the naming strategy.
public var nameOverrides: [String: String]

Expand All @@ -76,7 +79,7 @@ public struct Config: Sendable {
access: AccessModifier,
additionalImports: [String] = [],
filter: DocumentFilter? = nil,
namingStrategy: NamingStrategy = .defensive,
namingStrategy: NamingStrategy = Config.defaultNamingStrategy,
nameOverrides: [String: String] = [:],
featureFlags: FeatureFlags = []
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct ImportDescription: Equatable, Codable {
/// A description of an access modifier.
///
/// For example: `public`.
public enum AccessModifier: String, Sendable, Equatable, Codable {
public enum AccessModifier: String, Sendable, Equatable, Codable, CaseIterable {

/// A declaration accessible outside of the module.
case `public`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension _GenerateOptions {
func runGenerator(outputDirectory: URL, pluginSource: PluginSource?, isDryRun: Bool) async throws {
let config = try loadedConfig()
let sortedModes = try resolvedModes(config)
let resolvedAccessModifier = resolvedAccessModifier(config) ?? Config.defaultAccessModifier
let resolvedAccessModifier = resolvedAccessModifier(config)
let resolvedAdditionalImports = resolvedAdditionalImports(config)
let resolvedNamingStragy = resolvedNamingStrategy(config)
let resolvedNameOverrides = resolvedNameOverrides(config)
Expand Down
15 changes: 12 additions & 3 deletions Sources/swift-openapi-generator/GenerateOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ struct _GenerateOptions: ParsableArguments {
"The access modifier to use for the API of generated code. Default: \(Config.defaultAccessModifier.rawValue)"
) var accessModifier: AccessModifier?

@Option(
help:
"The strategy for converting OpenAPI names into Swift names. Default: \(Config.defaultNamingStrategy.rawValue)"
) var namingStrategy: NamingStrategy?

@Option(help: "Additional import to add to all generated files.") var additionalImport: [String] = []

@Option(help: "Pre-release feature to enable. Options: \(FeatureFlag.prettyListing).") var featureFlag:
Expand All @@ -44,6 +49,7 @@ struct _GenerateOptions: ParsableArguments {
}

extension AccessModifier: ExpressibleByArgument {}
extension NamingStrategy: ExpressibleByArgument {}

extension _GenerateOptions {

Expand All @@ -60,10 +66,10 @@ extension _GenerateOptions {
/// Returns the access modifier requested by the user.
/// - Parameter config: The configuration specified by the user.
/// - Returns: The access modifier requested by the user, or nil if the default should be used.
func resolvedAccessModifier(_ config: _UserConfig?) -> AccessModifier? {
func resolvedAccessModifier(_ config: _UserConfig?) -> AccessModifier {
if let accessModifier { return accessModifier }
if let accessModifier = config?.accessModifier { return accessModifier }
return nil
return Config.defaultAccessModifier
}

/// Returns a list of additional imports requested by the user.
Expand All @@ -78,7 +84,10 @@ extension _GenerateOptions {
/// Returns the naming strategy requested by the user.
/// - Parameter config: The configuration specified by the user.
/// - Returns: The naming strategy requestd by the user.
func resolvedNamingStrategy(_ config: _UserConfig?) -> NamingStrategy { config?.namingStrategy ?? .defensive }
func resolvedNamingStrategy(_ config: _UserConfig?) -> NamingStrategy {
if let namingStrategy { return namingStrategy }
return config?.namingStrategy ?? Config.defaultNamingStrategy
}

/// Returns the name overrides requested by the user.
/// - Parameter config: The configuration specified by the user.
Expand Down

0 comments on commit 2c54d60

Please sign in to comment.