-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e1228c
commit 0c0d408
Showing
5 changed files
with
132 additions
and
127 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
95 changes: 48 additions & 47 deletions
95
tests/compiler-hooks/default-null-values/src/main/kotlin/hooks/TestPlugin.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 |
---|---|---|
@@ -1,62 +1,63 @@ | ||
package hooks | ||
|
||
import com.apollographql.apollo3.compiler.Plugin | ||
import com.apollographql.apollo3.compiler.Transform | ||
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinOutput | ||
import com.squareup.kotlinpoet.CodeBlock | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.TypeSpec | ||
|
||
class TestPlugin: Plugin { | ||
override fun kotlinOutputTransform(): ((KotlinOutput) -> KotlinOutput) { | ||
return ::transform | ||
} | ||
|
||
private fun transform(source: KotlinOutput): KotlinOutput { | ||
return KotlinOutput( | ||
fileSpecs = source.fileSpecs.map { | ||
it.toBuilder() | ||
.apply { | ||
members.replaceAll { member -> | ||
if (member is TypeSpec) { | ||
member.addDefaultValueToNullableProperties() | ||
} else { | ||
member | ||
override fun kotlinOutputTransform(): Transform<KotlinOutput> { | ||
return object : Transform<KotlinOutput> { | ||
override fun transform(input: KotlinOutput): KotlinOutput { | ||
return KotlinOutput( | ||
fileSpecs = input.fileSpecs.map { | ||
it.toBuilder() | ||
.apply { | ||
members.replaceAll { member -> | ||
if (member is TypeSpec) { | ||
member.addDefaultValueToNullableProperties() | ||
} else { | ||
member | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.build() | ||
}, | ||
codegenMetadata = source.codegenMetadata | ||
) | ||
} | ||
.build() | ||
}, | ||
codegenMetadata = input.codegenMetadata | ||
) | ||
} | ||
|
||
private fun TypeSpec.addDefaultValueToNullableProperties(): TypeSpec { | ||
return toBuilder() | ||
.apply { | ||
// Only care about data classes | ||
if (modifiers.contains(KModifier.DATA)) { | ||
primaryConstructor( | ||
primaryConstructor!!.toBuilder() | ||
.apply { | ||
parameters.replaceAll { param -> | ||
if (param.type.isNullable) { | ||
param.toBuilder() | ||
.defaultValue(CodeBlock.of("null")) | ||
.build() | ||
} else { | ||
param | ||
private fun TypeSpec.addDefaultValueToNullableProperties(): TypeSpec { | ||
return toBuilder() | ||
.apply { | ||
// Only care about data classes | ||
if (modifiers.contains(KModifier.DATA)) { | ||
primaryConstructor( | ||
primaryConstructor!!.toBuilder() | ||
.apply { | ||
parameters.replaceAll { param -> | ||
if (param.type.isNullable) { | ||
param.toBuilder() | ||
.defaultValue(CodeBlock.of("null")) | ||
.build() | ||
} else { | ||
param | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.build() | ||
) | ||
} | ||
.build() | ||
) | ||
} | ||
|
||
// Recurse on nested types | ||
typeSpecs.replaceAll { typeSpec -> | ||
typeSpec.addDefaultValueToNullableProperties() | ||
} | ||
} | ||
.build() | ||
// Recurse on nested types | ||
typeSpecs.replaceAll { typeSpec -> | ||
typeSpec.addDefaultValueToNullableProperties() | ||
} | ||
} | ||
.build() | ||
} | ||
} | ||
} | ||
} |
101 changes: 51 additions & 50 deletions
101
tests/compiler-hooks/getters-and-setters/src/main/kotlin/hooks/TestPlugin.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 |
---|---|---|
@@ -1,62 +1,63 @@ | ||
package hooks | ||
|
||
import com.apollographql.apollo3.compiler.Plugin | ||
import com.apollographql.apollo3.compiler.Transform | ||
import com.apollographql.apollo3.compiler.codegen.java.JavaOutput | ||
import com.squareup.javapoet.JavaFile | ||
import com.squareup.javapoet.MethodSpec | ||
import javax.lang.model.element.Modifier | ||
|
||
class TestPlugin : Plugin { | ||
override fun javaOutputTransform(): ((JavaOutput) -> JavaOutput) { | ||
return ::transform | ||
} | ||
|
||
private fun transform(source: JavaOutput): JavaOutput { | ||
return JavaOutput( | ||
javaFiles = source.javaFiles.map { javaFile -> | ||
JavaFile.builder( | ||
javaFile.packageName, | ||
// Only process operations (generated at the top level) | ||
if (javaFile.packageName.endsWith(".adapter") || | ||
javaFile.packageName.endsWith(".selections") || | ||
javaFile.packageName.endsWith(".type") || | ||
javaFile.packageName.endsWith(".fragment") | ||
) { | ||
javaFile.typeSpec | ||
} else { | ||
javaFile.typeSpec!!.toBuilder() | ||
.apply { | ||
typeSpecs.replaceAll { typeSpec -> | ||
if (typeSpec.name == "Builder") { | ||
typeSpec | ||
} else { | ||
typeSpec | ||
.toBuilder() | ||
.addMethods(typeSpec.fieldSpecs | ||
// Ignore $hashCode, $toString etc. | ||
.filterNot { fieldSpec -> fieldSpec.hasModifier(Modifier.TRANSIENT) } | ||
.flatMap { fieldSpec -> | ||
listOf( | ||
MethodSpec.methodBuilder("get${fieldSpec.name.removePrefix("__").capitalize()}") | ||
.addModifiers(Modifier.PUBLIC) | ||
.returns(fieldSpec.type) | ||
.addStatement("return this.\$L", fieldSpec.name) | ||
.build(), | ||
MethodSpec.methodBuilder("set${fieldSpec.name.removePrefix("__").capitalize()}") | ||
.addModifiers(Modifier.PUBLIC) | ||
.addParameter(fieldSpec.type, fieldSpec.name) | ||
.addStatement("this.\$L = \$L", fieldSpec.name, fieldSpec.name) | ||
.build() | ||
) | ||
}) | ||
.build() | ||
override fun javaOutputTransform(): Transform<JavaOutput> { | ||
return object : Transform<JavaOutput> { | ||
override fun transform(input: JavaOutput): JavaOutput { | ||
return JavaOutput( | ||
javaFiles = input.javaFiles.map { javaFile -> | ||
JavaFile.builder( | ||
javaFile.packageName, | ||
// Only process operations (generated at the top level) | ||
if (javaFile.packageName.endsWith(".adapter") || | ||
javaFile.packageName.endsWith(".selections") || | ||
javaFile.packageName.endsWith(".type") || | ||
javaFile.packageName.endsWith(".fragment") | ||
) { | ||
javaFile.typeSpec | ||
} else { | ||
javaFile.typeSpec!!.toBuilder() | ||
.apply { | ||
typeSpecs.replaceAll { typeSpec -> | ||
if (typeSpec.name == "Builder") { | ||
typeSpec | ||
} else { | ||
typeSpec | ||
.toBuilder() | ||
.addMethods(typeSpec.fieldSpecs | ||
// Ignore $hashCode, $toString etc. | ||
.filterNot { fieldSpec -> fieldSpec.hasModifier(Modifier.TRANSIENT) } | ||
.flatMap { fieldSpec -> | ||
listOf( | ||
MethodSpec.methodBuilder("get${fieldSpec.name.removePrefix("__").capitalize()}") | ||
.addModifiers(Modifier.PUBLIC) | ||
.returns(fieldSpec.type) | ||
.addStatement("return this.\$L", fieldSpec.name) | ||
.build(), | ||
MethodSpec.methodBuilder("set${fieldSpec.name.removePrefix("__").capitalize()}") | ||
.addModifiers(Modifier.PUBLIC) | ||
.addParameter(fieldSpec.type, fieldSpec.name) | ||
.addStatement("this.\$L = \$L", fieldSpec.name, fieldSpec.name) | ||
.build() | ||
) | ||
}) | ||
.build() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.build() | ||
}).build() | ||
}, | ||
codegenMetadata = source.codegenMetadata | ||
) | ||
.build() | ||
}).build() | ||
}, | ||
codegenMetadata = input.codegenMetadata | ||
) | ||
} | ||
} | ||
} | ||
} |
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