Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a CLI option for selecting the naming strategy #708

Merged
merged 4 commits into from
Jan 6, 2025
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
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
}
czechboy0 marked this conversation as resolved.
Show resolved Hide resolved

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