Skip to content
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 @@ -6,6 +6,7 @@ import com.apollographql.apollo.api.ResponseWriter
import com.apollographql.apollo.compiler.ir.CodeGenerationContext
import com.apollographql.apollo.compiler.ir.Condition
import com.apollographql.apollo.compiler.ir.Field
import com.apollographql.apollo.compiler.ir.TypeDeclaration
import com.squareup.javapoet.*
import java.util.*
import javax.lang.model.element.Modifier
Expand Down Expand Up @@ -94,7 +95,8 @@ class ResponseFieldSpec(
val readValueCode = CodeBlock.builder()
.addStatement("final \$T \$L", normalizedFieldSpec.type, fieldSpec.name)
.beginControlFlow("if (\$LStr != null)", fieldSpec.name)
.addStatement("\$L = \$T.valueOf(\$LStr)", fieldSpec.name, normalizedFieldSpec.type, fieldSpec.name)
.addStatement("\$L = \$T.\$L(\$LStr)", fieldSpec.name, normalizedFieldSpec.type,
TypeDeclaration.ENUM_SAFE_VALUE_OF, fieldSpec.name)
.nextControlFlow("else")
.addStatement("\$L = null", fieldSpec.name)
.endControlFlow()
Expand Down Expand Up @@ -138,7 +140,8 @@ class ResponseFieldSpec(
fun readScalar(): CodeBlock {
val readMethod = SCALAR_LIST_ITEM_READ_METHODS[rawFieldType] ?: "readString"
return if (rawFieldType.isEnum(context)) {
CodeBlock.of("return \$T.valueOf(\$L.\$L());\n", rawFieldType, RESPONSE_LIST_ITEM_READER_PARAM.name, readMethod)
CodeBlock.of("return \$T.\$L(\$L.\$L());\n", rawFieldType, TypeDeclaration.ENUM_SAFE_VALUE_OF,
RESPONSE_LIST_ITEM_READER_PARAM.name, readMethod)
} else {
CodeBlock.of("return \$L.\$L();\n", RESPONSE_LIST_ITEM_READER_PARAM.name, readMethod)
}
Expand Down Expand Up @@ -279,9 +282,11 @@ class ResponseFieldSpec(
return CodeBlock.builder()
.add(
if (listItemType.isEnum(context)) {
CodeBlock.of("\$L.\$L(((\$L) \$L).name());\n", RESPONSE_LIST_ITEM_WRITER_PARAM.name, writeMethod, listItemType, OBJECT_VALUE_PARAM.name)
CodeBlock.of("\$L.\$L(((\$L) \$L).name());\n", RESPONSE_LIST_ITEM_WRITER_PARAM.name, writeMethod,
listItemType, OBJECT_VALUE_PARAM.name)
} else {
CodeBlock.of("\$L.\$L(\$L);\n", RESPONSE_LIST_ITEM_WRITER_PARAM.name, writeMethod, OBJECT_VALUE_PARAM.name)
CodeBlock.of("\$L.\$L(\$L);\n", RESPONSE_LIST_ITEM_WRITER_PARAM.name, writeMethod,
OBJECT_VALUE_PARAM.name)
})
.build()
}
Expand All @@ -297,7 +302,8 @@ class ResponseFieldSpec(

fun writeObject(): CodeBlock {
return CodeBlock.builder()
.addStatement("\$L.writeObject(((\$L) \$L).\$L)", RESPONSE_LIST_ITEM_WRITER_PARAM.name, listItemType, OBJECT_VALUE_PARAM.name, marshaller)
.addStatement("\$L.writeObject(((\$L) \$L).\$L)", RESPONSE_LIST_ITEM_WRITER_PARAM.name, listItemType,
OBJECT_VALUE_PARAM.name, marshaller)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.apollographql.apollo.compiler.ir

import com.apollographql.apollo.compiler.Annotations
import com.apollographql.apollo.compiler.ClassNames
import com.apollographql.apollo.compiler.InputTypeSpecBuilder
import com.squareup.javapoet.TypeSpec
import com.squareup.javapoet.*
import javax.lang.model.element.Modifier

data class TypeDeclaration(
Expand All @@ -23,13 +24,6 @@ data class TypeDeclaration(
}

private fun enumTypeToTypeSpec(): TypeSpec {
fun TypeSpec.Builder.addEnumJavaDoc(): TypeSpec.Builder {
if (!description.isNullOrEmpty()) {
addJavadoc("\$L\n", description)
}
return this
}

fun TypeSpec.Builder.addEnumConstants(): TypeSpec.Builder {
values?.forEach { value ->
val typeSpec = TypeSpec.anonymousClassBuilder("")
Expand All @@ -54,11 +48,56 @@ data class TypeDeclaration(
return this
}

val enumConstants = values?.map { value ->
value.name to TypeSpec.anonymousClassBuilder("")
.apply {
if (!value.description.isNullOrEmpty()) {
addJavadoc("\$L\n", value.description)
}
}
.apply {
if (value.isDeprecated == true) {
addAnnotation(Annotations.DEPRECATED)
if (!value.deprecationReason.isNullOrBlank()) {
addJavadoc("@deprecated \$L\n", value.deprecationReason)
}
}
}
.build()
}
val unknownConstantTypeSpec = TypeSpec.anonymousClassBuilder("")
.addJavadoc("\$L\n", "Auto generated constant for unknown enum values")
.build()
val safeValueOfMethodSpec = MethodSpec.methodBuilder(ENUM_SAFE_VALUE_OF)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(ParameterSpec.builder(ClassNames.STRING, "value").build())
.returns(ClassName.get("", name))
.addCode(CodeBlock.builder()
.beginControlFlow("for (\$L enumValue : values())", name)
.beginControlFlow("if (enumValue.name().equals(value))")
.addStatement("return enumValue")
.endControlFlow()
.endControlFlow()
.addStatement("return \$L.\$L", name, ENUM_UNKNOWN_CONSTANT)
.build()
)
.build()

return TypeSpec.enumBuilder(name)
.addAnnotation(Annotations.GENERATED_BY_APOLLO)
.addModifiers(Modifier.PUBLIC)
.addEnumConstants()
.addEnumJavaDoc()
.apply {
enumConstants?.forEach { (name, typeSpec) ->
addEnumConstant(name, typeSpec)
}
}
.addEnumConstant(ENUM_UNKNOWN_CONSTANT, unknownConstantTypeSpec)
.apply {
if (!description.isNullOrEmpty()) {
addJavadoc("\$L\n", description)
}
}
.addMethod(safeValueOfMethodSpec)
.build()
}

Expand All @@ -69,5 +108,7 @@ data class TypeDeclaration(
val KIND_INPUT_OBJECT_TYPE: String = "InputObjectType"
val KIND_ENUM: String = "EnumType"
val KIND_SCALAR_TYPE: String = "ScalarType"
val ENUM_UNKNOWN_CONSTANT: String = "\$UNKNOWN"
val ENUM_SAFE_VALUE_OF: String = "safeValueOf"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.arguments_complex.type;

import java.lang.Deprecated;
import java.lang.String;
import javax.annotation.Generated;

/**
Expand Down Expand Up @@ -28,5 +29,19 @@ public enum Episode {
* @deprecated For test purpose only
*/
@Deprecated
DEPRECATED
DEPRECATED,

/**
* Auto generated constant for unknown enum values
*/
$UNKNOWN;

public static Episode safeValueOf(String value) {
for (Episode enumValue : values()) {
if (enumValue.name().equals(value)) {
return enumValue;
}
}
return Episode.$UNKNOWN;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.arguments_simple.type;

import java.lang.Deprecated;
import java.lang.String;
import javax.annotation.Generated;

/**
Expand Down Expand Up @@ -28,5 +29,19 @@ public enum Episode {
* @deprecated For test purpose only
*/
@Deprecated
DEPRECATED
DEPRECATED,

/**
* Auto generated constant for unknown enum values
*/
$UNKNOWN;

public static Episode safeValueOf(String value) {
for (Episode enumValue : values()) {
if (enumValue.name().equals(value)) {
return enumValue;
}
}
return Episode.$UNKNOWN;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.deprecation.type;

import java.lang.Deprecated;
import java.lang.String;
import javax.annotation.Generated;

/**
Expand Down Expand Up @@ -28,5 +29,19 @@ public enum Episode {
* @deprecated For test purpose only
*/
@Deprecated
DEPRECATED
DEPRECATED,

/**
* Auto generated constant for unknown enum values
*/
$UNKNOWN;

public static Episode safeValueOf(String value) {
for (Episode enumValue : values()) {
if (enumValue.name().equals(value)) {
return enumValue;
}
}
return Episode.$UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,13 @@ public Hero map(ResponseReader reader) {
final List<Episode> appearsIn = reader.readList($responseFields[2], new ResponseReader.ListReader<Episode>() {
@Override
public Episode read(ResponseReader.ListItemReader listItemReader) {
return Episode.valueOf(listItemReader.readString());
return Episode.safeValueOf(listItemReader.readString());
}
});
final String firstAppearsInStr = reader.readString($responseFields[3]);
final Episode firstAppearsIn;
if (firstAppearsInStr != null) {
firstAppearsIn = Episode.valueOf(firstAppearsInStr);
firstAppearsIn = Episode.safeValueOf(firstAppearsInStr);
} else {
firstAppearsIn = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.enum_type.type;

import java.lang.String;
import javax.annotation.Generated;

/**
Expand All @@ -20,5 +21,19 @@ public enum Episode {
/**
* Star Wars Episode VI: Return of the Jedi, released in 1983. (JEDI in lowercase)
*/
jedi
jedi,

/**
* Auto generated constant for unknown enum values
*/
$UNKNOWN;

public static Episode safeValueOf(String value) {
for (Episode enumValue : values()) {
if (enumValue.name().equals(value)) {
return enumValue;
}
}
return Episode.$UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public Hero map(ResponseReader reader) {
final List<Episode> appearsIn = reader.readList($responseFields[2], new ResponseReader.ListReader<Episode>() {
@Override
public Episode read(ResponseReader.ListItemReader listItemReader) {
return Episode.valueOf(listItemReader.readString());
return Episode.safeValueOf(listItemReader.readString());
}
});
final Fragments fragments = reader.readConditional($responseFields[3], new ResponseReader.ConditionalTypeReader<Fragments>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.fragment_with_inline_fragment.type;

import java.lang.Deprecated;
import java.lang.String;
import javax.annotation.Generated;

/**
Expand Down Expand Up @@ -28,5 +29,19 @@ public enum Episode {
* @deprecated For test purpose only
*/
@Deprecated
DEPRECATED
DEPRECATED,

/**
* Auto generated constant for unknown enum values
*/
$UNKNOWN;

public static Episode safeValueOf(String value) {
for (Episode enumValue : values()) {
if (enumValue.name().equals(value)) {
return enumValue;
}
}
return Episode.$UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public Friend map(ResponseReader reader) {
final List<Episode> appearsIn = reader.readList($responseFields[1], new ResponseReader.ListReader<Episode>() {
@Override
public Episode read(ResponseReader.ListItemReader listItemReader) {
return Episode.valueOf(listItemReader.readString());
return Episode.safeValueOf(listItemReader.readString());
}
});
return new Friend(__typename, appearsIn);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.inline_fragments_with_friends.type;

import java.lang.Deprecated;
import java.lang.String;
import javax.annotation.Generated;

/**
Expand Down Expand Up @@ -28,5 +29,19 @@ public enum Episode {
* @deprecated For test purpose only
*/
@Deprecated
DEPRECATED
DEPRECATED,

/**
* Auto generated constant for unknown enum values
*/
$UNKNOWN;

public static Episode safeValueOf(String value) {
for (Episode enumValue : values()) {
if (enumValue.name().equals(value)) {
return enumValue;
}
}
return Episode.$UNKNOWN;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.input_object_type.type;

import java.lang.Deprecated;
import java.lang.String;
import javax.annotation.Generated;

/**
Expand Down Expand Up @@ -28,5 +29,19 @@ public enum Episode {
* @deprecated For test purpose only
*/
@Deprecated
DEPRECATED
DEPRECATED,

/**
* Auto generated constant for unknown enum values
*/
$UNKNOWN;

public static Episode safeValueOf(String value) {
for (Episode enumValue : values()) {
if (enumValue.name().equals(value)) {
return enumValue;
}
}
return Episode.$UNKNOWN;
}
}
Loading