-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from Netflix/feature/codegen-constants-102
- Loading branch information
Showing
8 changed files
with
198 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...e/src/main/kotlin/com/netflix/graphql/dgs/codegen/generators/shared/CodeGeneratorUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.netflix.graphql.dgs.codegen.generators.shared | ||
|
||
import org.apache.commons.lang.StringUtils | ||
|
||
object CodeGeneratorUtils { | ||
|
||
enum class Case { | ||
LOWERCASE, | ||
UPPERCASE | ||
} | ||
|
||
/** | ||
* Transforms the input string, that should express a camel case notation, into one that expresses a snake case notation. | ||
* | ||
* @see StringUtils.splitByCharacterTypeCamelCase | ||
* */ | ||
fun camelCasetoSnakeCase(input: String, case: Case = Case.LOWERCASE): String { | ||
val parts = StringUtils.splitByCharacterTypeCamelCase(input) | ||
return parts.joinToString(separator = "_") { | ||
when (case) { | ||
Case.LOWERCASE -> it.toLowerCase() | ||
Case.UPPERCASE -> it.toUpperCase() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...c/test/kotlin/com/netflix/graphql/dgs/codegen/generators/shared/CodeGeneratorUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* | ||
* Copyright 2020 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.netflix.graphql.dgs.codegen.generators.shared | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import java.util.stream.Stream | ||
|
||
internal class CodeGeneratorUtilsTest { | ||
|
||
@ParameterizedTest(name = "{index} => {0}, expected {1}") | ||
@MethodSource("toSnakeCaseData") | ||
fun toSnakeCase(input: String, output: String) { | ||
assertThat(CodeGeneratorUtils.camelCasetoSnakeCase(input)).isEqualTo(output) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun toSnakeCaseData(): Stream<Arguments> = Stream.of( | ||
"abc" to "abc", | ||
"ABC" to "abc", | ||
"Abc" to "abc", | ||
"1AB" to "1_ab", | ||
"1Abc" to "1_abc", | ||
"ABCefg" to "ab_cefg", | ||
"A1BCefg" to "a_1_b_cefg", | ||
"AbCeFg" to "ab_ce_fg", | ||
).map { Arguments.of(it.first, it.second) } | ||
} | ||
} |