diff --git a/wire-library/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt b/wire-library/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt index 7797080fad..a396ed91a5 100644 --- a/wire-library/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt +++ b/wire-library/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt @@ -101,11 +101,11 @@ import com.squareup.wire.schema.internal.eligibleAsAnnotationMember import com.squareup.wire.schema.internal.javaPackage import com.squareup.wire.schema.internal.optionValueToInt import com.squareup.wire.schema.internal.optionValueToLong -import java.util.Locale import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.channels.SendChannel import okio.ByteString import okio.ByteString.Companion.encode +import java.util.Locale class KotlinGenerator private constructor( val schema: Schema, @@ -415,25 +415,30 @@ class KotlinGenerator private constructor( if (emitAndroid) { newName("CREATOR", "CREATOR") } - message.fieldsAndFlatOneOfFields().forEach { field -> - if (field.name == field.type!!.simpleName || - schema.getType(field.qualifiedName) != null - ) { - newName(field.qualifiedName, field) - } else { - newName(field.name, field) - } - } - message.boxOneOfs().forEach { oneOf -> - val fieldName = newName(oneOf.name, oneOf) - val keysFieldName = boxedOneOfKeysFieldName(fieldName) - check(newName(keysFieldName) == keysFieldName) { - "unexpected name collision for keys set of boxed one of, ${oneOf.name}" - } - newName(boxedOneOfClassName(oneOf.name), boxedOneOfClassName(oneOf.name)) - oneOf.fields.forEach { field -> - val keyFieldName = boxedOneOfKeyFieldName(oneOf.name, field.name) - newName(keyFieldName, keyFieldName) + for (fieldOrOneOf in message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + if (fieldOrOneOf.name == fieldOrOneOf.type!!.simpleName || + schema.getType(fieldOrOneOf.qualifiedName) != null + ) { + newName(fieldOrOneOf.qualifiedName, fieldOrOneOf) + } else { + newName(fieldOrOneOf.name, fieldOrOneOf) + } + } + is OneOf -> { + val fieldName = newName(fieldOrOneOf.name, fieldOrOneOf) + val keysFieldName = boxedOneOfKeysFieldName(fieldName) + check(newName(keysFieldName) == keysFieldName) { + "unexpected name collision for keys set of boxed one of, ${fieldOrOneOf.name}" + } + newName(boxedOneOfClassName(fieldOrOneOf.name), boxedOneOfClassName(fieldOrOneOf.name)) + fieldOrOneOf.fields.forEach { field -> + val keyFieldName = boxedOneOfKeyFieldName(fieldOrOneOf.name, field.name) + newName(keyFieldName, keyFieldName) + } + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") } } } @@ -555,13 +560,18 @@ class KotlinGenerator private constructor( funBuilder.addStatement("val builder = Builder()") - type.fieldsAndFlatOneOfFields().forEach { field -> - val fieldName = nameAllocator[field] - funBuilder.addStatement("builder.%1L = %1L", fieldName) - } - type.boxOneOfs().forEach { oneOf -> - val fieldName = nameAllocator[oneOf] - funBuilder.addStatement("builder.%1L = %1L", fieldName) + for (fieldOrOneOf in type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = nameAllocator[fieldOrOneOf] + funBuilder.addStatement("builder.%1L = %1L", fieldName) + } + is OneOf -> { + val fieldName = nameAllocator[fieldOrOneOf] + funBuilder.addStatement("builder.%1L = %1L", fieldName) + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") + } } return funBuilder @@ -593,13 +603,18 @@ class KotlinGenerator private constructor( addStatement("if (%N !is %T) return·false", otherName, kotlinType) addStatement("if (unknownFields != %N.unknownFields) return·false", otherName) - for (field in type.fieldsAndFlatOneOfFields()) { - val fieldName = localNameAllocator[field] - addStatement("if (%1L != %2N.%1L) return·false", fieldName, otherName) - } - for (oneOf in type.boxOneOfs()) { - val fieldName = localNameAllocator[oneOf] - addStatement("if (%1L != %2N.%1L) return·false", fieldName, otherName) + for (fieldOrOneOf in type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = localNameAllocator[fieldOrOneOf] + addStatement("if (%1L != %2N.%1L) return·false", fieldName, otherName) + } + is OneOf -> { + val fieldName = localNameAllocator[fieldOrOneOf] + addStatement("if (%1L != %2N.%1L) return·false", fieldName, otherName) + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") + } } addStatement("return true") } @@ -640,20 +655,26 @@ class KotlinGenerator private constructor( beginControlFlow("if (%N == 0)", resultName) addStatement("%N = unknownFields.hashCode()", resultName) - for (field in type.fieldsAndFlatOneOfFields()) { - val fieldName = localNameAllocator[field] - add("%1N = %1N * 37 + ", resultName) - if (field.isRepeated || field.isRequired || field.isMap) { - addStatement("%L.hashCode()", fieldName) - } else { - addStatement("(%L?.hashCode() ?: 0)", fieldName) + + for (fieldOrOneOf in type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = localNameAllocator[fieldOrOneOf] + add("%1N = %1N * 37 + ", resultName) + if (fieldOrOneOf.isRepeated || fieldOrOneOf.isRequired || fieldOrOneOf.isMap) { + addStatement("%L.hashCode()", fieldName) + } else { + addStatement("(%L?.hashCode() ?: 0)", fieldName) + } + } + is OneOf -> { + val fieldName = localNameAllocator[fieldOrOneOf] + add("%1N = %1N * 37 + ", resultName) + addStatement("(%L?.hashCode() ?: 0)", fieldName) + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") } } - for (oneOf in type.boxOneOfs()) { - val fieldName = localNameAllocator[oneOf] - add("%1N = %1N * 37 + ", resultName) - addStatement("(%L?.hashCode() ?: 0)", fieldName) - } addStatement("super.hashCode = %N", resultName) endControlFlow() @@ -680,24 +701,29 @@ class KotlinGenerator private constructor( val result = FunSpec.builder("copy") .returns(type.typeName) val fieldNames = mutableListOf() - for (field in type.fieldsAndFlatOneOfFields()) { - val fieldName = nameAllocator[field] - result.addParameter( - ParameterSpec.builder(fieldName, field.typeNameForMessageField) - .defaultValue("this.%N", fieldName) - .build() - ) - fieldNames += fieldName - } - for (oneOf in type.boxOneOfs()) { - val fieldName = nameAllocator[oneOf] - val fieldClass = type.oneOfClassFor(oneOf, nameAllocator) - result.addParameter( - ParameterSpec.builder(fieldName, fieldClass) - .defaultValue("this.%N", fieldName) - .build() - ) - fieldNames += fieldName + for (fieldOrOneOf in type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = nameAllocator[fieldOrOneOf] + result.addParameter( + ParameterSpec.builder(fieldName, fieldOrOneOf.typeNameForMessageField) + .defaultValue("this.%N", fieldName) + .build() + ) + fieldNames += fieldName + } + is OneOf -> { + val fieldName = nameAllocator[fieldOrOneOf] + val fieldClass = type.oneOfClassFor(fieldOrOneOf, nameAllocator) + result.addParameter( + ParameterSpec.builder(fieldName, fieldClass) + .defaultValue("this.%N", fieldName) + .build() + ) + fieldNames += fieldName + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") + } } result.addParameter(ParameterSpec.builder("unknownFields", ByteString::class) .defaultValue("this.unknownFields") @@ -741,53 +767,64 @@ class KotlinGenerator private constructor( add("return %T(⇥\n", className) val missingRequiredFields = MemberName("com.squareup.wire.internal", "missingRequiredFields") - type.fieldsAndFlatOneOfFields().forEach { field -> - val fieldName = nameAllocator[field] + for (fieldOrOneOf in type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = nameAllocator[fieldOrOneOf] - val throwExceptionBlock = if (!field.isRepeated && field.isRequired) { - CodeBlock.of(" ?: throw %1M(%2L, %2S)", missingRequiredFields, nameAllocator[field]) - } else { - CodeBlock.of("") - } + val throwExceptionBlock = if (!fieldOrOneOf.isRepeated && fieldOrOneOf.isRequired) { + CodeBlock.of(" ?: throw %1M(%2L, %2S)", missingRequiredFields, nameAllocator[fieldOrOneOf]) + } else { + CodeBlock.of("") + } - addStatement("%1L = %1L%2L,", fieldName, throwExceptionBlock) - } - for (oneOf in type.boxOneOfs()) { - val fieldName = nameAllocator[oneOf] - addStatement("%1L = %1L,", fieldName) + addStatement("%1L = %1L%2L,", fieldName, throwExceptionBlock) + } + is OneOf -> { + val fieldName = nameAllocator[fieldOrOneOf] + addStatement("%1L = %1L,", fieldName) + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") + } } add("unknownFields = buildUnknownFields()") add("⇤\n)\n") // close the block } - type.fieldsAndFlatOneOfFields().forEach { field -> - val fieldName = nameAllocator[field] + for (fieldOrOneOf in type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = nameAllocator[fieldOrOneOf] - val propertyBuilder = PropertySpec.builder(fieldName, field.typeNameForBuilderField) - .mutable(true) - .initializer(field.identityValue) + val propertyBuilder = PropertySpec.builder(fieldName, fieldOrOneOf.typeNameForBuilderField) + .mutable(true) + .initializer(fieldOrOneOf.identityValue) - if (javaInterOp) { - propertyBuilder.addAnnotation(JvmField::class) - } + if (javaInterOp) { + propertyBuilder.addAnnotation(JvmField::class) + } - builder.addProperty(propertyBuilder.build()) - } - for (oneOf in type.boxOneOfs()) { - val fieldName = nameAllocator[oneOf] - val fieldClass = type.oneOfClassFor(oneOf, nameAllocator) + builder.addProperty(propertyBuilder.build()) + } + is OneOf -> { + val fieldName = nameAllocator[fieldOrOneOf] + val fieldClass = type.oneOfClassFor(fieldOrOneOf, nameAllocator) - val propertyBuilder = PropertySpec.builder(fieldName, fieldClass) - .mutable(true) - .initializer(CodeBlock.of("null")) + val propertyBuilder = PropertySpec.builder(fieldName, fieldClass) + .mutable(true) + .initializer(CodeBlock.of("null")) - if (javaInterOp) { - propertyBuilder.addAnnotation(JvmField::class) - } + if (javaInterOp) { + propertyBuilder.addAnnotation(JvmField::class) + } - builder.addProperty(propertyBuilder.build()) + builder.addProperty(propertyBuilder.build()) + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") + } } + // TODO(Benoit) Order builder setters in the same order as the fields. type.fields.forEach { field -> builder.addFunction(builderSetter(field, nameAllocator, builderClass, oneOf = null)) } @@ -894,98 +931,140 @@ class KotlinGenerator private constructor( val nameAllocator = nameAllocator(message) val byteClass = ProtoType.BYTES.typeName - val fields = message.fieldsAndFlatOneOfFields() - fields.forEach { field -> - val fieldClass = field.typeNameForMessageField - val fieldName = nameAllocator[field] + val parametersAndProperties = parametersAndProperties(message, nameAllocator) + for ((parameter, property) in parametersAndProperties) { + constructorBuilder.addParameter(parameter) + classBuilder.addProperty(property) + } + + val unknownFields = nameAllocator["unknownFields"] + constructorBuilder.addParameter( + ParameterSpec.builder(unknownFields, byteClass) + .defaultValue("%T.EMPTY", byteClass) + .build()) + + classBuilder.primaryConstructor(constructorBuilder.build()) + } + + private fun parametersAndProperties( + message: MessageType, nameAllocator: NameAllocator + ): List> { + val result = mutableListOf>() + + for (fieldOrOneOf in message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> result.add( + constructorParameterAndProperty( + message, + fieldOrOneOf, + nameAllocator + ) + ) + is OneOf -> result.add( + constructorParameterAndProperty( + message, + fieldOrOneOf, + nameAllocator + ) + ) + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") + } + } + + return result + } + + private fun constructorParameterAndProperty( + message: MessageType, + field: Field, + nameAllocator: NameAllocator + ): Pair { + val fieldClass = field.typeNameForMessageField + val fieldName = nameAllocator[field] - val parameterSpec = ParameterSpec.builder(fieldName, fieldClass) - if (!field.isRequired) { - parameterSpec.defaultValue(field.identityValue) + val parameterSpec = ParameterSpec.builder(fieldName, fieldClass) + if (!field.isRequired) { + parameterSpec.defaultValue(field.identityValue) + } + + val initializer = when { + field.type!!.valueType?.isStruct == true -> { + CodeBlock.of( + "%M(%S, %N)", + MemberName("com.squareup.wire.internal", "immutableCopyOfMapWithStructValues"), + fieldName, + fieldName + ) + } + field.type!!.isStruct -> { + CodeBlock.of( + "%M(%S, %N)", + MemberName("com.squareup.wire.internal", "immutableCopyOfStruct"), + fieldName, + fieldName + ) + } + field.isRepeated || field.isMap -> { + CodeBlock.of( + "%M(%S, %N)", + MemberName("com.squareup.wire.internal", "immutableCopyOf"), + fieldName, + fieldName + ) } + else -> CodeBlock.of("%N", fieldName) + } - val initializer = when { - field.type!!.valueType?.isStruct == true -> { - CodeBlock.of("%M(%S, %N)", - MemberName("com.squareup.wire.internal", "immutableCopyOfMapWithStructValues"), - fieldName, - fieldName + val propertySpec = PropertySpec.builder(fieldName, fieldClass) + .initializer(initializer) + .apply { + if (field.isDeprecated) { + addAnnotation( + AnnotationSpec.builder(Deprecated::class) + .addMember("message = %S", "$fieldName is deprecated") + .build() ) } - field.type!!.isStruct -> { - CodeBlock.of("%M(%S, %N)", - MemberName("com.squareup.wire.internal", "immutableCopyOfStruct"), - fieldName, - fieldName - ) + for (annotation in optionAnnotations(field.options)) { + addAnnotation(annotation) } - field.isRepeated || field.isMap -> { - CodeBlock.of("%M(%S, %N)", - MemberName("com.squareup.wire.internal", "immutableCopyOf"), - fieldName, - fieldName - ) + addAnnotation(wireFieldAnnotation(message, field)) + if (javaInterOp) { + addAnnotation(JvmField::class) + } + if (field.documentation.isNotBlank()) { + addKdoc("%L\n", field.documentation.sanitizeKdoc()) + } + if (field.isExtension) { + addKdoc("Extension source: %L\n", field.location.withPathOnly()) } - else -> CodeBlock.of("%N", fieldName) } + return parameterSpec.build() to propertySpec.build() + } - constructorBuilder.addParameter(parameterSpec.build()) - classBuilder.addProperty(PropertySpec.builder(fieldName, fieldClass) - .initializer(initializer) - .apply { - if (field.isDeprecated) { - addAnnotation( - AnnotationSpec.builder(Deprecated::class) - .addMember("message = %S", "$fieldName is deprecated") - .build() - ) - } - for (annotation in optionAnnotations(field.options)) { - addAnnotation(annotation) - } - addAnnotation(wireFieldAnnotation(message, field)) - if (javaInterOp) { - addAnnotation(JvmField::class) - } - if (field.documentation.isNotBlank()) { - addKdoc("%L\n", field.documentation.sanitizeKdoc()) - } - if (field.isExtension) { - addKdoc("Extension source: %L\n", field.location.withPathOnly()) - } - } - .build()) - } - - val boxOneOfs = message.boxOneOfs() - for (oneOf in boxOneOfs) { - val fieldClass = message.oneOfClassFor(oneOf, nameAllocator) - val fieldName = oneOf.name + private fun constructorParameterAndProperty( + message: MessageType, + oneOf: OneOf, + nameAllocator: NameAllocator + ): Pair { + val fieldClass = message.oneOfClassFor(oneOf, nameAllocator) + val fieldName = oneOf.name - val parameterSpec = ParameterSpec.builder(fieldName, fieldClass) - parameterSpec.defaultValue(CodeBlock.of("null")) + val parameterSpec = ParameterSpec.builder(fieldName, fieldClass) + parameterSpec.defaultValue(CodeBlock.of("null")) - constructorBuilder.addParameter(parameterSpec.build()) - classBuilder.addProperty(PropertySpec.builder(fieldName, fieldClass) - .initializer(CodeBlock.of("%N", fieldName)) - .apply { - if (javaInterOp) { - addAnnotation(JvmField::class) - } - if (oneOf.documentation.isNotBlank()) { - addKdoc("%L\n", oneOf.documentation.sanitizeKdoc()) - } + val propertySpec = PropertySpec.builder(fieldName, fieldClass) + .initializer(CodeBlock.of("%N", fieldName)) + .apply { + if (javaInterOp) { + addAnnotation(JvmField::class) } - .build()) - } - - val unknownFields = nameAllocator["unknownFields"] - constructorBuilder.addParameter( - ParameterSpec.builder(unknownFields, byteClass) - .defaultValue("%T.EMPTY", byteClass) - .build()) + if (oneOf.documentation.isNotBlank()) { + addKdoc("%L\n", oneOf.documentation.sanitizeKdoc()) + } + } - classBuilder.primaryConstructor(constructorBuilder.build()) + return parameterSpec.build() to propertySpec.build() } private fun wireFieldAnnotation(message: MessageType, field: Field): AnnotationSpec { @@ -1050,45 +1129,50 @@ class KotlinGenerator private constructor( val sanitizeMember = MemberName("com.squareup.wire.internal", "sanitize") val localNameAllocator = nameAllocator.copy() val className = generatedTypeName(type) - val fields = type.fieldsAndFlatOneOfFields() - val boxOneOfs = type.boxOneOfs() + val fieldsAndOneOfs = type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs() val body = buildCodeBlock { - if (fields.isEmpty() && boxOneOfs.isEmpty()) { + if (fieldsAndOneOfs.isEmpty()) { addStatement("return %S", className.simpleName + "{}") } else { val resultName = localNameAllocator.newName("result") addStatement("val %N = mutableListOf<%T>()", resultName, STRING) - for (field in fields) { - val fieldName = localNameAllocator[field] - if (field.isRepeated || field.isMap) { - add("if (%N.isNotEmpty()) ", fieldName) - } else if (field.acceptsNull) { - add("if (%N != null) ", fieldName) - } - addStatement("%N += %P", resultName, buildCodeBlock { - add(fieldName) - if (field.isRedacted) { - add("=██") - } else { - if (field.type == ProtoType.STRING) { - add("=\${%M($fieldName)}", sanitizeMember) - } else { + + for (fieldOrOneOf in fieldsAndOneOfs) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = localNameAllocator[fieldOrOneOf] + if (fieldOrOneOf.isRepeated || fieldOrOneOf.isMap) { + add("if (%N.isNotEmpty()) ", fieldName) + } else if (fieldOrOneOf.acceptsNull) { + add("if (%N != null) ", fieldName) + } + addStatement("%N += %P", resultName, buildCodeBlock { + add(fieldName) + if (fieldOrOneOf.isRedacted) { + add("=██") + } else { + if (fieldOrOneOf.type == ProtoType.STRING) { + add("=\${%M($fieldName)}", sanitizeMember) + } else { + add("=\$") + add(fieldName) + } + } + }) + } + is OneOf -> { + val fieldName = localNameAllocator[fieldOrOneOf] + add("if (%N != null) ", fieldName) + addStatement("%N += %P", resultName, buildCodeBlock { + add(fieldName) + // TODO(Benoit) Do we redact if one of the field is redacted? add("=\$") add(fieldName) } + ) } - }) - } - for (oneOf in boxOneOfs) { - val fieldName = localNameAllocator[oneOf] - add("if (%N != null) ", fieldName) - addStatement("%N += %P", resultName, buildCodeBlock { - add(fieldName) - // TODO(Benoit) Do we redact if one of the field is redacted? - add("=\$") - add(fieldName) + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") } - ) } addStatement( @@ -1112,7 +1196,7 @@ class KotlinGenerator private constructor( companionBuilder: TypeSpec.Builder, nameAllocator: NameAllocator ) { - for (field in type.fieldsAndFlatOneOfFields()) { + for (field in type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs().filterIsInstance()) { val default = field.default ?: continue val fieldName = "DEFAULT_" + nameAllocator[field].toUpperCase(Locale.US) @@ -1282,18 +1366,23 @@ class KotlinGenerator private constructor( val body = buildCodeBlock { addStatement("var %N = value.unknownFields.size", sizeName) - message.fieldsAndFlatOneOfFields().forEach { field -> - val fieldName = localNameAllocator[field] - if (field.encodeMode == EncodeMode.OMIT_IDENTITY) { - add("if (value.%1L != %2L) ", fieldName, field.identityValue) + for (fieldOrOneOf in message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = localNameAllocator[fieldOrOneOf] + if (fieldOrOneOf.encodeMode == EncodeMode.OMIT_IDENTITY) { + add("if (value.%1L != %2L) ", fieldName, fieldOrOneOf.identityValue) + } + addStatement("%N += %L.encodedSizeWithTag(%L, value.%L)", sizeName, adapterFor(fieldOrOneOf), + fieldOrOneOf.tag, fieldName) + } + is OneOf -> { + val fieldName = localNameAllocator[fieldOrOneOf] + add("if (value.%1L != %2L) ", fieldName, "null") + addStatement("%N += value.%L.encodedSizeWithTag()", sizeName, fieldName) + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") } - addStatement("%N += %L.encodedSizeWithTag(%L, value.%L)", sizeName, adapterFor(field), - field.tag, fieldName) - } - for (boxOneOf in message.boxOneOfs()) { - val fieldName = localNameAllocator[boxOneOf] - add("if (value.%1L != %2L) ", fieldName, "null") - addStatement("%N += value.%L.encodedSizeWithTag()", sizeName, fieldName) } addStatement("return %N", sizeName) } @@ -1319,15 +1408,17 @@ class KotlinGenerator private constructor( val body = buildCodeBlock { val nameAllocator = nameAllocator(message) - message.fieldsAndFlatOneOfFields().forEach { field -> + for (field in message.fields + message.flatOneOfs().flatMap { it.fields }) { val fieldName = nameAllocator[field] if (field.encodeMode == EncodeMode.OMIT_IDENTITY) { add("if (value.%L != %L) ", fieldName, field.identityValue) } - addStatement("%L.encodeWithTag(writer, %L, value.%L)", + addStatement( + "%L.encodeWithTag(writer, %L, value.%L)", adapterFor(field), field.tag, - fieldName) + fieldName + ) } for (boxOneOf in message.boxOneOfs()) { val fieldName = nameAllocator[boxOneOf] @@ -1350,20 +1441,25 @@ class KotlinGenerator private constructor( val nameAllocator = nameAllocator(message).copy() val declarationBody = buildCodeBlock { - message.fieldsAndFlatOneOfFields().forEach { field -> - val fieldName = nameAllocator[field] - val fieldDeclaration: CodeBlock = field.getDeclaration(fieldName) - addStatement("%L", fieldDeclaration) - } - for (boxOneOf in message.boxOneOfs()) { - val fieldName = nameAllocator[boxOneOf] - val oneOfClass = (message.typeName as ClassName) - .nestedClass(boxedOneOfClassName(boxOneOf.name)) - .parameterizedBy(STAR) - val fieldClass = com.squareup.wire.OneOf::class.asClassName() - .parameterizedBy(oneOfClass, STAR).copy(nullable = true) - val fieldDeclaration = CodeBlock.of("var $fieldName: %T = %L", fieldClass, "null") - addStatement("%L", fieldDeclaration) + for (fieldOrOneOf in message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = nameAllocator[fieldOrOneOf] + val fieldDeclaration: CodeBlock = fieldOrOneOf.getDeclaration(fieldName) + addStatement("%L", fieldDeclaration) + } + is OneOf -> { + val fieldName = nameAllocator[fieldOrOneOf] + val oneOfClass = (message.typeName as ClassName) + .nestedClass(boxedOneOfClassName(fieldOrOneOf.name)) + .parameterizedBy(STAR) + val fieldClass = com.squareup.wire.OneOf::class.asClassName() + .parameterizedBy(oneOfClass, STAR).copy(nullable = true) + val fieldDeclaration = CodeBlock.of("var $fieldName: %T = %L", fieldClass, "null") + addStatement("%L", fieldDeclaration) + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") + } } } @@ -1371,20 +1467,31 @@ class KotlinGenerator private constructor( addStatement("return·%T(⇥", className) val missingRequiredFields = MemberName("com.squareup.wire.internal", "missingRequiredFields") - message.fieldsAndFlatOneOfFields().forEach { field -> - val fieldName = nameAllocator[field] + for (fieldOrOneOf in message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) { + when (fieldOrOneOf) { + is Field -> { + val fieldName = nameAllocator[fieldOrOneOf] + + val throwExceptionBlock = + if (!fieldOrOneOf.isRepeated && !fieldOrOneOf.isMap && fieldOrOneOf.isRequired) { + CodeBlock.of( + " ?: throw %1M(%2L, %3S)", + missingRequiredFields, + fieldName, + fieldOrOneOf.name + ) + } else { + CodeBlock.of("") + } - val throwExceptionBlock = if (!field.isRepeated && !field.isMap && field.isRequired) { - CodeBlock.of(" ?: throw %1M(%2L, %3S)", missingRequiredFields, fieldName, field.name) - } else { - CodeBlock.of("") + addStatement("%1L = %1L%2L,", fieldName, throwExceptionBlock) + } + is OneOf -> { + val fieldName = nameAllocator[fieldOrOneOf] + addStatement("%1L = %1L,", fieldName) + } + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") } - - addStatement("%1L = %1L%2L,", fieldName, throwExceptionBlock) - } - for (boxOneOf in message.boxOneOfs()) { - val fieldName = nameAllocator[boxOneOf] - addStatement("%1L = %1L,", fieldName) } add("unknownFields = unknownFields") @@ -1392,7 +1499,7 @@ class KotlinGenerator private constructor( } val decodeBlock = buildCodeBlock { - val fields = message.fieldsAndFlatOneOfFields() + val fields = message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs().filterIsInstance() val boxOneOfs = message.boxOneOfs() if (fields.isEmpty() && boxOneOfs.isEmpty()) { addStatement("val unknownFields = reader.forEachTag(reader::readUnknownField)") @@ -1401,7 +1508,7 @@ class KotlinGenerator private constructor( addStatement("val unknownFields = reader.forEachTag { %L ->", tag) addStatement("⇥when (%L) {⇥", tag) - message.fieldsAndFlatOneOfFields().forEach { field -> + fields.forEach { field -> val fieldName = nameAllocator[field] val adapterName = field.getAdapterName() @@ -1482,7 +1589,7 @@ class KotlinGenerator private constructor( } val redactedFields = mutableListOf() - for (field in message.fieldsAndFlatOneOfFields()) { + for (field in message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs().filterIsInstance()) { val fieldName = nameAllocator[field] val redactedField = field.redact(fieldName) if (redactedField != null) { @@ -2111,22 +2218,20 @@ class KotlinGenerator private constructor( .build() } - private fun MessageType.fieldsAndFlatOneOfFields(): List { - val result = mutableListOf() - result.addAll(this.declaredFields) - result.addAll(this.extensionFields) - result.addAll(this.flatOneOfs().flatMap { it.fields }) - return result - } - - private fun MessageType.boxOneOfs(): List { - val result = mutableListOf() - for (oneOf in this.oneOfs) { - if (oneOf.fields.size >= boxOneOfsMinSize) { - result.add(oneOf) + private fun MessageType.fieldsAndFlatOneOfFieldsAndBoxedOneOfs(): List { + val fieldsAndFlatOneOfFields: List = + declaredFields + extensionFields + flatOneOfs().flatMap { it.fields } + + return (fieldsAndFlatOneOfFields + boxOneOfs()) + .sortedBy { fieldOrOneOf -> + when (fieldOrOneOf) { + is Field -> fieldOrOneOf.location.line + // TODO(Benoit) If boxed oneofs without fields become a problem, we can add location to + // oneofs and use that. + is OneOf -> fieldOrOneOf.fields.getOrNull(0)?.location?.line ?: 0 + else -> throw IllegalArgumentException("Unexpected element: $fieldOrOneOf") + } } - } - return result } private fun MessageType.flatOneOfs(): List { @@ -2139,6 +2244,10 @@ class KotlinGenerator private constructor( return result } + private fun MessageType.boxOneOfs(): List { + return oneOfs.filter { it.fields.size >= boxOneOfsMinSize } + } + private fun MessageType.oneOfClassFor(oneOf: OneOf, nameAllocator: NameAllocator): TypeName { val oneOfClass = (this.typeName as ClassName) .nestedClass(nameAllocator[boxedOneOfClassName(oneOf.name)]) diff --git a/wire-library/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt b/wire-library/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt index c03cca9b89..1e751313dd 100644 --- a/wire-library/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt +++ b/wire-library/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt @@ -194,10 +194,10 @@ class KotlinGeneratorTest { //language=kotlin val expected = """ package routeguide - + import com.squareup.wire.Service import com.squareup.wire.WireRpc - + /** * RouteGuide service interface. */ @@ -213,7 +213,7 @@ class KotlinGeneratorTest { ) public fun GetFeature(request: Point): Feature } - + """.trimIndent() val repoBuilder = RepoBuilder() @@ -236,11 +236,11 @@ class KotlinGeneratorTest { //language=kotlin val expected = """ package routeguide - + import com.squareup.wire.MessageSource import com.squareup.wire.Service import com.squareup.wire.WireRpc - + /** * RouteGuide service interface. */ @@ -256,7 +256,7 @@ class KotlinGeneratorTest { ) public fun RecordRoute(request: MessageSource): RouteSummary } - + """.trimIndent() val repoBuilder = RepoBuilder() @@ -279,12 +279,12 @@ class KotlinGeneratorTest { //language=kotlin val expected = """ package routeguide - + import com.squareup.wire.MessageSink import com.squareup.wire.Service import com.squareup.wire.WireRpc import kotlin.Unit - + /** * RouteGuide service interface. */ @@ -300,7 +300,7 @@ class KotlinGeneratorTest { ) public fun ListFeatures(request: Rectangle, response: MessageSink): Unit } - + """.trimIndent() val repoBuilder = RepoBuilder() @@ -324,13 +324,13 @@ class KotlinGeneratorTest { //language=kotlin val expected = """ package routeguide - + import com.squareup.wire.MessageSink import com.squareup.wire.MessageSource import com.squareup.wire.Service import com.squareup.wire.WireRpc import kotlin.Unit - + /** * RouteGuide service interface. */ @@ -346,7 +346,7 @@ class KotlinGeneratorTest { ) public fun RouteChat(request: MessageSource, response: MessageSink): Unit } - + """.trimIndent() val repoBuilder = RepoBuilder() @@ -373,10 +373,10 @@ class KotlinGeneratorTest { //language=kotlin val expected = """ package com.squareup.routeguide - + import com.squareup.wire.Service import com.squareup.wire.WireRpc - + /** * RouteGuide service interface. */ @@ -392,7 +392,7 @@ class KotlinGeneratorTest { ) public fun GetFeature(request: Point): Feature } - + """.trimIndent() val repoBuilder = RepoBuilder() @@ -654,10 +654,10 @@ class KotlinGeneratorTest { //language=kotlin val blockingClientInterface = """ package routeguide - + import com.squareup.wire.GrpcStreamingCall import com.squareup.wire.Service - + /** * RouteGuide service interface. */ @@ -667,16 +667,16 @@ class KotlinGeneratorTest { */ public fun RouteChat(): GrpcStreamingCall } - + """.trimIndent() //language=kotlin val blockingClientImplementation = """ package routeguide - + import com.squareup.wire.GrpcClient import com.squareup.wire.GrpcMethod import com.squareup.wire.GrpcStreamingCall - + /** * RouteGuide service interface. */ @@ -693,18 +693,18 @@ class KotlinGeneratorTest { responseAdapter = RouteNote.ADAPTER )) } - + """.trimIndent() //language=kotlin val blockingServer = """ package routeguide - + import com.squareup.wire.MessageSink import com.squareup.wire.MessageSource import com.squareup.wire.Service import com.squareup.wire.WireRpc import kotlin.Unit - + /** * RouteGuide service interface. */ @@ -720,15 +720,15 @@ class KotlinGeneratorTest { ) public fun RouteChat(request: MessageSource, response: MessageSink): Unit } - + """.trimIndent() //language=kotlin val suspendingClientInterface = """ package routeguide - + import com.squareup.wire.GrpcStreamingCall import com.squareup.wire.Service - + /** * RouteGuide service interface. */ @@ -738,16 +738,16 @@ class KotlinGeneratorTest { */ public fun RouteChat(): GrpcStreamingCall } - + """.trimIndent() //language=kotlin val suspendingClientImplementation = """ package routeguide - + import com.squareup.wire.GrpcClient import com.squareup.wire.GrpcMethod import com.squareup.wire.GrpcStreamingCall - + /** * RouteGuide service interface. */ @@ -764,18 +764,18 @@ class KotlinGeneratorTest { responseAdapter = RouteNote.ADAPTER )) } - + """.trimIndent() //language=kotlin val suspendingServer = """ package routeguide - + import com.squareup.wire.Service import com.squareup.wire.WireRpc import kotlin.Unit import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.channels.SendChannel - + /** * RouteGuide service interface. */ @@ -792,7 +792,7 @@ class KotlinGeneratorTest { public suspend fun RouteChat(request: ReceiveChannel, response: SendChannel): Unit } - + """.trimIndent() val repoBuilder = RepoBuilder() @@ -1534,14 +1534,100 @@ class KotlinGeneratorTest { | public override fun redact(`value`: RedactedFields): RedactedFields = value.copy( | a = "", | b = 0, - | secret_data = null, | c = null, | d = null, + | secret_data = null, | unknownFields = ByteString.EMPTY | ) """.trimMargin()) } + @Test + fun fieldsDeclarationOrderIsRespected() { + val repoBuilder = RepoBuilder() + .add("message.proto", """ + |syntax = "proto2"; + |message SomeMessage { + | optional string a = 1; + | optional string b = 2; + | oneof choice { + | string c = 3; + | string d = 8; + | } + | optional SecretData secret_data = 4; + | optional string e = 5; + | oneof decision { + | string f = 6; + | string g = 7; + | string h = 9; + | } + | optional string i = 10; + | oneof unique { + | string j = 12; + | } + | optional string k = 11; + |} + | + |message SecretData {} + |""".trimMargin() + ) + val code = repoBuilder.generateKotlin("SomeMessage", boxOneOfsMinSize = 3) + println(code) + assertThat(code).contains(""" + |public class SomeMessage( + | @field:WireField( + | tag = 1, + | adapter = "com.squareup.wire.ProtoAdapter#STRING" + | ) + | public val a: String? = null, + | @field:WireField( + | tag = 2, + | adapter = "com.squareup.wire.ProtoAdapter#STRING" + | ) + | public val b: String? = null, + | @field:WireField( + | tag = 3, + | adapter = "com.squareup.wire.ProtoAdapter#STRING", + | oneofName = "choice" + | ) + | public val c: String? = null, + | @field:WireField( + | tag = 8, + | adapter = "com.squareup.wire.ProtoAdapter#STRING", + | oneofName = "choice" + | ) + | public val d: String? = null, + | @field:WireField( + | tag = 4, + | adapter = "SecretData#ADAPTER" + | ) + | public val secret_data: SecretData? = null, + | @field:WireField( + | tag = 5, + | adapter = "com.squareup.wire.ProtoAdapter#STRING" + | ) + | public val e: String? = null, + | public val decision: OneOf, *>? = null, + | @field:WireField( + | tag = 10, + | adapter = "com.squareup.wire.ProtoAdapter#STRING" + | ) + | public val i: String? = null, + | @field:WireField( + | tag = 12, + | adapter = "com.squareup.wire.ProtoAdapter#STRING", + | oneofName = "unique" + | ) + | public val j: String? = null, + | @field:WireField( + | tag = 11, + | adapter = "com.squareup.wire.ProtoAdapter#STRING" + | ) + | public val k: String? = null, + | unknownFields: ByteString = ByteString.EMPTY + """.trimMargin()) + } + companion object { private val pointMessage = """ |message Point { diff --git a/wire-library/wire-test-utils/src/main/java/com/squareup/wire/schema/RepoBuilder.kt b/wire-library/wire-test-utils/src/main/java/com/squareup/wire/schema/RepoBuilder.kt index 4d64ce5688..da9a747045 100644 --- a/wire-library/wire-test-utils/src/main/java/com/squareup/wire/schema/RepoBuilder.kt +++ b/wire-library/wire-test-utils/src/main/java/com/squareup/wire/schema/RepoBuilder.kt @@ -23,12 +23,12 @@ import com.squareup.wire.java.Profile import com.squareup.wire.kotlin.KotlinGenerator import com.squareup.wire.kotlin.RpcCallStyle import com.squareup.wire.kotlin.RpcRole -import java.io.File -import java.io.IOException import okio.Path.Companion.toPath import okio.buffer import okio.fakefilesystem.FakeFileSystem import okio.source +import java.io.File +import java.io.IOException /** * Builds a repository of `.proto` and `.wire` files to create schemas, profiles, and adapters for @@ -117,12 +117,14 @@ class RepoBuilder { fun generateKotlin( typeName: String, - profileName: String? = null + profileName: String? = null, + boxOneOfsMinSize: Int = 5_000, ): String { val schema = schema() val kotlinGenerator = KotlinGenerator( - schema, - profile = profile(profileName), + schema, + profile = profile(profileName), + boxOneOfsMinSize = boxOneOfsMinSize ) val type = schema.getType(typeName)!! val typeSpec = kotlinGenerator.generateType(type) diff --git a/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/com/squareup/wire/proto2/alltypes/AllTypes.kt b/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/com/squareup/wire/proto2/alltypes/AllTypes.kt index afdd22c54c..4c4a23042e 100644 --- a/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/com/squareup/wire/proto2/alltypes/AllTypes.kt +++ b/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/com/squareup/wire/proto2/alltypes/AllTypes.kt @@ -387,6 +387,27 @@ public class AllTypes( map_string_string: Map = emptyMap(), map_string_message: Map = emptyMap(), map_string_enum: Map = emptyMap(), + @field:WireField( + tag = 601, + adapter = "com.squareup.wire.ProtoAdapter#STRING", + oneofName = "choice" + ) + @JvmField + public val oneof_string: String? = null, + @field:WireField( + tag = 602, + adapter = "com.squareup.wire.ProtoAdapter#INT32", + oneofName = "choice" + ) + @JvmField + public val oneof_int32: Int? = null, + @field:WireField( + tag = 603, + adapter = "com.squareup.wire.proto2.alltypes.AllTypes${'$'}NestedMessage#ADAPTER", + oneofName = "choice" + ) + @JvmField + public val oneof_nested_message: NestedMessage? = null, /** * Extension source: all_types_proto2.proto */ @@ -571,27 +592,6 @@ public class AllTypes( ext_pack_float: List = emptyList(), ext_pack_double: List = emptyList(), ext_pack_nested_enum: List = emptyList(), - @field:WireField( - tag = 601, - adapter = "com.squareup.wire.ProtoAdapter#STRING", - oneofName = "choice" - ) - @JvmField - public val oneof_string: String? = null, - @field:WireField( - tag = 602, - adapter = "com.squareup.wire.ProtoAdapter#INT32", - oneofName = "choice" - ) - @JvmField - public val oneof_int32: Int? = null, - @field:WireField( - tag = 603, - adapter = "com.squareup.wire.proto2.alltypes.AllTypes${'$'}NestedMessage#ADAPTER", - oneofName = "choice" - ) - @JvmField - public val oneof_nested_message: NestedMessage? = null, unknownFields: ByteString = ByteString.EMPTY ) : Message(ADAPTER, unknownFields) { @field:WireField( @@ -1316,6 +1316,9 @@ public class AllTypes( builder.map_string_string = map_string_string builder.map_string_message = map_string_message builder.map_string_enum = map_string_enum + builder.oneof_string = oneof_string + builder.oneof_int32 = oneof_int32 + builder.oneof_nested_message = oneof_nested_message builder.ext_opt_int32 = ext_opt_int32 builder.ext_opt_uint32 = ext_opt_uint32 builder.ext_opt_sint32 = ext_opt_sint32 @@ -1364,9 +1367,6 @@ public class AllTypes( builder.ext_pack_float = ext_pack_float builder.ext_pack_double = ext_pack_double builder.ext_pack_nested_enum = ext_pack_nested_enum - builder.oneof_string = oneof_string - builder.oneof_int32 = oneof_int32 - builder.oneof_nested_message = oneof_nested_message builder.addUnknownFields(unknownFields) return builder } @@ -1460,6 +1460,9 @@ public class AllTypes( if (map_string_string != other.map_string_string) return false if (map_string_message != other.map_string_message) return false if (map_string_enum != other.map_string_enum) return false + if (oneof_string != other.oneof_string) return false + if (oneof_int32 != other.oneof_int32) return false + if (oneof_nested_message != other.oneof_nested_message) return false if (ext_opt_int32 != other.ext_opt_int32) return false if (ext_opt_uint32 != other.ext_opt_uint32) return false if (ext_opt_sint32 != other.ext_opt_sint32) return false @@ -1508,9 +1511,6 @@ public class AllTypes( if (ext_pack_float != other.ext_pack_float) return false if (ext_pack_double != other.ext_pack_double) return false if (ext_pack_nested_enum != other.ext_pack_nested_enum) return false - if (oneof_string != other.oneof_string) return false - if (oneof_int32 != other.oneof_int32) return false - if (oneof_nested_message != other.oneof_nested_message) return false return true } @@ -1603,6 +1603,9 @@ public class AllTypes( result = result * 37 + map_string_string.hashCode() result = result * 37 + map_string_message.hashCode() result = result * 37 + map_string_enum.hashCode() + result = result * 37 + (oneof_string?.hashCode() ?: 0) + result = result * 37 + (oneof_int32?.hashCode() ?: 0) + result = result * 37 + (oneof_nested_message?.hashCode() ?: 0) result = result * 37 + (ext_opt_int32?.hashCode() ?: 0) result = result * 37 + (ext_opt_uint32?.hashCode() ?: 0) result = result * 37 + (ext_opt_sint32?.hashCode() ?: 0) @@ -1651,9 +1654,6 @@ public class AllTypes( result = result * 37 + ext_pack_float.hashCode() result = result * 37 + ext_pack_double.hashCode() result = result * 37 + ext_pack_nested_enum.hashCode() - result = result * 37 + (oneof_string?.hashCode() ?: 0) - result = result * 37 + (oneof_int32?.hashCode() ?: 0) - result = result * 37 + (oneof_nested_message?.hashCode() ?: 0) super.hashCode = result } return result @@ -1746,6 +1746,9 @@ public class AllTypes( if (map_string_string.isNotEmpty()) result += """map_string_string=$map_string_string""" if (map_string_message.isNotEmpty()) result += """map_string_message=$map_string_message""" if (map_string_enum.isNotEmpty()) result += """map_string_enum=$map_string_enum""" + if (oneof_string != null) result += """oneof_string=${sanitize(oneof_string)}""" + if (oneof_int32 != null) result += """oneof_int32=$oneof_int32""" + if (oneof_nested_message != null) result += """oneof_nested_message=$oneof_nested_message""" if (ext_opt_int32 != null) result += """ext_opt_int32=$ext_opt_int32""" if (ext_opt_uint32 != null) result += """ext_opt_uint32=$ext_opt_uint32""" if (ext_opt_sint32 != null) result += """ext_opt_sint32=$ext_opt_sint32""" @@ -1797,9 +1800,6 @@ public class AllTypes( if (ext_pack_double.isNotEmpty()) result += """ext_pack_double=$ext_pack_double""" if (ext_pack_nested_enum.isNotEmpty()) result += """ext_pack_nested_enum=$ext_pack_nested_enum""" - if (oneof_string != null) result += """oneof_string=${sanitize(oneof_string)}""" - if (oneof_int32 != null) result += """oneof_int32=$oneof_int32""" - if (oneof_nested_message != null) result += """oneof_nested_message=$oneof_nested_message""" return result.joinToString(prefix = "AllTypes{", separator = ", ", postfix = "}") } @@ -1889,6 +1889,9 @@ public class AllTypes( map_string_string: Map = this.map_string_string, map_string_message: Map = this.map_string_message, map_string_enum: Map = this.map_string_enum, + oneof_string: String? = this.oneof_string, + oneof_int32: Int? = this.oneof_int32, + oneof_nested_message: NestedMessage? = this.oneof_nested_message, ext_opt_int32: Int? = this.ext_opt_int32, ext_opt_uint32: Int? = this.ext_opt_uint32, ext_opt_sint32: Int? = this.ext_opt_sint32, @@ -1937,9 +1940,6 @@ public class AllTypes( ext_pack_float: List = this.ext_pack_float, ext_pack_double: List = this.ext_pack_double, ext_pack_nested_enum: List = this.ext_pack_nested_enum, - oneof_string: String? = this.oneof_string, - oneof_int32: Int? = this.oneof_int32, - oneof_nested_message: NestedMessage? = this.oneof_nested_message, unknownFields: ByteString = this.unknownFields ): AllTypes = AllTypes(opt_int32, opt_uint32, opt_sint32, opt_fixed32, opt_sfixed32, opt_int64, opt_uint64, opt_sint64, opt_fixed64, opt_sfixed64, opt_bool, opt_float, opt_double, @@ -1954,17 +1954,17 @@ public class AllTypes( default_fixed32, default_sfixed32, default_int64, default_uint64, default_sint64, default_fixed64, default_sfixed64, default_bool, default_float, default_double, default_string, default_bytes, default_nested_enum, map_int32_int32, map_string_string, - map_string_message, map_string_enum, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, - ext_opt_fixed32, ext_opt_sfixed32, ext_opt_int64, ext_opt_uint64, ext_opt_sint64, - ext_opt_fixed64, ext_opt_sfixed64, ext_opt_bool, ext_opt_float, ext_opt_double, - ext_opt_string, ext_opt_bytes, ext_opt_nested_enum, ext_opt_nested_message, ext_rep_int32, - ext_rep_uint32, ext_rep_sint32, ext_rep_fixed32, ext_rep_sfixed32, ext_rep_int64, - ext_rep_uint64, ext_rep_sint64, ext_rep_fixed64, ext_rep_sfixed64, ext_rep_bool, - ext_rep_float, ext_rep_double, ext_rep_string, ext_rep_bytes, ext_rep_nested_enum, - ext_rep_nested_message, ext_pack_int32, ext_pack_uint32, ext_pack_sint32, ext_pack_fixed32, - ext_pack_sfixed32, ext_pack_int64, ext_pack_uint64, ext_pack_sint64, ext_pack_fixed64, - ext_pack_sfixed64, ext_pack_bool, ext_pack_float, ext_pack_double, ext_pack_nested_enum, - oneof_string, oneof_int32, oneof_nested_message, unknownFields) + map_string_message, map_string_enum, oneof_string, oneof_int32, oneof_nested_message, + ext_opt_int32, ext_opt_uint32, ext_opt_sint32, ext_opt_fixed32, ext_opt_sfixed32, + ext_opt_int64, ext_opt_uint64, ext_opt_sint64, ext_opt_fixed64, ext_opt_sfixed64, + ext_opt_bool, ext_opt_float, ext_opt_double, ext_opt_string, ext_opt_bytes, + ext_opt_nested_enum, ext_opt_nested_message, ext_rep_int32, ext_rep_uint32, ext_rep_sint32, + ext_rep_fixed32, ext_rep_sfixed32, ext_rep_int64, ext_rep_uint64, ext_rep_sint64, + ext_rep_fixed64, ext_rep_sfixed64, ext_rep_bool, ext_rep_float, ext_rep_double, + ext_rep_string, ext_rep_bytes, ext_rep_nested_enum, ext_rep_nested_message, ext_pack_int32, + ext_pack_uint32, ext_pack_sint32, ext_pack_fixed32, ext_pack_sfixed32, ext_pack_int64, + ext_pack_uint64, ext_pack_sint64, ext_pack_fixed64, ext_pack_sfixed64, ext_pack_bool, + ext_pack_float, ext_pack_double, ext_pack_nested_enum, unknownFields) public class Builder : Message.Builder() { @JvmField @@ -2222,6 +2222,15 @@ public class AllTypes( @JvmField public var map_string_enum: Map = emptyMap() + @JvmField + public var oneof_string: String? = null + + @JvmField + public var oneof_int32: Int? = null + + @JvmField + public var oneof_nested_message: NestedMessage? = null + @JvmField public var ext_opt_int32: Int? = null @@ -2366,15 +2375,6 @@ public class AllTypes( @JvmField public var ext_pack_nested_enum: List = emptyList() - @JvmField - public var oneof_string: String? = null - - @JvmField - public var oneof_int32: Int? = null - - @JvmField - public var oneof_nested_message: NestedMessage? = null - public fun opt_int32(opt_int32: Int?): Builder { this.opt_int32 = opt_int32 return this @@ -3211,6 +3211,9 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, + oneof_string = oneof_string, + oneof_int32 = oneof_int32, + oneof_nested_message = oneof_nested_message, ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, @@ -3259,9 +3262,6 @@ public class AllTypes( ext_pack_float = ext_pack_float, ext_pack_double = ext_pack_double, ext_pack_nested_enum = ext_pack_nested_enum, - oneof_string = oneof_string, - oneof_int32 = oneof_int32, - oneof_nested_message = oneof_nested_message, unknownFields = buildUnknownFields() ) } @@ -3410,6 +3410,9 @@ public class AllTypes( size += map_string_stringAdapter.encodedSizeWithTag(502, value.map_string_string) size += map_string_messageAdapter.encodedSizeWithTag(503, value.map_string_message) size += map_string_enumAdapter.encodedSizeWithTag(504, value.map_string_enum) + size += ProtoAdapter.STRING.encodedSizeWithTag(601, value.oneof_string) + size += ProtoAdapter.INT32.encodedSizeWithTag(602, value.oneof_int32) + size += NestedMessage.ADAPTER.encodedSizeWithTag(603, value.oneof_nested_message) size += ProtoAdapter.INT32.encodedSizeWithTag(1001, value.ext_opt_int32) size += ProtoAdapter.UINT32.encodedSizeWithTag(1002, value.ext_opt_uint32) size += ProtoAdapter.SINT32.encodedSizeWithTag(1003, value.ext_opt_sint32) @@ -3459,9 +3462,6 @@ public class AllTypes( size += ProtoAdapter.FLOAT.asPacked().encodedSizeWithTag(1212, value.ext_pack_float) size += ProtoAdapter.DOUBLE.asPacked().encodedSizeWithTag(1213, value.ext_pack_double) size += NestedEnum.ADAPTER.asPacked().encodedSizeWithTag(1216, value.ext_pack_nested_enum) - size += ProtoAdapter.STRING.encodedSizeWithTag(601, value.oneof_string) - size += ProtoAdapter.INT32.encodedSizeWithTag(602, value.oneof_int32) - size += NestedMessage.ADAPTER.encodedSizeWithTag(603, value.oneof_nested_message) return size } @@ -3691,6 +3691,9 @@ public class AllTypes( val map_string_string = mutableMapOf() val map_string_message = mutableMapOf() val map_string_enum = mutableMapOf() + var oneof_string: String? = null + var oneof_int32: Int? = null + var oneof_nested_message: NestedMessage? = null var ext_opt_int32: Int? = null var ext_opt_uint32: Int? = null var ext_opt_sint32: Int? = null @@ -3739,9 +3742,6 @@ public class AllTypes( val ext_pack_float = mutableListOf() val ext_pack_double = mutableListOf() val ext_pack_nested_enum = mutableListOf() - var oneof_string: String? = null - var oneof_int32: Int? = null - var oneof_nested_message: NestedMessage? = null val unknownFields = reader.forEachTag { tag -> when (tag) { 1 -> opt_int32 = ProtoAdapter.INT32.decode(reader) @@ -3849,6 +3849,9 @@ public class AllTypes( 502 -> map_string_string.putAll(map_string_stringAdapter.decode(reader)) 503 -> map_string_message.putAll(map_string_messageAdapter.decode(reader)) 504 -> map_string_enum.putAll(map_string_enumAdapter.decode(reader)) + 601 -> oneof_string = ProtoAdapter.STRING.decode(reader) + 602 -> oneof_int32 = ProtoAdapter.INT32.decode(reader) + 603 -> oneof_nested_message = NestedMessage.ADAPTER.decode(reader) 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) 1003 -> ext_opt_sint32 = ProtoAdapter.SINT32.decode(reader) @@ -3909,9 +3912,6 @@ public class AllTypes( } catch (e: ProtoAdapter.EnumConstantNotFoundException) { reader.addUnknownField(tag, FieldEncoding.VARINT, e.value.toLong()) } - 601 -> oneof_string = ProtoAdapter.STRING.decode(reader) - 602 -> oneof_int32 = ProtoAdapter.INT32.decode(reader) - 603 -> oneof_nested_message = NestedMessage.ADAPTER.decode(reader) else -> reader.readUnknownField(tag) } } @@ -4003,6 +4003,9 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, + oneof_string = oneof_string, + oneof_int32 = oneof_int32, + oneof_nested_message = oneof_nested_message, ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, @@ -4051,9 +4054,6 @@ public class AllTypes( ext_pack_float = ext_pack_float, ext_pack_double = ext_pack_double, ext_pack_nested_enum = ext_pack_nested_enum, - oneof_string = oneof_string, - oneof_int32 = oneof_int32, - oneof_nested_message = oneof_nested_message, unknownFields = unknownFields ) } @@ -4063,9 +4063,9 @@ public class AllTypes( req_nested_message = NestedMessage.ADAPTER.redact(value.req_nested_message), rep_nested_message = value.rep_nested_message.redactElements(NestedMessage.ADAPTER), map_string_message = value.map_string_message.redactElements(NestedMessage.ADAPTER), + oneof_nested_message = value.oneof_nested_message?.let(NestedMessage.ADAPTER::redact), ext_opt_nested_message = value.ext_opt_nested_message?.let(NestedMessage.ADAPTER::redact), ext_rep_nested_message = value.ext_rep_nested_message.redactElements(NestedMessage.ADAPTER), - oneof_nested_message = value.oneof_nested_message?.let(NestedMessage.ADAPTER::redact), unknownFields = ByteString.EMPTY ) } diff --git a/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/All32.kt b/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/All32.kt index 877b6cc481..9641263dab 100644 --- a/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/All32.kt +++ b/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/All32.kt @@ -78,11 +78,6 @@ public class All32( pack_sint32: List = emptyList(), pack_fixed32: List = emptyList(), pack_sfixed32: List = emptyList(), - map_int32_int32: Map = emptyMap(), - map_int32_uint32: Map = emptyMap(), - map_int32_sint32: Map = emptyMap(), - map_int32_fixed32: Map = emptyMap(), - map_int32_sfixed32: Map = emptyMap(), @field:WireField( tag = 401, adapter = "com.squareup.wire.ProtoAdapter#INT32", @@ -99,6 +94,11 @@ public class All32( ) @JvmField public val oneof_sfixed32: Int? = null, + map_int32_int32: Map = emptyMap(), + map_int32_uint32: Map = emptyMap(), + map_int32_sint32: Map = emptyMap(), + map_int32_fixed32: Map = emptyMap(), + map_int32_sfixed32: Map = emptyMap(), unknownFields: ByteString = ByteString.EMPTY ) : Message(ADAPTER, unknownFields) { @field:WireField( @@ -261,13 +261,13 @@ public class All32( builder.pack_sint32 = pack_sint32 builder.pack_fixed32 = pack_fixed32 builder.pack_sfixed32 = pack_sfixed32 + builder.oneof_int32 = oneof_int32 + builder.oneof_sfixed32 = oneof_sfixed32 builder.map_int32_int32 = map_int32_int32 builder.map_int32_uint32 = map_int32_uint32 builder.map_int32_sint32 = map_int32_sint32 builder.map_int32_fixed32 = map_int32_fixed32 builder.map_int32_sfixed32 = map_int32_sfixed32 - builder.oneof_int32 = oneof_int32 - builder.oneof_sfixed32 = oneof_sfixed32 builder.addUnknownFields(unknownFields) return builder } @@ -291,13 +291,13 @@ public class All32( if (pack_sint32 != other.pack_sint32) return false if (pack_fixed32 != other.pack_fixed32) return false if (pack_sfixed32 != other.pack_sfixed32) return false + if (oneof_int32 != other.oneof_int32) return false + if (oneof_sfixed32 != other.oneof_sfixed32) return false if (map_int32_int32 != other.map_int32_int32) return false if (map_int32_uint32 != other.map_int32_uint32) return false if (map_int32_sint32 != other.map_int32_sint32) return false if (map_int32_fixed32 != other.map_int32_fixed32) return false if (map_int32_sfixed32 != other.map_int32_sfixed32) return false - if (oneof_int32 != other.oneof_int32) return false - if (oneof_sfixed32 != other.oneof_sfixed32) return false return true } @@ -320,13 +320,13 @@ public class All32( result = result * 37 + pack_sint32.hashCode() result = result * 37 + pack_fixed32.hashCode() result = result * 37 + pack_sfixed32.hashCode() + result = result * 37 + (oneof_int32?.hashCode() ?: 0) + result = result * 37 + (oneof_sfixed32?.hashCode() ?: 0) result = result * 37 + map_int32_int32.hashCode() result = result * 37 + map_int32_uint32.hashCode() result = result * 37 + map_int32_sint32.hashCode() result = result * 37 + map_int32_fixed32.hashCode() result = result * 37 + map_int32_sfixed32.hashCode() - result = result * 37 + (oneof_int32?.hashCode() ?: 0) - result = result * 37 + (oneof_sfixed32?.hashCode() ?: 0) super.hashCode = result } return result @@ -349,13 +349,13 @@ public class All32( if (pack_sint32.isNotEmpty()) result += """pack_sint32=$pack_sint32""" if (pack_fixed32.isNotEmpty()) result += """pack_fixed32=$pack_fixed32""" if (pack_sfixed32.isNotEmpty()) result += """pack_sfixed32=$pack_sfixed32""" + if (oneof_int32 != null) result += """oneof_int32=$oneof_int32""" + if (oneof_sfixed32 != null) result += """oneof_sfixed32=$oneof_sfixed32""" if (map_int32_int32.isNotEmpty()) result += """map_int32_int32=$map_int32_int32""" if (map_int32_uint32.isNotEmpty()) result += """map_int32_uint32=$map_int32_uint32""" if (map_int32_sint32.isNotEmpty()) result += """map_int32_sint32=$map_int32_sint32""" if (map_int32_fixed32.isNotEmpty()) result += """map_int32_fixed32=$map_int32_fixed32""" if (map_int32_sfixed32.isNotEmpty()) result += """map_int32_sfixed32=$map_int32_sfixed32""" - if (oneof_int32 != null) result += """oneof_int32=$oneof_int32""" - if (oneof_sfixed32 != null) result += """oneof_sfixed32=$oneof_sfixed32""" return result.joinToString(prefix = "All32{", separator = ", ", postfix = "}") } @@ -375,18 +375,18 @@ public class All32( pack_sint32: List = this.pack_sint32, pack_fixed32: List = this.pack_fixed32, pack_sfixed32: List = this.pack_sfixed32, + oneof_int32: Int? = this.oneof_int32, + oneof_sfixed32: Int? = this.oneof_sfixed32, map_int32_int32: Map = this.map_int32_int32, map_int32_uint32: Map = this.map_int32_uint32, map_int32_sint32: Map = this.map_int32_sint32, map_int32_fixed32: Map = this.map_int32_fixed32, map_int32_sfixed32: Map = this.map_int32_sfixed32, - oneof_int32: Int? = this.oneof_int32, - oneof_sfixed32: Int? = this.oneof_sfixed32, unknownFields: ByteString = this.unknownFields ): All32 = All32(my_int32, my_uint32, my_sint32, my_fixed32, my_sfixed32, rep_int32, rep_uint32, rep_sint32, rep_fixed32, rep_sfixed32, pack_int32, pack_uint32, pack_sint32, pack_fixed32, - pack_sfixed32, map_int32_int32, map_int32_uint32, map_int32_sint32, map_int32_fixed32, - map_int32_sfixed32, oneof_int32, oneof_sfixed32, unknownFields) + pack_sfixed32, oneof_int32, oneof_sfixed32, map_int32_int32, map_int32_uint32, + map_int32_sint32, map_int32_fixed32, map_int32_sfixed32, unknownFields) public class Builder : Message.Builder() { @JvmField @@ -434,6 +434,12 @@ public class All32( @JvmField public var pack_sfixed32: List = emptyList() + @JvmField + public var oneof_int32: Int? = null + + @JvmField + public var oneof_sfixed32: Int? = null + @JvmField public var map_int32_int32: Map = emptyMap() @@ -449,12 +455,6 @@ public class All32( @JvmField public var map_int32_sfixed32: Map = emptyMap() - @JvmField - public var oneof_int32: Int? = null - - @JvmField - public var oneof_sfixed32: Int? = null - /** * Prefixing so the generated code doesn't rename it weirdly. */ @@ -596,13 +596,13 @@ public class All32( pack_sint32 = pack_sint32, pack_fixed32 = pack_fixed32, pack_sfixed32 = pack_sfixed32, + oneof_int32 = oneof_int32, + oneof_sfixed32 = oneof_sfixed32, map_int32_int32 = map_int32_int32, map_int32_uint32 = map_int32_uint32, map_int32_sint32 = map_int32_sint32, map_int32_fixed32 = map_int32_fixed32, map_int32_sfixed32 = map_int32_sfixed32, - oneof_int32 = oneof_int32, - oneof_sfixed32 = oneof_sfixed32, unknownFields = buildUnknownFields() ) } @@ -650,13 +650,13 @@ public class All32( size += ProtoAdapter.SINT32.asPacked().encodedSizeWithTag(303, value.pack_sint32) size += ProtoAdapter.FIXED32.asPacked().encodedSizeWithTag(304, value.pack_fixed32) size += ProtoAdapter.SFIXED32.asPacked().encodedSizeWithTag(305, value.pack_sfixed32) + size += ProtoAdapter.INT32.encodedSizeWithTag(401, value.oneof_int32) + size += ProtoAdapter.SFIXED32.encodedSizeWithTag(402, value.oneof_sfixed32) size += map_int32_int32Adapter.encodedSizeWithTag(501, value.map_int32_int32) size += map_int32_uint32Adapter.encodedSizeWithTag(502, value.map_int32_uint32) size += map_int32_sint32Adapter.encodedSizeWithTag(503, value.map_int32_sint32) size += map_int32_fixed32Adapter.encodedSizeWithTag(504, value.map_int32_fixed32) size += map_int32_sfixed32Adapter.encodedSizeWithTag(505, value.map_int32_sfixed32) - size += ProtoAdapter.INT32.encodedSizeWithTag(401, value.oneof_int32) - size += ProtoAdapter.SFIXED32.encodedSizeWithTag(402, value.oneof_sfixed32) return size } @@ -703,13 +703,13 @@ public class All32( val pack_sint32 = mutableListOf() val pack_fixed32 = mutableListOf() val pack_sfixed32 = mutableListOf() + var oneof_int32: Int? = null + var oneof_sfixed32: Int? = null val map_int32_int32 = mutableMapOf() val map_int32_uint32 = mutableMapOf() val map_int32_sint32 = mutableMapOf() val map_int32_fixed32 = mutableMapOf() val map_int32_sfixed32 = mutableMapOf() - var oneof_int32: Int? = null - var oneof_sfixed32: Int? = null val unknownFields = reader.forEachTag { tag -> when (tag) { 1 -> my_int32 = ProtoAdapter.INT32.decode(reader) @@ -727,13 +727,13 @@ public class All32( 303 -> pack_sint32.add(ProtoAdapter.SINT32.decode(reader)) 304 -> pack_fixed32.add(ProtoAdapter.FIXED32.decode(reader)) 305 -> pack_sfixed32.add(ProtoAdapter.SFIXED32.decode(reader)) + 401 -> oneof_int32 = ProtoAdapter.INT32.decode(reader) + 402 -> oneof_sfixed32 = ProtoAdapter.SFIXED32.decode(reader) 501 -> map_int32_int32.putAll(map_int32_int32Adapter.decode(reader)) 502 -> map_int32_uint32.putAll(map_int32_uint32Adapter.decode(reader)) 503 -> map_int32_sint32.putAll(map_int32_sint32Adapter.decode(reader)) 504 -> map_int32_fixed32.putAll(map_int32_fixed32Adapter.decode(reader)) 505 -> map_int32_sfixed32.putAll(map_int32_sfixed32Adapter.decode(reader)) - 401 -> oneof_int32 = ProtoAdapter.INT32.decode(reader) - 402 -> oneof_sfixed32 = ProtoAdapter.SFIXED32.decode(reader) else -> reader.readUnknownField(tag) } } @@ -753,13 +753,13 @@ public class All32( pack_sint32 = pack_sint32, pack_fixed32 = pack_fixed32, pack_sfixed32 = pack_sfixed32, + oneof_int32 = oneof_int32, + oneof_sfixed32 = oneof_sfixed32, map_int32_int32 = map_int32_int32, map_int32_uint32 = map_int32_uint32, map_int32_sint32 = map_int32_sint32, map_int32_fixed32 = map_int32_fixed32, map_int32_sfixed32 = map_int32_sfixed32, - oneof_int32 = oneof_int32, - oneof_sfixed32 = oneof_sfixed32, unknownFields = unknownFields ) } diff --git a/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/All64.kt b/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/All64.kt index 71c0ba73a7..85daf757e9 100644 --- a/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/All64.kt +++ b/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/All64.kt @@ -78,11 +78,6 @@ public class All64( pack_sint64: List = emptyList(), pack_fixed64: List = emptyList(), pack_sfixed64: List = emptyList(), - map_int64_int64: Map = emptyMap(), - map_int64_uint64: Map = emptyMap(), - map_int64_sint64: Map = emptyMap(), - map_int64_fixed64: Map = emptyMap(), - map_int64_sfixed64: Map = emptyMap(), @field:WireField( tag = 401, adapter = "com.squareup.wire.ProtoAdapter#INT64", @@ -99,6 +94,11 @@ public class All64( ) @JvmField public val oneof_sfixed64: Long? = null, + map_int64_int64: Map = emptyMap(), + map_int64_uint64: Map = emptyMap(), + map_int64_sint64: Map = emptyMap(), + map_int64_fixed64: Map = emptyMap(), + map_int64_sfixed64: Map = emptyMap(), unknownFields: ByteString = ByteString.EMPTY ) : Message(ADAPTER, unknownFields) { @field:WireField( @@ -263,13 +263,13 @@ public class All64( builder.pack_sint64 = pack_sint64 builder.pack_fixed64 = pack_fixed64 builder.pack_sfixed64 = pack_sfixed64 + builder.oneof_int64 = oneof_int64 + builder.oneof_sfixed64 = oneof_sfixed64 builder.map_int64_int64 = map_int64_int64 builder.map_int64_uint64 = map_int64_uint64 builder.map_int64_sint64 = map_int64_sint64 builder.map_int64_fixed64 = map_int64_fixed64 builder.map_int64_sfixed64 = map_int64_sfixed64 - builder.oneof_int64 = oneof_int64 - builder.oneof_sfixed64 = oneof_sfixed64 builder.addUnknownFields(unknownFields) return builder } @@ -293,13 +293,13 @@ public class All64( if (pack_sint64 != other.pack_sint64) return false if (pack_fixed64 != other.pack_fixed64) return false if (pack_sfixed64 != other.pack_sfixed64) return false + if (oneof_int64 != other.oneof_int64) return false + if (oneof_sfixed64 != other.oneof_sfixed64) return false if (map_int64_int64 != other.map_int64_int64) return false if (map_int64_uint64 != other.map_int64_uint64) return false if (map_int64_sint64 != other.map_int64_sint64) return false if (map_int64_fixed64 != other.map_int64_fixed64) return false if (map_int64_sfixed64 != other.map_int64_sfixed64) return false - if (oneof_int64 != other.oneof_int64) return false - if (oneof_sfixed64 != other.oneof_sfixed64) return false return true } @@ -322,13 +322,13 @@ public class All64( result = result * 37 + pack_sint64.hashCode() result = result * 37 + pack_fixed64.hashCode() result = result * 37 + pack_sfixed64.hashCode() + result = result * 37 + (oneof_int64?.hashCode() ?: 0) + result = result * 37 + (oneof_sfixed64?.hashCode() ?: 0) result = result * 37 + map_int64_int64.hashCode() result = result * 37 + map_int64_uint64.hashCode() result = result * 37 + map_int64_sint64.hashCode() result = result * 37 + map_int64_fixed64.hashCode() result = result * 37 + map_int64_sfixed64.hashCode() - result = result * 37 + (oneof_int64?.hashCode() ?: 0) - result = result * 37 + (oneof_sfixed64?.hashCode() ?: 0) super.hashCode = result } return result @@ -351,13 +351,13 @@ public class All64( if (pack_sint64.isNotEmpty()) result += """pack_sint64=$pack_sint64""" if (pack_fixed64.isNotEmpty()) result += """pack_fixed64=$pack_fixed64""" if (pack_sfixed64.isNotEmpty()) result += """pack_sfixed64=$pack_sfixed64""" + if (oneof_int64 != null) result += """oneof_int64=$oneof_int64""" + if (oneof_sfixed64 != null) result += """oneof_sfixed64=$oneof_sfixed64""" if (map_int64_int64.isNotEmpty()) result += """map_int64_int64=$map_int64_int64""" if (map_int64_uint64.isNotEmpty()) result += """map_int64_uint64=$map_int64_uint64""" if (map_int64_sint64.isNotEmpty()) result += """map_int64_sint64=$map_int64_sint64""" if (map_int64_fixed64.isNotEmpty()) result += """map_int64_fixed64=$map_int64_fixed64""" if (map_int64_sfixed64.isNotEmpty()) result += """map_int64_sfixed64=$map_int64_sfixed64""" - if (oneof_int64 != null) result += """oneof_int64=$oneof_int64""" - if (oneof_sfixed64 != null) result += """oneof_sfixed64=$oneof_sfixed64""" return result.joinToString(prefix = "All64{", separator = ", ", postfix = "}") } @@ -377,18 +377,18 @@ public class All64( pack_sint64: List = this.pack_sint64, pack_fixed64: List = this.pack_fixed64, pack_sfixed64: List = this.pack_sfixed64, + oneof_int64: Long? = this.oneof_int64, + oneof_sfixed64: Long? = this.oneof_sfixed64, map_int64_int64: Map = this.map_int64_int64, map_int64_uint64: Map = this.map_int64_uint64, map_int64_sint64: Map = this.map_int64_sint64, map_int64_fixed64: Map = this.map_int64_fixed64, map_int64_sfixed64: Map = this.map_int64_sfixed64, - oneof_int64: Long? = this.oneof_int64, - oneof_sfixed64: Long? = this.oneof_sfixed64, unknownFields: ByteString = this.unknownFields ): All64 = All64(my_int64, my_uint64, my_sint64, my_fixed64, my_sfixed64, rep_int64, rep_uint64, rep_sint64, rep_fixed64, rep_sfixed64, pack_int64, pack_uint64, pack_sint64, pack_fixed64, - pack_sfixed64, map_int64_int64, map_int64_uint64, map_int64_sint64, map_int64_fixed64, - map_int64_sfixed64, oneof_int64, oneof_sfixed64, unknownFields) + pack_sfixed64, oneof_int64, oneof_sfixed64, map_int64_int64, map_int64_uint64, + map_int64_sint64, map_int64_fixed64, map_int64_sfixed64, unknownFields) public class Builder : Message.Builder() { @JvmField @@ -436,6 +436,12 @@ public class All64( @JvmField public var pack_sfixed64: List = emptyList() + @JvmField + public var oneof_int64: Long? = null + + @JvmField + public var oneof_sfixed64: Long? = null + @JvmField public var map_int64_int64: Map = emptyMap() @@ -451,12 +457,6 @@ public class All64( @JvmField public var map_int64_sfixed64: Map = emptyMap() - @JvmField - public var oneof_int64: Long? = null - - @JvmField - public var oneof_sfixed64: Long? = null - /** * Prefixing so the generated code doesn't rename it weirdly. */ @@ -598,13 +598,13 @@ public class All64( pack_sint64 = pack_sint64, pack_fixed64 = pack_fixed64, pack_sfixed64 = pack_sfixed64, + oneof_int64 = oneof_int64, + oneof_sfixed64 = oneof_sfixed64, map_int64_int64 = map_int64_int64, map_int64_uint64 = map_int64_uint64, map_int64_sint64 = map_int64_sint64, map_int64_fixed64 = map_int64_fixed64, map_int64_sfixed64 = map_int64_sfixed64, - oneof_int64 = oneof_int64, - oneof_sfixed64 = oneof_sfixed64, unknownFields = buildUnknownFields() ) } @@ -654,13 +654,13 @@ public class All64( size += ProtoAdapter.SINT64.asPacked().encodedSizeWithTag(303, value.pack_sint64) size += ProtoAdapter.FIXED64.asPacked().encodedSizeWithTag(304, value.pack_fixed64) size += ProtoAdapter.SFIXED64.asPacked().encodedSizeWithTag(305, value.pack_sfixed64) + size += ProtoAdapter.INT64.encodedSizeWithTag(401, value.oneof_int64) + size += ProtoAdapter.SFIXED64.encodedSizeWithTag(402, value.oneof_sfixed64) size += map_int64_int64Adapter.encodedSizeWithTag(501, value.map_int64_int64) size += map_int64_uint64Adapter.encodedSizeWithTag(502, value.map_int64_uint64) size += map_int64_sint64Adapter.encodedSizeWithTag(503, value.map_int64_sint64) size += map_int64_fixed64Adapter.encodedSizeWithTag(504, value.map_int64_fixed64) size += map_int64_sfixed64Adapter.encodedSizeWithTag(505, value.map_int64_sfixed64) - size += ProtoAdapter.INT64.encodedSizeWithTag(401, value.oneof_int64) - size += ProtoAdapter.SFIXED64.encodedSizeWithTag(402, value.oneof_sfixed64) return size } @@ -707,13 +707,13 @@ public class All64( val pack_sint64 = mutableListOf() val pack_fixed64 = mutableListOf() val pack_sfixed64 = mutableListOf() + var oneof_int64: Long? = null + var oneof_sfixed64: Long? = null val map_int64_int64 = mutableMapOf() val map_int64_uint64 = mutableMapOf() val map_int64_sint64 = mutableMapOf() val map_int64_fixed64 = mutableMapOf() val map_int64_sfixed64 = mutableMapOf() - var oneof_int64: Long? = null - var oneof_sfixed64: Long? = null val unknownFields = reader.forEachTag { tag -> when (tag) { 1 -> my_int64 = ProtoAdapter.INT64.decode(reader) @@ -731,13 +731,13 @@ public class All64( 303 -> pack_sint64.add(ProtoAdapter.SINT64.decode(reader)) 304 -> pack_fixed64.add(ProtoAdapter.FIXED64.decode(reader)) 305 -> pack_sfixed64.add(ProtoAdapter.SFIXED64.decode(reader)) + 401 -> oneof_int64 = ProtoAdapter.INT64.decode(reader) + 402 -> oneof_sfixed64 = ProtoAdapter.SFIXED64.decode(reader) 501 -> map_int64_int64.putAll(map_int64_int64Adapter.decode(reader)) 502 -> map_int64_uint64.putAll(map_int64_uint64Adapter.decode(reader)) 503 -> map_int64_sint64.putAll(map_int64_sint64Adapter.decode(reader)) 504 -> map_int64_fixed64.putAll(map_int64_fixed64Adapter.decode(reader)) 505 -> map_int64_sfixed64.putAll(map_int64_sfixed64Adapter.decode(reader)) - 401 -> oneof_int64 = ProtoAdapter.INT64.decode(reader) - 402 -> oneof_sfixed64 = ProtoAdapter.SFIXED64.decode(reader) else -> reader.readUnknownField(tag) } } @@ -757,13 +757,13 @@ public class All64( pack_sint64 = pack_sint64, pack_fixed64 = pack_fixed64, pack_sfixed64 = pack_sfixed64, + oneof_int64 = oneof_int64, + oneof_sfixed64 = oneof_sfixed64, map_int64_int64 = map_int64_int64, map_int64_uint64 = map_int64_uint64, map_int64_sint64 = map_int64_sint64, map_int64_fixed64 = map_int64_fixed64, map_int64_sfixed64 = map_int64_sfixed64, - oneof_int64 = oneof_int64, - oneof_sfixed64 = oneof_sfixed64, unknownFields = unknownFields ) } diff --git a/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/AllStructs.kt b/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/AllStructs.kt index 2371acdbe7..104b5ddcc9 100644 --- a/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/AllStructs.kt +++ b/wire-library/wire-tests/src/jvmJsonKotlinTest/proto-kotlin/squareup/proto3/AllStructs.kt @@ -41,12 +41,12 @@ public class AllStructs( rep_list: List?> = emptyList(), rep_value_a: List = emptyList(), rep_null_value: List = emptyList(), + oneof_struct: Map? = null, + oneof_list: List<*>? = null, map_int32_struct: Map?> = emptyMap(), map_int32_list: Map?> = emptyMap(), map_int32_value_a: Map = emptyMap(), map_int32_null_value: Map = emptyMap(), - oneof_struct: Map? = null, - oneof_list: List<*>? = null, unknownFields: ByteString = ByteString.EMPTY ) : Message(ADAPTER, unknownFields) { @field:WireField( @@ -165,6 +165,24 @@ public class AllStructs( public val rep_null_value: List = immutableCopyOfStruct("rep_null_value", rep_null_value) + @field:WireField( + tag = 201, + adapter = "com.squareup.wire.ProtoAdapter#STRUCT_MAP", + jsonName = "oneofStruct", + oneofName = "choice" + ) + @JvmField + public val oneof_struct: Map? = immutableCopyOfStruct("oneof_struct", oneof_struct) + + @field:WireField( + tag = 202, + adapter = "com.squareup.wire.ProtoAdapter#STRUCT_LIST", + jsonName = "oneofList", + oneofName = "choice" + ) + @JvmField + public val oneof_list: List<*>? = immutableCopyOfStruct("oneof_list", oneof_list) + @field:WireField( tag = 301, keyAdapter = "com.squareup.wire.ProtoAdapter#INT32", @@ -205,24 +223,6 @@ public class AllStructs( public val map_int32_null_value: Map = immutableCopyOfMapWithStructValues("map_int32_null_value", map_int32_null_value) - @field:WireField( - tag = 201, - adapter = "com.squareup.wire.ProtoAdapter#STRUCT_MAP", - jsonName = "oneofStruct", - oneofName = "choice" - ) - @JvmField - public val oneof_struct: Map? = immutableCopyOfStruct("oneof_struct", oneof_struct) - - @field:WireField( - tag = 202, - adapter = "com.squareup.wire.ProtoAdapter#STRUCT_LIST", - jsonName = "oneofList", - oneofName = "choice" - ) - @JvmField - public val oneof_list: List<*>? = immutableCopyOfStruct("oneof_list", oneof_list) - init { require(countNonNull(oneof_struct, oneof_list) <= 1) { "At most one of oneof_struct, oneof_list may be non-null" @@ -244,12 +244,12 @@ public class AllStructs( builder.rep_list = rep_list builder.rep_value_a = rep_value_a builder.rep_null_value = rep_null_value + builder.oneof_struct = oneof_struct + builder.oneof_list = oneof_list builder.map_int32_struct = map_int32_struct builder.map_int32_list = map_int32_list builder.map_int32_value_a = map_int32_value_a builder.map_int32_null_value = map_int32_null_value - builder.oneof_struct = oneof_struct - builder.oneof_list = oneof_list builder.addUnknownFields(unknownFields) return builder } @@ -271,12 +271,12 @@ public class AllStructs( if (rep_list != other.rep_list) return false if (rep_value_a != other.rep_value_a) return false if (rep_null_value != other.rep_null_value) return false + if (oneof_struct != other.oneof_struct) return false + if (oneof_list != other.oneof_list) return false if (map_int32_struct != other.map_int32_struct) return false if (map_int32_list != other.map_int32_list) return false if (map_int32_value_a != other.map_int32_value_a) return false if (map_int32_null_value != other.map_int32_null_value) return false - if (oneof_struct != other.oneof_struct) return false - if (oneof_list != other.oneof_list) return false return true } @@ -297,12 +297,12 @@ public class AllStructs( result = result * 37 + rep_list.hashCode() result = result * 37 + rep_value_a.hashCode() result = result * 37 + rep_null_value.hashCode() + result = result * 37 + (oneof_struct?.hashCode() ?: 0) + result = result * 37 + (oneof_list?.hashCode() ?: 0) result = result * 37 + map_int32_struct.hashCode() result = result * 37 + map_int32_list.hashCode() result = result * 37 + map_int32_value_a.hashCode() result = result * 37 + map_int32_null_value.hashCode() - result = result * 37 + (oneof_struct?.hashCode() ?: 0) - result = result * 37 + (oneof_list?.hashCode() ?: 0) super.hashCode = result } return result @@ -323,13 +323,13 @@ public class AllStructs( if (rep_list.isNotEmpty()) result += """rep_list=$rep_list""" if (rep_value_a.isNotEmpty()) result += """rep_value_a=$rep_value_a""" if (rep_null_value.isNotEmpty()) result += """rep_null_value=$rep_null_value""" + if (oneof_struct != null) result += """oneof_struct=$oneof_struct""" + if (oneof_list != null) result += """oneof_list=$oneof_list""" if (map_int32_struct.isNotEmpty()) result += """map_int32_struct=$map_int32_struct""" if (map_int32_list.isNotEmpty()) result += """map_int32_list=$map_int32_list""" if (map_int32_value_a.isNotEmpty()) result += """map_int32_value_a=$map_int32_value_a""" if (map_int32_null_value.isNotEmpty()) result += """map_int32_null_value=$map_int32_null_value""" - if (oneof_struct != null) result += """oneof_struct=$oneof_struct""" - if (oneof_list != null) result += """oneof_list=$oneof_list""" return result.joinToString(prefix = "AllStructs{", separator = ", ", postfix = "}") } @@ -347,16 +347,16 @@ public class AllStructs( rep_list: List?> = this.rep_list, rep_value_a: List = this.rep_value_a, rep_null_value: List = this.rep_null_value, + oneof_struct: Map? = this.oneof_struct, + oneof_list: List<*>? = this.oneof_list, map_int32_struct: Map?> = this.map_int32_struct, map_int32_list: Map?> = this.map_int32_list, map_int32_value_a: Map = this.map_int32_value_a, map_int32_null_value: Map = this.map_int32_null_value, - oneof_struct: Map? = this.oneof_struct, - oneof_list: List<*>? = this.oneof_list, unknownFields: ByteString = this.unknownFields ): AllStructs = AllStructs(struct, list, null_value, value_a, value_b, value_c, value_d, value_e, - value_f, rep_struct, rep_list, rep_value_a, rep_null_value, map_int32_struct, map_int32_list, - map_int32_value_a, map_int32_null_value, oneof_struct, oneof_list, unknownFields) + value_f, rep_struct, rep_list, rep_value_a, rep_null_value, oneof_struct, oneof_list, + map_int32_struct, map_int32_list, map_int32_value_a, map_int32_null_value, unknownFields) public class Builder : Message.Builder() { @JvmField @@ -399,22 +399,22 @@ public class AllStructs( public var rep_null_value: List = emptyList() @JvmField - public var map_int32_struct: Map?> = emptyMap() + public var oneof_struct: Map? = null @JvmField - public var map_int32_list: Map?> = emptyMap() + public var oneof_list: List<*>? = null @JvmField - public var map_int32_value_a: Map = emptyMap() + public var map_int32_struct: Map?> = emptyMap() @JvmField - public var map_int32_null_value: Map = emptyMap() + public var map_int32_list: Map?> = emptyMap() @JvmField - public var oneof_struct: Map? = null + public var map_int32_value_a: Map = emptyMap() @JvmField - public var oneof_list: List<*>? = null + public var map_int32_null_value: Map = emptyMap() public fun struct(struct: Map?): Builder { this.struct = struct @@ -531,12 +531,12 @@ public class AllStructs( rep_list = rep_list, rep_value_a = rep_value_a, rep_null_value = rep_null_value, + oneof_struct = oneof_struct, + oneof_list = oneof_list, map_int32_struct = map_int32_struct, map_int32_list = map_int32_list, map_int32_value_a = map_int32_value_a, map_int32_null_value = map_int32_null_value, - oneof_struct = oneof_struct, - oneof_list = oneof_list, unknownFields = buildUnknownFields() ) } @@ -585,12 +585,12 @@ public class AllStructs( size += ProtoAdapter.STRUCT_LIST.asRepeated().encodedSizeWithTag(102, value.rep_list) size += ProtoAdapter.STRUCT_VALUE.asRepeated().encodedSizeWithTag(103, value.rep_value_a) size += ProtoAdapter.STRUCT_NULL.asRepeated().encodedSizeWithTag(104, value.rep_null_value) + size += ProtoAdapter.STRUCT_MAP.encodedSizeWithTag(201, value.oneof_struct) + size += ProtoAdapter.STRUCT_LIST.encodedSizeWithTag(202, value.oneof_list) size += map_int32_structAdapter.encodedSizeWithTag(301, value.map_int32_struct) size += map_int32_listAdapter.encodedSizeWithTag(302, value.map_int32_list) size += map_int32_value_aAdapter.encodedSizeWithTag(303, value.map_int32_value_a) size += map_int32_null_valueAdapter.encodedSizeWithTag(304, value.map_int32_null_value) - size += ProtoAdapter.STRUCT_MAP.encodedSizeWithTag(201, value.oneof_struct) - size += ProtoAdapter.STRUCT_LIST.encodedSizeWithTag(202, value.oneof_list) return size } @@ -632,12 +632,12 @@ public class AllStructs( val rep_list = mutableListOf?>() val rep_value_a = mutableListOf() val rep_null_value = mutableListOf() + var oneof_struct: Map? = null + var oneof_list: List<*>? = null val map_int32_struct = mutableMapOf?>() val map_int32_list = mutableMapOf?>() val map_int32_value_a = mutableMapOf() val map_int32_null_value = mutableMapOf() - var oneof_struct: Map? = null - var oneof_list: List<*>? = null val unknownFields = reader.forEachTag { tag -> when (tag) { 1 -> struct = ProtoAdapter.STRUCT_MAP.decode(reader) @@ -661,12 +661,12 @@ public class AllStructs( } catch (e: ProtoAdapter.EnumConstantNotFoundException) { reader.addUnknownField(tag, FieldEncoding.VARINT, e.value.toLong()) } + 201 -> oneof_struct = ProtoAdapter.STRUCT_MAP.decode(reader) + 202 -> oneof_list = ProtoAdapter.STRUCT_LIST.decode(reader) 301 -> map_int32_struct.putAll(map_int32_structAdapter.decode(reader)) 302 -> map_int32_list.putAll(map_int32_listAdapter.decode(reader)) 303 -> map_int32_value_a.putAll(map_int32_value_aAdapter.decode(reader)) 304 -> map_int32_null_value.putAll(map_int32_null_valueAdapter.decode(reader)) - 201 -> oneof_struct = ProtoAdapter.STRUCT_MAP.decode(reader) - 202 -> oneof_list = ProtoAdapter.STRUCT_LIST.decode(reader) else -> reader.readUnknownField(tag) } } @@ -684,12 +684,12 @@ public class AllStructs( rep_list = rep_list, rep_value_a = rep_value_a, rep_null_value = rep_null_value, + oneof_struct = oneof_struct, + oneof_list = oneof_list, map_int32_struct = map_int32_struct, map_int32_list = map_int32_list, map_int32_value_a = map_int32_value_a, map_int32_null_value = map_int32_null_value, - oneof_struct = oneof_struct, - oneof_list = oneof_list, unknownFields = unknownFields ) } @@ -706,11 +706,11 @@ public class AllStructs( rep_struct = value.rep_struct.redactElements(ProtoAdapter.STRUCT_MAP), rep_list = value.rep_list.redactElements(ProtoAdapter.STRUCT_LIST), rep_value_a = value.rep_value_a.redactElements(ProtoAdapter.STRUCT_VALUE), + oneof_struct = value.oneof_struct?.let(ProtoAdapter.STRUCT_MAP::redact), + oneof_list = value.oneof_list?.let(ProtoAdapter.STRUCT_LIST::redact), map_int32_struct = value.map_int32_struct.redactElements(ProtoAdapter.STRUCT_MAP), map_int32_list = value.map_int32_list.redactElements(ProtoAdapter.STRUCT_LIST), map_int32_value_a = value.map_int32_value_a.redactElements(ProtoAdapter.STRUCT_VALUE), - oneof_struct = value.oneof_struct?.let(ProtoAdapter.STRUCT_MAP::redact), - oneof_list = value.oneof_list?.let(ProtoAdapter.STRUCT_LIST::redact), unknownFields = ByteString.EMPTY ) }