Skip to content

Commit

Permalink
Add Java8 Optional generation (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
sav007 authored May 17, 2017
1 parent ada0575 commit 46fb4e2
Show file tree
Hide file tree
Showing 10 changed files with 615 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ object ClassNames {
val UNMODIFIABLE_MAP_BUILDER: ClassName = ClassName.get(UnmodifiableMapBuilder::class.java)
val OPTIONAL: ClassName = ClassName.get(Optional::class.java)
val GUAVA_OPTIONAL: ClassName = ClassName.get("com.google.common.base", "Optional")
val JAVA_OPTIONAL: ClassName = ClassName.get("java.util", "Optional")
val API_UTILS: ClassName = ClassName.get(Utils::class.java)
val FRAGMENT: ClassName = ClassName.get(GraphqlFragment::class.java)

Expand Down Expand Up @@ -57,4 +58,7 @@ object ClassNames {
fun parameterizedGuavaOptional(type: TypeName): TypeName =
ParameterizedTypeName.get(GUAVA_OPTIONAL, type)

fun parameterizedJavaOptional(type: TypeName): TypeName =
ParameterizedTypeName.get(JAVA_OPTIONAL, type)

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.apollographql.android.compiler

import com.apollographql.android.compiler.ClassNames.parameterizedGuavaOptional
import com.apollographql.android.compiler.ClassNames.parameterizedJavaOptional
import com.apollographql.android.compiler.ClassNames.parameterizedOptional
import com.apollographql.android.compiler.ir.CodeGenerationContext
import com.squareup.javapoet.ClassName
Expand Down Expand Up @@ -31,6 +32,7 @@ class JavaTypeResolver(
when (context.nullableValueType) {
NullableValueType.APOLLO_OPTIONAL -> parameterizedOptional(javaType)
NullableValueType.GUAVA_OPTIONAL -> parameterizedGuavaOptional(javaType)
NullableValueType.JAVA_OPTIONAL -> parameterizedJavaOptional(javaType)
else -> javaType.annotated(Annotations.NULLABLE)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.apollographql.android.compiler
enum class NullableValueType(val value: String) {
ANNOTATED("annotated"),
APOLLO_OPTIONAL("apolloOptional"),
GUAVA_OPTIONAL("guavaOptional");
GUAVA_OPTIONAL("guavaOptional"),
JAVA_OPTIONAL("javaOptional");

companion object {
fun findByValue(value: String): NullableValueType? = NullableValueType.values().find { it.value == value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ class OperationTypeSpecBuilder(
.addParameter(ParameterSpec.builder(DATA_VAR_TYPE, "data").build())
.returns(wrapperType(context))
.addStatement(
if (context.nullableValueType != NullableValueType.ANNOTATED) {
if (context.nullableValueType == NullableValueType.JAVA_OPTIONAL) {
"return Optional.ofNullable(data)"
} else if (context.nullableValueType != NullableValueType.ANNOTATED) {
"return Optional.fromNullable(data)"
} else {
"return data"
Expand Down Expand Up @@ -126,6 +128,7 @@ class OperationTypeSpecBuilder(
private fun wrapperType(context: CodeGenerationContext) = when (context.nullableValueType) {
NullableValueType.GUAVA_OPTIONAL -> ClassNames.parameterizedGuavaOptional(DATA_VAR_TYPE)
NullableValueType.APOLLO_OPTIONAL -> ClassNames.parameterizedOptional(DATA_VAR_TYPE)
NullableValueType.JAVA_OPTIONAL -> ClassNames.parameterizedJavaOptional(DATA_VAR_TYPE)
else -> DATA_VAR_TYPE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ fun TypeSpec.withValueInitConstructor(nullableValueGenerationType: NullableValue
.filter { !it.modifiers.contains(Modifier.STATIC) }
.map {
if (it.type.isOptional() && nullableValueGenerationType != NullableValueType.ANNOTATED) {
val optionalType = if (nullableValueGenerationType == NullableValueType.GUAVA_OPTIONAL)
ClassNames.GUAVA_OPTIONAL
else
ClassNames.OPTIONAL
CodeBlock.of("this.\$L = \$T.fromNullable(\$L);\n", it.name, optionalType, it.name)
val factory = when (nullableValueGenerationType) {
NullableValueType.GUAVA_OPTIONAL -> ClassNames.GUAVA_OPTIONAL to "fromNullable"
NullableValueType.JAVA_OPTIONAL -> ClassNames.JAVA_OPTIONAL to "ofNullable"
else -> ClassNames.OPTIONAL to "fromNullable"
}
CodeBlock.of("this.\$L = \$T.\$L(\$L);\n", it.name, factory.first, factory.second, it.name)
} else {
CodeBlock.of("this.\$L = \$L;\n", it.name, it.name)
}
Expand Down Expand Up @@ -217,7 +218,7 @@ fun ClassName.mapperFieldName(): String = "${simpleName().decapitalize()}${Util.

fun TypeName.isOptional(): Boolean {
val rawType = (this as? ParameterizedTypeName)?.rawType ?: this
return rawType == ClassNames.OPTIONAL || rawType == ClassNames.GUAVA_OPTIONAL
return rawType == ClassNames.OPTIONAL || rawType == ClassNames.GUAVA_OPTIONAL || rawType == ClassNames.JAVA_OPTIONAL
}

fun TypeName.unwrapOptionalType(): TypeName {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query TestQuery {
hero {
name
friendsConnection {
totalCount
edges {
node {
name
}
}
}
}
}
Loading

0 comments on commit 46fb4e2

Please sign in to comment.