From 93d9321d0a0028a84ad197f57f80ae04514e5762 Mon Sep 17 00:00:00 2001 From: panpanini Date: Tue, 11 Feb 2020 15:48:53 +1300 Subject: [PATCH 01/10] Add toJson function to Message, Enum interfaces --- runtime/src/main/kotlin/Message.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime/src/main/kotlin/Message.kt b/runtime/src/main/kotlin/Message.kt index a30a874..328e9b5 100644 --- a/runtime/src/main/kotlin/Message.kt +++ b/runtime/src/main/kotlin/Message.kt @@ -14,6 +14,8 @@ interface Message> : Serializable { fun protoMarshal(m: Marshaller) fun protoMarshal(): ByteArray = Marshaller.allocate(protoSize).also(::protoMarshal).complete()!! + fun toJson(): String + interface Companion> { fun protoUnmarshal(u: Unmarshaller): T fun protoUnmarshal(arr: ByteArray) = protoUnmarshal(Unmarshaller.fromByteArray(arr)) @@ -21,6 +23,7 @@ interface Message> : Serializable { interface Enum : Serializable { val value: Int + fun toJson(): String interface Companion { fun fromValue(value: Int): T From ab009da63147b4ca444e142ba462af1961555408 Mon Sep 17 00:00:00 2001 From: panpanini Date: Tue, 11 Feb 2020 15:50:58 +1300 Subject: [PATCH 02/10] support JSON generation for Messages, Lists --- .../jp/co/panpanini/MessageGenerator.kt | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt b/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt index 0a8387b..ed75d0b 100644 --- a/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt +++ b/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt @@ -57,10 +57,13 @@ class MessageGenerator(private val file: File, private val kotlinTypeMappings: M typeSpec.primaryConstructor(constructor.build()) typeSpec.addFunction(createSecondaryConstructor(type)) + typeSpec.addFunction(createToJsonFunction(type)) + mapEntry?.let { typeSpec.addSuperinterface(mapEntry) } + type.nestedTypes.map { it.toTypeSpec(file, kotlinTypeMappings) }.forEach { @@ -517,6 +520,71 @@ class MessageGenerator(private val file: File, private val kotlinTypeMappings: M return typeSpec.build() } + private fun createToJsonFunction(type: File.Type.Message): FunSpec { + val builder = StringBuilder() + .append("return ") + .append("\"\"\"\n{ ") + .append( + type.fields.joinToString(", ") { field -> + "\n\"${field.name}\" : ${getJsonValue(field)}" + } + ) + .append("\n}\n\"\"\".trimIndent()") + + return FunSpec.builder("toJson") + .addModifiers(KModifier.OVERRIDE) + .addCode(builder.toString()) + .build() + } + + private fun getJsonValue(field: File.Field): String { + return when (field) { + is File.Field.Standard -> { + when { + field.map -> { + getMapJsonValue(field) + } + field.repeated -> { + getListJsonValue(field) + } + else -> { + "\"\${${getStandardJsonValue(field, field.kotlinFieldName)}}\"" + + } + } + } + is File.Field.OneOf -> { + TODO() + } + } + } + + private fun getStandardJsonValue(field: File.Field.Standard, fieldName: String): String { + return when (field.type) { + File.Field.Type.BYTES -> TODO() + File.Field.Type.ENUM -> "$fieldName.toJson()" + File.Field.Type.MESSAGE -> "$fieldName.toJson()" + File.Field.Type.STRING -> field.kotlinFieldName + else -> "$fieldName.toString()" + } + } + + private fun getMapJsonValue(field: File.Field.Standard): String { + return "TODO(\"implement map json\")" + } + + private fun getListJsonValue(field: File.Field.Standard): String { + val builder = StringBuilder() + builder.append("[ ") + builder.append( + "\${ ${field.kotlinFieldName}.joinToString(\", \") { ${getStandardJsonValue(field, "it")} }" + ) + builder.append(" }") + + builder.append(" ]") + return builder.toString() + } + private fun File.Field.Standard.sizeExpression(): CodeBlock { val sizer = Sizer::class val codeBlock = CodeBlock.builder() From 71232695525e40581fed6ac29e066f866972a1f3 Mon Sep 17 00:00:00 2001 From: panpanini Date: Tue, 11 Feb 2020 17:53:11 +1300 Subject: [PATCH 03/10] Implement map support for JSON serialization --- .../kotlin/jp/co/panpanini/MessageGenerator.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt b/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt index ed75d0b..a41930e 100644 --- a/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt +++ b/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt @@ -526,14 +526,14 @@ class MessageGenerator(private val file: File, private val kotlinTypeMappings: M .append("\"\"\"\n{ ") .append( type.fields.joinToString(", ") { field -> - "\n\"${field.name}\" : ${getJsonValue(field)}" + "\n\"${field.name}\"·:·${getJsonValue(field)}" } ) .append("\n}\n\"\"\".trimIndent()") return FunSpec.builder("toJson") .addModifiers(KModifier.OVERRIDE) - .addCode(builder.toString()) + .addCode(CodeBlock.of(builder.toString())) .build() } @@ -570,7 +570,18 @@ class MessageGenerator(private val file: File, private val kotlinTypeMappings: M } private fun getMapJsonValue(field: File.Field.Standard): String { - return "TODO(\"implement map json\")" + // get the value type so we can call the correct function to get the correct json representation + val value = field.mapEntry()?.fields?.get(1) as? File.Field.Standard + ?: throw IllegalStateException("map value must not be null") + + val builder = StringBuilder() + .append("{·") + .append("\${${field.kotlinFieldName}.entries.joinToString(\",·\")·{·(k,·v)·->\"\$k·:·\${${getStandardJsonValue(value, "v")}}\"·}·}") + .append("·}") + + return builder.toString() + + } private fun getListJsonValue(field: File.Field.Standard): String { From b6399bf8c4b5159a5a6a81be27f406dbaa72cd3b Mon Sep 17 00:00:00 2001 From: panpanini Date: Tue, 11 Feb 2020 17:53:52 +1300 Subject: [PATCH 04/10] Add toJson support to Enums --- .../main/kotlin/jp/co/panpanini/EnumGenerator.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/src/main/kotlin/jp/co/panpanini/EnumGenerator.kt b/library/src/main/kotlin/jp/co/panpanini/EnumGenerator.kt index 21dd67d..58b98e6 100644 --- a/library/src/main/kotlin/jp/co/panpanini/EnumGenerator.kt +++ b/library/src/main/kotlin/jp/co/panpanini/EnumGenerator.kt @@ -34,6 +34,7 @@ class EnumGenerator { typeSpec.addProperty(PropertySpec.builder("value", Int::class).initializer("value").build()) typeSpec.addType(companion.build()) typeSpec.addFunction(createToStringFunction()) + typeSpec.addFunction(createToJsonFunction(type.values)) return typeSpec.build() } @@ -79,4 +80,19 @@ class EnumGenerator { .addCode("return name") .build() } + + private fun createToJsonFunction(values: List): FunSpec { + val whenBlock = CodeBlock.builder() + .beginControlFlow("return when(this)") + values.forEach { + whenBlock.addStatement("%L -> %S", it.kotlinValueName, it.name) + } + whenBlock.addStatement("else -> %S", values.first().name) + whenBlock.endControlFlow() + return FunSpec.builder("toJson") + .addModifiers(KModifier.OVERRIDE) + .returns(String::class) + .addCode(whenBlock.build()) + .build() + } } \ No newline at end of file From 544e98b6be7be755e173f3ee986620d977732110 Mon Sep 17 00:00:00 2001 From: panpanini Date: Tue, 11 Feb 2020 17:54:41 +1300 Subject: [PATCH 05/10] Update map.proto to include different value types and list --- library/src/test/resources/input/mappy.input | Bin 507 -> 815 bytes library/src/test/resources/proto/map.proto | 2 ++ 2 files changed, 2 insertions(+) diff --git a/library/src/test/resources/input/mappy.input b/library/src/test/resources/input/mappy.input index 99693d1dd39ced50c7787893a19b0c39fcb1fbf5..21ebd3daaae3b0bf6fddf145d6c241ef0c64fd43 100644 GIT binary patch literal 815 zcmZvb-EPxB5QX<=cJ0jC;0~y<0tnXkR@`iuwXUL^J{#D>X>zi}V@# z1bsbb*XzcrxSQiM=iAYdjFaEXwrRJUZZk_$U6@kV;`Yws{yXVqyRIJ;ny<^Y-O)2t z>+hwku$SS#4S2Sx9P4{+ukd62)!DG zF7|Gp9PiT{C*ip->zl5G5XKM=yv5#}oueDQPX+3qt6eo5~K|f=4iYC0t4_IvtAl+;-ENZTs|nxsyK)gwk*_t) ztJ61wVv4PfB83l5;3QHMQxdRs+*C|S9w~|`$@I}v0{2w9DK9k H7y0lHYZiA& delta 160 zcmZ3__M3TvnCN3>F3#M<0=BgYT+A$tj6%$u9K2lITr4b%ObkNITr5Idyj-jx m4vP>A7Yipp7Y~HX3XMFo_N96k`B~ things = 2; + map otherThings = 3; + repeated Thing thingList = 4; } message Thing { From 5fb03a4ebc33119def05fd650fcf2a005cd71491 Mon Sep 17 00:00:00 2001 From: panpanini Date: Tue, 11 Feb 2020 18:04:48 +1300 Subject: [PATCH 06/10] Update test files for Enum test --- library/src/test/resources/kotlin/Language.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/library/src/test/resources/kotlin/Language.kt b/library/src/test/resources/kotlin/Language.kt index b5a34bd..26628a2 100644 --- a/library/src/test/resources/kotlin/Language.kt +++ b/library/src/test/resources/kotlin/Language.kt @@ -20,6 +20,15 @@ enum class Language(override val value: Int) : Serializable, Message.Enum { GO(4); override fun toString(): String = name + override fun toJson(): String = when(this) { + PROTOBUF -> "PROTOBUF" + KOTLIN -> "KOTLIN" + JAVA -> "JAVA" + SWIFT -> "SWIFT" + GO -> "GO" + else -> "PROTOBUF" + } + companion object : Message.Enum.Companion { @JvmStatic override fun fromValue(value: Int): Language = when(value) { From d9345259c093bbe01f6a63eb26a5d45e65e5a1d1 Mon Sep 17 00:00:00 2001 From: panpanini Date: Tue, 11 Feb 2020 18:05:02 +1300 Subject: [PATCH 07/10] Update test files for Message, Map, List tests --- library/src/test/resources/kotlin/Mappy.kt | 161 ++++++++++++++++++++- library/src/test/resources/kotlin/Thing.kt | 5 + 2 files changed, 162 insertions(+), 4 deletions(-) diff --git a/library/src/test/resources/kotlin/Mappy.kt b/library/src/test/resources/kotlin/Mappy.kt index cc696d2..2ec7811 100644 --- a/library/src/test/resources/kotlin/Mappy.kt +++ b/library/src/test/resources/kotlin/Mappy.kt @@ -10,6 +10,7 @@ import jp.co.panpanini.Unmarshaller import kotlin.ByteArray import kotlin.Int import kotlin.String +import kotlin.collections.List import kotlin.collections.Map import kotlin.jvm.JvmField import kotlin.jvm.JvmStatic @@ -17,13 +18,29 @@ import kotlin.jvm.JvmStatic data class Mappy( @JvmField val id: String = "", @JvmField val things: Map = emptyMap(), + @JvmField val otherThings: Map = emptyMap(), + @JvmField val thingList: List = emptyList(), val unknownFields: Map = emptyMap() ) : Message, Serializable { override val protoSize: Int = protoSizeImpl() - constructor(id: String, things: Map) : this(id, things, emptyMap()) - + constructor( + id: String, + things: Map, + otherThings: Map, + thingList: List + ) : this(id, things, otherThings, thingList, emptyMap()) + + override fun toJson() = """ + { + "id" : "${id}", + "things" : { ${things.entries.joinToString(", ") { (k, v) ->"$k : ${v.toJson()}" } } }, + "otherThings" : { ${otherThings.entries.joinToString(", ") { (k, v) ->"$k : ${v.toString()}" } } }, + + "thingList" : [ ${ thingList.joinToString(", ") { it.toJson() } } ] + } + """.trimIndent() fun Mappy.protoSizeImpl(): Int { var protoSize = 0 if (id != DEFAULT_ID) { @@ -32,6 +49,13 @@ data class Mappy( if (things.isNotEmpty()) { protoSize += jp.co.panpanini.Sizer.mapSize(2, things, api.Mappy::ThingsEntry) } + if (otherThings.isNotEmpty()) { + protoSize += jp.co.panpanini.Sizer.mapSize(3, otherThings, api.Mappy::OtherThingsEntry) + } + if (thingList.isNotEmpty()) { + protoSize += jp.co.panpanini.Sizer.tagSize(4) * thingList.size + + thingList.sumBy(jp.co.panpanini.Sizer::messageSize) + } protoSize += unknownFields.entries.sumBy { it.value.size() } return protoSize } @@ -44,6 +68,14 @@ data class Mappy( if (things.isNotEmpty()) { protoMarshal.writeMap(18, things, api.Mappy::ThingsEntry) + } + if (otherThings.isNotEmpty()) { + protoMarshal.writeMap(26, otherThings, api.Mappy::OtherThingsEntry) + + } + if (thingList.isNotEmpty()) { + thingList.forEach { protoMarshal.writeTag(34).writeMessage(it) } + } if (unknownFields.isNotEmpty()) { protoMarshal.writeUnknownFields(unknownFields) @@ -52,6 +84,8 @@ data class Mappy( fun Mappy.protoMergeImpl(other: Mappy?): Mappy = other?.copy( things = things + other.things, + otherThings = otherThings + other.otherThings, + thingList = thingList + other.thingList, unknownFields = unknownFields + other.unknownFields ) ?: this @@ -67,6 +101,8 @@ data class Mappy( fun newBuilder(): Builder = Builder() .id(id) .things(things) + .otherThings(otherThings) + .thingList(thingList) .unknownFields(unknownFields) data class ThingsEntry( @@ -79,6 +115,12 @@ data class Mappy( constructor(key: String, value: api.Thing) : this(key, value, emptyMap()) + override fun toJson() = """ + { + "key" : "${key}", + "value" : "${value.toJson()}" + } + """.trimIndent() fun ThingsEntry.protoSizeImpl(): Int { var protoSize = 0 if (key != DEFAULT_KEY) { @@ -146,6 +188,90 @@ data class Mappy( } } + data class OtherThingsEntry( + override val key: String, + override val value: Int, + val unknownFields: Map = emptyMap() + ) : Message, Serializable, Map.Entry { + override val protoSize: Int = protoSizeImpl() + + + constructor(key: String, value: Int) : this(key, value, emptyMap()) + + override fun toJson() = """ + { + "key" : "${key}", + "value" : "${value.toString()}" + } + """.trimIndent() + fun OtherThingsEntry.protoSizeImpl(): Int { + var protoSize = 0 + if (key != DEFAULT_KEY) { + protoSize += jp.co.panpanini.Sizer.tagSize(1) + + jp.co.panpanini.Sizer.stringSize(key) + } + if (value != DEFAULT_VALUE) { + protoSize += jp.co.panpanini.Sizer.tagSize(2) + + jp.co.panpanini.Sizer.int32Size(value) + } + protoSize += unknownFields.entries.sumBy { it.value.size() } + return protoSize + } + + fun OtherThingsEntry.protoMarshalImpl(protoMarshal: Marshaller) { + if (key != DEFAULT_KEY) { + protoMarshal.writeTag(10).writeString(key) + + } + if (value != DEFAULT_VALUE) { + protoMarshal.writeTag(16).writeInt32(value) + + } + if (unknownFields.isNotEmpty()) { + protoMarshal.writeUnknownFields(unknownFields) + } + } + + fun OtherThingsEntry.protoMergeImpl(other: OtherThingsEntry?): OtherThingsEntry = + other?.copy( + unknownFields = unknownFields + other.unknownFields + ) ?: this + + override fun protoMarshal(marshaller: Marshaller) = protoMarshalImpl(marshaller) + + override operator fun plus(other: OtherThingsEntry?): OtherThingsEntry = + protoMergeImpl(other) + + fun encode(): ByteArray = protoMarshal() + + override fun protoUnmarshal(protoUnmarshal: Unmarshaller): OtherThingsEntry = + Companion.protoUnmarshal(protoUnmarshal) + + companion object : Message.Companion { + @JvmField + val DEFAULT_KEY: String = "" + + @JvmField + val DEFAULT_VALUE: Int = 0 + + override fun protoUnmarshal(protoUnmarshal: Unmarshaller): OtherThingsEntry { + var key = "" + var value = 0 + while (true) { + when (protoUnmarshal.readTag()) { + 0 -> return OtherThingsEntry(key, value, protoUnmarshal.unknownFields()) + 10 -> key = protoUnmarshal.readString() + 16 -> value = protoUnmarshal.readInt32() + else -> protoUnmarshal.unknownField() + } + } + } + + @JvmStatic + fun decode(arr: ByteArray): OtherThingsEntry = protoUnmarshal(arr) + } + } + companion object : Message.Companion { @JvmField val DEFAULT_ID: String = "" @@ -153,15 +279,28 @@ data class Mappy( @JvmField val DEFAULT_THINGS: Map = emptyMap() + @JvmField + val DEFAULT_OTHER_THINGS: Map = emptyMap() + + @JvmField + val DEFAULT_THING_LIST: List = emptyList() + override fun protoUnmarshal(protoUnmarshal: Unmarshaller): Mappy { var id = "" var things: Map = emptyMap() + var otherThings: Map = emptyMap() + var thingList: List = emptyList() while (true) { when (protoUnmarshal.readTag()) { - 0 -> return Mappy(id, HashMap(things), protoUnmarshal.unknownFields()) + 0 -> return Mappy(id, HashMap(things), HashMap(otherThings), + thingList, protoUnmarshal.unknownFields()) 10 -> id = protoUnmarshal.readString() 18 -> things = protoUnmarshal.readMap(things, api.Mappy.ThingsEntry.Companion, true) + 26 -> otherThings = protoUnmarshal.readMap(otherThings, + api.Mappy.OtherThingsEntry.Companion, true) + 34 -> thingList = protoUnmarshal.readRepeatedMessage(thingList, + api.Thing.Companion, true) else -> protoUnmarshal.unknownField() } } @@ -176,6 +315,10 @@ data class Mappy( var things: Map = DEFAULT_THINGS + var otherThings: Map = DEFAULT_OTHER_THINGS + + var thingList: List = DEFAULT_THING_LIST + var unknownFields: Map = emptyMap() fun id(id: String?): Builder { @@ -188,11 +331,21 @@ data class Mappy( return this } + fun otherThings(otherThings: Map?): Builder { + this.otherThings = otherThings ?: DEFAULT_OTHER_THINGS + return this + } + + fun thingList(thingList: List?): Builder { + this.thingList = thingList ?: DEFAULT_THING_LIST + return this + } + fun unknownFields(unknownFields: Map): Builder { this.unknownFields = unknownFields return this } - fun build(): Mappy = Mappy(id, things, unknownFields) + fun build(): Mappy = Mappy(id, things, otherThings, thingList, unknownFields) } } diff --git a/library/src/test/resources/kotlin/Thing.kt b/library/src/test/resources/kotlin/Thing.kt index d678a48..714a992 100644 --- a/library/src/test/resources/kotlin/Thing.kt +++ b/library/src/test/resources/kotlin/Thing.kt @@ -21,6 +21,11 @@ data class Thing(@JvmField val id: String = "", val unknownFields: Map Date: Wed, 19 Feb 2020 11:34:15 +1300 Subject: [PATCH 08/10] Support JSON encoding for OneOf messages, ByteArr types --- .../jp/co/panpanini/MessageGenerator.kt | 28 +++++++++++++++---- runtime/src/main/kotlin/ByteArr.kt | 10 +++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt b/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt index a41930e..9a3788a 100644 --- a/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt +++ b/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt @@ -537,7 +537,7 @@ class MessageGenerator(private val file: File, private val kotlinTypeMappings: M .build() } - private fun getJsonValue(field: File.Field): String { + private fun getJsonValue(field: File.Field, prefix: String = ""): String { return when (field) { is File.Field.Standard -> { when { @@ -548,23 +548,41 @@ class MessageGenerator(private val file: File, private val kotlinTypeMappings: M getListJsonValue(field) } else -> { - "\"\${${getStandardJsonValue(field, field.kotlinFieldName)}}\"" + "\"\${${getStandardJsonValue(field, "$prefix${field.kotlinFieldName}")}}\"" } } } is File.Field.OneOf -> { - TODO() + val block = CodeBlock.builder() + block.add("\${ ") + block.beginControlFlow("when (${field.kotlinFieldName})") + field.fields.forEach { + val code = CodeBlock.builder() + .beginControlFlow("is ${field.kotlinTypeName}.${it.kotlinFieldName.beginWithUpperCase()} ->") + .addStatement( + getJsonValue(it, "${field.kotlinFieldName}.") + ) + .endControlFlow() + block.add(code.build()) + } + block.beginControlFlow("is ${field.kotlinTypeName}.NotSet ->") + .addStatement("null") + .endControlFlow() + + block.endControlFlow() + block.addStatement("}") + block.build().toString() } } } private fun getStandardJsonValue(field: File.Field.Standard, fieldName: String): String { return when (field.type) { - File.Field.Type.BYTES -> TODO() + File.Field.Type.BYTES -> "$fieldName.base64Encode()" File.Field.Type.ENUM -> "$fieldName.toJson()" File.Field.Type.MESSAGE -> "$fieldName.toJson()" - File.Field.Type.STRING -> field.kotlinFieldName + File.Field.Type.STRING -> fieldName else -> "$fieldName.toString()" } } diff --git a/runtime/src/main/kotlin/ByteArr.kt b/runtime/src/main/kotlin/ByteArr.kt index 5783620..6d15e51 100644 --- a/runtime/src/main/kotlin/ByteArr.kt +++ b/runtime/src/main/kotlin/ByteArr.kt @@ -1,9 +1,19 @@ package jp.co.panpanini import java.io.Serializable +import java.nio.charset.StandardCharsets +import java.util.Base64 class ByteArr(val array: ByteArray = ByteArray(0)) : Serializable { override fun equals(other: Any?) = other is ByteArr && array.contentEquals(other.array) override fun hashCode() = array.contentHashCode() override fun toString() = array.contentToString() + fun base64Encode(): String { + return String(Base64.getUrlEncoder().encode(array), StandardCharsets.UTF_8) + } + companion object { + fun base64Decode(base64String: String): ByteArr { + return ByteArr(Base64.getUrlDecoder().decode(base64String)) + } + } } \ No newline at end of file From 979e8c21657ee3a5cf1ad66a951561616bf20236 Mon Sep 17 00:00:00 2001 From: panpanini Date: Wed, 19 Feb 2020 11:49:49 +1300 Subject: [PATCH 09/10] Update tests for OneOf types --- library/src/test/resources/kotlin/Item.kt | 5 +++ .../src/test/resources/kotlin/OneOfTest.kt | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/library/src/test/resources/kotlin/Item.kt b/library/src/test/resources/kotlin/Item.kt index fcde792..f2b9dcc 100644 --- a/library/src/test/resources/kotlin/Item.kt +++ b/library/src/test/resources/kotlin/Item.kt @@ -21,6 +21,11 @@ data class Item(@JvmField val id: String = "", val unknownFields: Map { + "${oneofField.oneofUint32.toString()}" + } + is OneofField.OneofString -> { + "${oneofField.oneofString}" + } + is OneofField.OneofBytes -> { + "${oneofField.oneofBytes.base64Encode()}" + } + is OneofField.OneofBool -> { + "${oneofField.oneofBool.toString()}" + } + is OneofField.OneofUint64 -> { + "${oneofField.oneofUint64.toString()}" + } + is OneofField.OneofFloat -> { + "${oneofField.oneofFloat.toString()}" + } + is OneofField.OneofDouble -> { + "${oneofField.oneofDouble.toString()}" + } + is OneofField.OneofItem -> { + "${oneofField.oneofItem.toJson()}" + } + is OneofField.NotSet -> { + null + } + } + } + + } + """.trimIndent() fun OneOfTest.protoSizeImpl(): Int { var protoSize = 0 if (oneofField !is OneofField.NotSet) { From e29f07c67a6d749209bade717d345d08ac0b8495 Mon Sep 17 00:00:00 2001 From: panpanini Date: Wed, 19 Feb 2020 12:03:59 +1300 Subject: [PATCH 10/10] Add comment regarding INT64 handling in JSON --- library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt b/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt index 9a3788a..7e7f18e 100644 --- a/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt +++ b/library/src/main/kotlin/jp/co/panpanini/MessageGenerator.kt @@ -583,6 +583,7 @@ class MessageGenerator(private val file: File, private val kotlinTypeMappings: M File.Field.Type.ENUM -> "$fieldName.toJson()" File.Field.Type.MESSAGE -> "$fieldName.toJson()" File.Field.Type.STRING -> fieldName + // TODO: INT64 -> return String value else -> "$fieldName.toString()" } }