Refactor missing/null properties to helper utils instead (#900)

* Create missing property and unexpected null templates

* Fix rebase conflicts
This commit is contained in:
Zac Sweers
2019-09-08 17:36:58 -04:00
committed by GitHub
parent 329d0e14b0
commit 619be26e95
2 changed files with 27 additions and 17 deletions

View File

@@ -31,7 +31,6 @@ import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.joinToCode import com.squareup.kotlinpoet.joinToCode
import com.squareup.moshi.JsonAdapter import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.JsonReader import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
@@ -176,16 +175,6 @@ internal class AdapterGenerator(
.build() .build()
} }
private fun jsonDataException(
description: String,
identifier: String,
condition: String,
reader: ParameterSpec
): CodeBlock {
return CodeBlock.of("%T(%T(%S).append(%S).append(%S).append(%N.path).toString())",
JsonDataException::class, StringBuilder::class, description, identifier, condition, reader)
}
private fun generateFromJsonFun(): FunSpec { private fun generateFromJsonFun(): FunSpec {
val result = FunSpec.builder("fromJson") val result = FunSpec.builder("fromJson")
.addModifiers(KModifier.OVERRIDE) .addModifiers(KModifier.OVERRIDE)
@@ -210,8 +199,7 @@ internal class AdapterGenerator(
result.addStatement("%N = %N.fromJson(%N)", result.addStatement("%N = %N.fromJson(%N)",
property.localName, nameAllocator[property.delegateKey], readerParam) property.localName, nameAllocator[property.delegateKey], readerParam)
} else { } else {
val exception = jsonDataException( val exception = unexpectedNull(property.localName, readerParam)
"Non-null value '", property.localName, "' was null at ", readerParam)
result.addStatement("%N = %N.fromJson(%N) ?: throw·%L", result.addStatement("%N = %N.fromJson(%N) ?: throw·%L",
property.localName, nameAllocator[property.delegateKey], readerParam, exception) property.localName, nameAllocator[property.delegateKey], readerParam, exception)
} }
@@ -222,8 +210,7 @@ internal class AdapterGenerator(
result.addStatement("%L -> %N = %N.fromJson(%N)", result.addStatement("%L -> %N = %N.fromJson(%N)",
index, property.localName, nameAllocator[property.delegateKey], readerParam) index, property.localName, nameAllocator[property.delegateKey], readerParam)
} else { } else {
val exception = jsonDataException( val exception = unexpectedNull(property.localName, readerParam)
"Non-null value '", property.localName, "' was null at ", readerParam)
result.addStatement("%L -> %N = %N.fromJson(%N) ?: throw·%L", result.addStatement("%L -> %N = %N.fromJson(%N) ?: throw·%L",
index, property.localName, nameAllocator[property.delegateKey], readerParam, index, property.localName, nameAllocator[property.delegateKey], readerParam,
exception) exception)
@@ -306,8 +293,9 @@ internal class AdapterGenerator(
result.addCode("%N = %N", property.name, property.localName) result.addCode("%N = %N", property.name, property.localName)
} }
if (!property.isTransient && property.isRequired) { if (!property.isTransient && property.isRequired) {
result.addCode(" ?: throw·%L", jsonDataException( val missingPropertyBlock =
"Required property '", property.localName, "' missing at ", readerParam)) CodeBlock.of("%T.missingProperty(%S, %N)", Util::class, property.localName, readerParam)
result.addCode(" ?: throw·%L", missingPropertyBlock)
} }
separator = ",\n" separator = ",\n"
} }
@@ -334,6 +322,10 @@ internal class AdapterGenerator(
return result.build() return result.build()
} }
private fun unexpectedNull(identifier: String, reader: ParameterSpec): CodeBlock {
return CodeBlock.of("%T.unexpectedNull(%S, %N)", Util::class, identifier, reader)
}
private fun generateToJsonFun(): FunSpec { private fun generateToJsonFun(): FunSpec {
val result = FunSpec.builder("toJson") val result = FunSpec.builder("toJson")
.addModifiers(KModifier.OVERRIDE) .addModifiers(KModifier.OVERRIDE)

View File

@@ -17,7 +17,9 @@ package com.squareup.moshi.internal;
import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonClass; import com.squareup.moshi.JsonClass;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.JsonQualifier; import com.squareup.moshi.JsonQualifier;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.Moshi; import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types; import com.squareup.moshi.Types;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@@ -42,6 +44,8 @@ import static com.squareup.moshi.Types.subtypeOf;
import static com.squareup.moshi.Types.supertypeOf; import static com.squareup.moshi.Types.supertypeOf;
public final class Util { public final class Util {
private static final String REQUIRED_PROPERTY_TEMPLATE = "Required property '%s' missing at %s";
private static final String UNEXPECTED_NULL_TEMPLATE = "Non-null value '%s' was null at %s";
public static final Set<Annotation> NO_ANNOTATIONS = Collections.emptySet(); public static final Set<Annotation> NO_ANNOTATIONS = Collections.emptySet();
public static final Type[] EMPTY_TYPE_ARRAY = new Type[] {}; public static final Type[] EMPTY_TYPE_ARRAY = new Type[] {};
@Nullable private static final Class<?> DEFAULT_CONSTRUCTOR_MARKER; @Nullable private static final Class<?> DEFAULT_CONSTRUCTOR_MARKER;
@@ -619,4 +623,18 @@ public final class Util {
} }
return mask; return mask;
} }
public static JsonDataException missingProperty(String property, JsonReader reader) {
return jsonDataException(REQUIRED_PROPERTY_TEMPLATE, property, reader);
}
public static JsonDataException unexpectedNull(String property, JsonReader reader) {
return jsonDataException(UNEXPECTED_NULL_TEMPLATE, property, reader);
}
private static JsonDataException jsonDataException(
String template, String property, JsonReader reader) {
return new JsonDataException(
String.format(template, property, reader.getPath()));
}
} }