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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion 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 Down Expand Up @@ -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
Loading