mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Use StringBuilder to deduplicate String constants
This commit is contained in:
committed by
Jake Wharton
parent
30c973e494
commit
3994723c3c
@@ -159,6 +159,16 @@ 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 resultName = nameAllocator.newName("result")
|
val resultName = nameAllocator.newName("result")
|
||||||
|
|
||||||
@@ -185,10 +195,10 @@ 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 {
|
||||||
result.addStatement("%N = %N.fromJson(%N) ?: throw·%T(%P)",
|
val exception = jsonDataException(
|
||||||
property.localName, nameAllocator[property.delegateKey], readerParam,
|
"Non-null value '", property.localName, "' was null at ", readerParam)
|
||||||
JsonDataException::class,
|
result.addStatement("%N = %N.fromJson(%N) ?: throw·%L",
|
||||||
"Non-null value '${property.localName}' was null at \${${readerParam.name}.path}")
|
property.localName, nameAllocator[property.delegateKey], readerParam, exception)
|
||||||
}
|
}
|
||||||
result.addStatement("%N = true", property.localIsPresentName)
|
result.addStatement("%N = true", property.localIsPresentName)
|
||||||
result.endControlFlow()
|
result.endControlFlow()
|
||||||
@@ -197,10 +207,11 @@ 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 {
|
||||||
result.addStatement("%L -> %N = %N.fromJson(%N) ?: throw·%T(%P)",
|
val exception = jsonDataException(
|
||||||
|
"Non-null value '", property.localName, "' was null at ", readerParam)
|
||||||
|
result.addStatement("%L -> %N = %N.fromJson(%N) ?: throw·%L",
|
||||||
index, property.localName, nameAllocator[property.delegateKey], readerParam,
|
index, property.localName, nameAllocator[property.delegateKey], readerParam,
|
||||||
JsonDataException::class,
|
exception)
|
||||||
"Non-null value '${property.localName}' was null at \${${readerParam.name}.path}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -230,8 +241,8 @@ internal class AdapterGenerator(
|
|||||||
result.addCode(separator)
|
result.addCode(separator)
|
||||||
result.addCode("%N = %N", property.name, property.localName)
|
result.addCode("%N = %N", property.name, property.localName)
|
||||||
if (property.isRequired) {
|
if (property.isRequired) {
|
||||||
result.addCode(" ?: throw·%T(%P)", JsonDataException::class,
|
result.addCode(" ?: throw·%L", jsonDataException(
|
||||||
"Required property '${property.localName}' missing at \${${readerParam.name}.path}")
|
"Required property '", property.localName, "' missing at ", readerParam))
|
||||||
}
|
}
|
||||||
separator = ",\n"
|
separator = ",\n"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user