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

fix: GraphQLEnum value camelCase conversion strategy #2745

Merged
merged 6 commits into from
Jan 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@ extension GraphQLEnumValue.Name {
}
}

/// Convert to `camelCase` from a number of different `snake_case` variants.
///
/// All inner `_` characters will be removed, each 'word' will be capitalized, returning a final
/// firstLowercased string while preserving original leading and trailing `_` characters.
private func convertToCamelCase(_ value: String) -> String {
if value.allSatisfy({ $0.isUppercase }) {
// For `UPPERCASE`. e.g) UPPER -> upper, STARWARS -> starwards
return value.lowercased()
guard value.firstIndex(of: "_") != nil else {
if value.firstIndex(where: { $0.isLowercase }) != nil {
return value.firstLowercased
} else {
return value.lowercased()
}
}
if value.contains("_") {
// For `snake_case`. e.g) snake_case -> snakeCase, UPPER_SNAKE_CASE -> upperSnakeCase
return value.split(separator: "_").enumerated().map { $0.offset == 0 ? $0.element.lowercased() : $0.element.capitalized }.joined()
}
if let firstChar = value.first, firstChar.isUppercase {
// For `UpperCamelCase`. e.g) UpperCamelCase -> upperCamelCase
return [firstChar.lowercased(), String(value.suffix(from: value.index(value.startIndex, offsetBy: 1)))].joined()
}
return value

return value.components(separatedBy: "_")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might look odd to be separating by _ and then converting empty elements back to _ but that's the trick to preserving leading/trailing _ without having to find the first/last indexes around them.

Copy link
Member Author

@calvincestari calvincestari Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

components(separatedBy:) also preserves empty _ positions vs. split which would just give us the inner 'words'.

.map({ $0.isEmpty ? "_" : $0.capitalized })
.joined()
.firstLowercased
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,24 @@ class EnumTemplateTests: XCTestCase {
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true))
}

func test_render_givenCaseConversionStrategy_camelCase_generatesSwiftEnum_convertedToCamelCase() {
func test_render_givenOption_caseConversionStrategy_camelCase_generatesSwiftEnumValues_convertedToCamelCase() {
// given
buildSubject(
name: "casedEnum",
values: [
("lower", nil, nil),
("UPPER", nil, nil),
// Mixed case
("lowercase", nil, nil),
("UPPERCASE", nil, nil),
("Capitalized", nil, nil),
("snake_case", nil, nil),
("UPPER_SNAKE_CASE", nil, nil),
("_1", nil, nil),
("_one_two_three_", nil, nil),
("camelCased", nil, nil),
("UpperCamelCase", nil, nil),
("BEFORE2023", nil, nil),

// Escaped keywords
("associatedtype", nil, nil),
("class", nil, nil),
("deinit", nil, nil),
Expand Down Expand Up @@ -170,14 +180,24 @@ class EnumTemplateTests: XCTestCase {
("throws", nil, nil),
("true", nil, nil),
("try", nil, nil),
]
],
config: .mock(
options: .init(conversionStrategies: .init(enumCases: .camelCase))
)
)

let expected = """
enum CasedEnum: String, EnumType {
case lower = "lower"
case upper = "UPPER"
case lowercase = "lowercase"
case uppercase = "UPPERCASE"
case capitalized = "Capitalized"
case snakeCase = "snake_case"
case upperSnakeCase = "UPPER_SNAKE_CASE"
case _1 = "_1"
case _oneTwoThree_ = "_one_two_three_"
case camelCased = "camelCased"
case upperCamelCase = "UpperCamelCase"
case before2023 = "BEFORE2023"
case `associatedtype` = "associatedtype"
case `class` = "class"
case `deinit` = "deinit"
Expand Down Expand Up @@ -241,14 +261,24 @@ class EnumTemplateTests: XCTestCase {
expect(actual).to(equalLineByLine(expected))
}

func test_render_givenSchemaEnum_noneConveersionStrategies_generatesSwiftEnumRespectingValueCasing() throws {
func test_render_givenOption_caseConversionStrategy_none_generatesSwiftEnumValues_respectingSchemaValueCasing() throws {
// given
buildSubject(
name: "casedEnum",
values: [
("lower", nil, nil),
("UPPER", nil, nil),
// Mixed case
("lowercase", nil, nil),
("UPPERCASE", nil, nil),
("Capitalized", nil, nil),
("snake_case", nil, nil),
("UPPER_SNAKE_CASE", nil, nil),
("_1", nil, nil),
("_one_two_three_", nil, nil),
("camelCased", nil, nil),
("UpperCamelCase", nil, nil),
("BEFORE2023", nil, nil),

// Escaped keywords
("associatedtype", nil, nil),
("class", nil, nil),
("deinit", nil, nil),
Expand Down Expand Up @@ -309,9 +339,16 @@ class EnumTemplateTests: XCTestCase {

let expected = """
enum CasedEnum: String, EnumType {
case lower
case UPPER
case lowercase
case UPPERCASE
case Capitalized
case snake_case
case UPPER_SNAKE_CASE
case _1
case _one_two_three_
case camelCased
case UpperCamelCase
case BEFORE2023
case `associatedtype`
case `class`
case `deinit`
Expand Down