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 additional capitalization rules #463

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -38,6 +38,9 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
testMocks: .swiftPackage(targetName: "SchemaTestMocks")
),
options: .init(
additionalCapitalizationRules: [
CapitalizationRule(term: .regex("[Ii]d"), strategy: .upper)
],
additionalInflectionRules: [
.pluralization(singularRegex: "animal", replacementRegex: "animals")
],
Expand Down Expand Up @@ -100,6 +103,16 @@ class ApolloCodegenConfigurationCodableTests: XCTestCase {
"version" : "persistedQueries"
},
"options" : {
"additionalCapitalizationRules" : [
{
"strategy" : "upper",
"term" : {
"regex" : {
"_0" : "[Ii]d"
}
}
}
],
"additionalInflectionRules" : [
{
"pluralization" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {

// MARK: - Other Types
public struct OutputOptions: Codable, Equatable {
/// Any non-default rules for capitalization you wish to include.
public let additionalCapitalizationRules: [CapitalizationRule]
/// Any non-default rules for pluralization or singularization you wish to include.
public let additionalInflectionRules: [InflectionRule]
/// How deprecated enum cases from the schema should be handled.
Expand Down Expand Up @@ -511,6 +513,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {

/// Default property values
public struct Default {
public static let additionalCapitalizationRules: [CapitalizationRule] = []
public static let additionalInflectionRules: [InflectionRule] = []
public static let deprecatedEnumCases: Composition = .include
public static let schemaDocumentation: Composition = .include
Expand All @@ -528,6 +531,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
/// Designated initializer.
///
/// - Parameters:
/// - additionalCapitalizationRules: Any non-default rules for capitalization you wish to include.
/// - additionalInflectionRules: Any non-default rules for pluralization or singularization
/// you wish to include.
/// - deprecatedEnumCases: How deprecated enum cases from the schema should be handled.
Expand All @@ -545,6 +549,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
/// - pruneGeneratedFiles: Whether unused generated files will be automatically deleted.
/// - markOperationDefinitionsAsFinal: Whether generated GraphQL operation and local cache mutation class types will be marked as `final`.
public init(
additionalCapitalizationRules: [CapitalizationRule] = Default.additionalCapitalizationRules,
additionalInflectionRules: [InflectionRule] = Default.additionalInflectionRules,
deprecatedEnumCases: Composition = Default.deprecatedEnumCases,
schemaDocumentation: Composition = Default.schemaDocumentation,
Expand All @@ -557,6 +562,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
pruneGeneratedFiles: Bool = Default.pruneGeneratedFiles,
markOperationDefinitionsAsFinal: Bool = Default.markOperationDefinitionsAsFinal
) {
self.additionalCapitalizationRules = additionalCapitalizationRules
self.additionalInflectionRules = additionalInflectionRules
self.deprecatedEnumCases = deprecatedEnumCases
self.schemaDocumentation = schemaDocumentation
Expand All @@ -573,6 +579,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
// MARK: Codable

enum CodingKeys: CodingKey, CaseIterable {
case additionalCapitalizationRules
case additionalInflectionRules
case queryStringLiteralFormat
case deprecatedEnumCases
Expand All @@ -592,6 +599,11 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
let values = try decoder.container(keyedBy: CodingKeys.self)
try throwIfContainsUnexpectedKey(container: values, type: Self.self, decoder: decoder)

additionalCapitalizationRules = try values.decodeIfPresent(
[CapitalizationRule].self,
forKey: .additionalCapitalizationRules
) ?? Default.additionalCapitalizationRules

additionalInflectionRules = try values.decodeIfPresent(
[InflectionRule].self,
forKey: .additionalInflectionRules
Expand Down Expand Up @@ -656,6 +668,7 @@ public struct ApolloCodegenConfiguration: Codable, Equatable {
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.additionalCapitalizationRules, forKey: .additionalCapitalizationRules)
try container.encode(self.additionalInflectionRules, forKey: .additionalInflectionRules)
try container.encode(self.deprecatedEnumCases, forKey: .deprecatedEnumCases)
try container.encode(self.schemaDocumentation, forKey: .schemaDocumentation)
Expand Down Expand Up @@ -1483,6 +1496,7 @@ extension ApolloCodegenConfiguration.OutputOptions {
/// Deprecated initializer.
///
/// - Parameters:
/// - additionalCapitalizationRules: Any non-default rules for capitalization you wish to include.
/// - additionalInflectionRules: Any non-default rules for pluralization or singularization
/// you wish to include.
/// - queryStringLiteralFormat: Formatting of the GraphQL query string literal that is
Expand All @@ -1502,9 +1516,10 @@ extension ApolloCodegenConfiguration.OutputOptions {
/// - pruneGeneratedFiles: Whether unused generated files will be automatically deleted.
/// - markOperationDefinitionsAsFinal: Whether generated GraphQL operation and local cache mutation class types will be marked as `final`.
@available(*, deprecated,
renamed: "init(additionalInflectionRules:queryStringLiteralFormat:deprecatedEnumCases:schemaDocumentation:selectionSetInitializers:operationDocumentFormat:cocoapodsCompatibleImportStatements:warningsOnDeprecatedUsage:conversionStrategies:pruneGeneratedFiles:markOperationDefinitionsAsFinal:)"
renamed: "init(additionalCapitalizationRules:additionalInflectionRules:queryStringLiteralFormat:deprecatedEnumCases:schemaDocumentation:selectionSetInitializers:operationDocumentFormat:cocoapodsCompatibleImportStatements:warningsOnDeprecatedUsage:conversionStrategies:pruneGeneratedFiles:markOperationDefinitionsAsFinal:)"
)
public init(
additionalCapitalizationRules: [CapitalizationRule] = Default.additionalCapitalizationRules,
additionalInflectionRules: [InflectionRule] = Default.additionalInflectionRules,
queryStringLiteralFormat: QueryStringLiteralFormat = .singleLine,
deprecatedEnumCases: ApolloCodegenConfiguration.Composition = Default.deprecatedEnumCases,
Expand All @@ -1517,6 +1532,7 @@ extension ApolloCodegenConfiguration.OutputOptions {
pruneGeneratedFiles: Bool = Default.pruneGeneratedFiles,
markOperationDefinitionsAsFinal: Bool = Default.markOperationDefinitionsAsFinal
) {
self.additionalCapitalizationRules = additionalCapitalizationRules
self.additionalInflectionRules = additionalInflectionRules
self.deprecatedEnumCases = deprecatedEnumCases
self.schemaDocumentation = schemaDocumentation
Expand All @@ -1533,6 +1549,7 @@ extension ApolloCodegenConfiguration.OutputOptions {
/// Deprecated initializer.
///
/// - Parameters:
/// - additionalCapitalizationRules: Any non-default rules for capitalization you wish to include.
/// - additionalInflectionRules: Any non-default rules for pluralization or singularization
/// you wish to include.
/// - queryStringLiteralFormat: Formatting of the GraphQL query string literal that is
Expand All @@ -1552,9 +1569,10 @@ extension ApolloCodegenConfiguration.OutputOptions {
/// - pruneGeneratedFiles: Whether unused generated files will be automatically deleted.
/// - markOperationDefinitionsAsFinal: Whether generated GraphQL operation and local cache mutation class types will be marked as `final`.
@available(*, deprecated,
renamed: "init(additionalInflectionRules:deprecatedEnumCases:schemaDocumentation:selectionSetInitializers:operationDocumentFormat:cocoapodsCompatibleImportStatements:warningsOnDeprecatedUsage:conversionStrategies:pruneGeneratedFiles:markOperationDefinitionsAsFinal:)"
renamed: "init(additionalCapitalizationRules:additionalInflectionRules:deprecatedEnumCases:schemaDocumentation:selectionSetInitializers:operationDocumentFormat:cocoapodsCompatibleImportStatements:warningsOnDeprecatedUsage:conversionStrategies:pruneGeneratedFiles:markOperationDefinitionsAsFinal:)"
)
public init(
additionalCapitalizationRules: [CapitalizationRule] = Default.additionalCapitalizationRules,
additionalInflectionRules: [InflectionRule] = Default.additionalInflectionRules,
queryStringLiteralFormat: QueryStringLiteralFormat,
deprecatedEnumCases: ApolloCodegenConfiguration.Composition = Default.deprecatedEnumCases,
Expand All @@ -1567,6 +1585,7 @@ extension ApolloCodegenConfiguration.OutputOptions {
pruneGeneratedFiles: Bool = Default.pruneGeneratedFiles,
markOperationDefinitionsAsFinal: Bool = Default.markOperationDefinitionsAsFinal
) {
self.additionalCapitalizationRules = additionalCapitalizationRules
self.additionalInflectionRules = additionalInflectionRules
self.deprecatedEnumCases = deprecatedEnumCases
self.schemaDocumentation = schemaDocumentation
Expand Down
23 changes: 23 additions & 0 deletions apollo-ios-codegen/Sources/ApolloCodegenLib/Capitalizer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

public struct CapitalizationRule: Codable, Equatable, Sendable {
public enum Term: Codable, Equatable, Sendable {
case string(String)
case regex(String)
}

public enum CaseStrategy: String, Codable, Equatable, Sendable {
case upper
case lower
case camel
case pascal
}

public let term: Term
public let strategy: CaseStrategy

public init(term: Term, strategy: CaseStrategy) {
self.term = term
self.strategy = strategy
}
}