* Pascal-case sealed type names

* Fix typo and add test case
This commit is contained in:
Ben Bader 2019-04-22 09:53:43 -07:00 коммит произвёл GitHub
Родитель 964340a7d0
Коммит cab9fecb60
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 42 добавлений и 6 удалений

Просмотреть файл

@ -532,6 +532,7 @@ class KotlinCodeGenerator(
val nameAllocator = nameAllocators[struct]
for (field in struct.fields) {
val name = nameAllocator.get(field)
val sealedName = FieldNamingPolicy.PASCAL.apply(name)
val type = field.type.typeName
val propertySpec = PropertySpec.builder("value", type)
@ -540,7 +541,7 @@ class KotlinCodeGenerator(
val propConstructor = FunSpec.constructorBuilder()
.addParameter("value", type)
val dataProp = TypeSpec.classBuilder(name)
val dataProp = TypeSpec.classBuilder(sealedName)
.addModifiers(KModifier.DATA)
.superclass(structClassName)
.addProperty(propertySpec.build())
@ -555,7 +556,7 @@ class KotlinCodeGenerator(
if (field.defaultValue != null) {
check(defaultValueTypeName == null) { "Error in thrifty-schema; unions may not have > 1 default value" }
defaultValueTypeName = structClassName.nestedClass(name)
defaultValueTypeName = structClassName.nestedClass(sealedName)
}
}
@ -805,11 +806,12 @@ class KotlinCodeGenerator(
for (field in struct.fields) {
val name = nameAllocator.get(field)
val type = field.type.typeName
val typeName = FieldNamingPolicy.PASCAL.apply(name)
// Add a builder fun
spec.addFunction(FunSpec.builder(name)
.addParameter("value", type)
.addStatement("return apply { this.$builderVarName = ${struct.name}.$name(value) }")
.addStatement("return apply { this.$builderVarName = ${struct.name}.$typeName(value) }")
.build())
}
@ -1031,8 +1033,9 @@ class KotlinCodeGenerator(
for (field in struct.fields) {
val name = nameAllocator.get(field)
val fieldType = field.type
val typeName = FieldNamingPolicy.PASCAL.apply(name)
writer.beginControlFlow("is $name ->")
writer.beginControlFlow("is $typeName ->")
writer.addStatement("protocol.writeFieldBegin(%S, %L, %T.%L)",
field.name,
@ -1078,6 +1081,7 @@ class KotlinCodeGenerator(
for (field in struct.fields) {
val name = nameAllocator.get(field)
val fieldType = field.type
val typeName = FieldNamingPolicy.PASCAL.apply(name)
reader.addCode {
addStatement("${field.id} -> {%>")
@ -1088,7 +1092,7 @@ class KotlinCodeGenerator(
if (builderType != null) {
addStatement("builder.$name($name)")
} else {
addStatement("result = $name($name)")
addStatement("result = $typeName($name)")
}
nextControlFlow("else")

Просмотреть файл

@ -729,7 +729,7 @@ class KotlinCodeGeneratorTest {
file.single().toString() should contain("""
| @JvmStatic
| val DEFAULT: HasDefault = int(16)
| val DEFAULT: HasDefault = Int(16)
""".trimMargin())
}

Просмотреть файл

@ -81,6 +81,27 @@ abstract class FieldNamingPolicy {
}
}
/**
* The Pascal-case policy generates PascalCase names.
*/
val PASCAL: FieldNamingPolicy = object : FieldNamingPolicy() {
override fun apply(name: String): String {
val caseFormat = caseFormatOf(name)
if (caseFormat != null) {
return caseFormat.to(CaseFormat.UPPER_CAMEL, name)
}
// Unknown format. We'll bulldoze the name by uppercasing the
// first char, then just removing any subsequent non-identifier chars.
return buildString {
append(Character.toUpperCase(name[0]))
name.substring(1)
.filter { it.isJavaIdentifierPart() }
.forEach { append(it) }
}
}
}
/**
* Find case format from string.
* @param s the input String

Просмотреть файл

@ -62,4 +62,15 @@ class FieldNamingPolicyTest {
// UPPER_UNDERSCORE
policy.apply("MY_FIELD") shouldBe "myField"
}
@Test
fun pascalPolicy() {
val policy = FieldNamingPolicy.PASCAL
policy.apply("my_field") shouldBe "MyField"
policy.apply("my-field") shouldBe "MyField"
policy.apply("MyField") shouldBe "MyField"
policy.apply("MY_FIELD") shouldBe "MyField"
policy.apply("my_f234") shouldBe "MyF234"
policy.apply("myField") shouldBe "MyField"
}
}