Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ dependencies {
testImplementation(libs.kotlin.test)
}

tasks.jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveClassifier = "all"

// Protoc plugins are all fat jars basically (the ones built on jvm)
// be really careful of what you put in the classpath here
from(
configurations.runtimeClasspath.map { prop ->
prop.map { if (it.isDirectory()) it else zipTree(it) }
}
)
if (project.name != "common") {
tasks.jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveClassifier = "all"

// Protoc plugins are all fat jars basically (the ones built on jvm)
// be really careful of what you put in the classpath here
from(
configurations.runtimeClasspath.map { prop ->
prop.map { if (it.isDirectory()) it else zipTree(it) }
}
)
}
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.gradle.kotlin.dsl.provideDelegate
import java.util.Properties
import java.util.concurrent.atomic.AtomicReference
import kotlin.io.path.Path
import kotlin.io.path.exists
import kotlin.io.path.inputStream

private val ref = AtomicReference<Properties>()
Expand All @@ -19,7 +20,13 @@ fun Project.localProperties(): Properties {
ref.compareAndSet(null, Properties().apply {
val globalRootDir: String by extra

load(Path(globalRootDir, "local.properties").inputStream())
val filepath = Path(globalRootDir, "local.properties")

if (!filepath.exists()) {
return@apply
}

load(filepath.inputStream())
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public fun com.google.protobuf.kotlin.ApiInternal.encodeWith(encoder: kotlinx.rp
}
}

if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
encoder.writeEnum(fieldNr = 7, value = syntax.number)
}
}
Expand Down Expand Up @@ -257,11 +257,11 @@ private fun com.google.protobuf.kotlin.ApiInternal.computeSize(): Int {
}

if (methods.isNotEmpty()) {
__result = methods.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += methods.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (options.isNotEmpty()) {
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (version.isNotEmpty()) {
Expand All @@ -273,10 +273,10 @@ private fun com.google.protobuf.kotlin.ApiInternal.computeSize(): Int {
}

if (mixins.isNotEmpty()) {
__result = mixins.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += mixins.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(7, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(syntax.number))
}

Expand Down Expand Up @@ -324,7 +324,7 @@ public fun com.google.protobuf.kotlin.MethodInternal.encodeWith(encoder: kotlinx
}
}

if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
encoder.writeEnum(fieldNr = 7, value = syntax.number)
}
}
Expand Down Expand Up @@ -395,10 +395,10 @@ private fun com.google.protobuf.kotlin.MethodInternal.computeSize(): Int {
}

if (options.isNotEmpty()) {
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(7, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(syntax.number))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public fun com.google.protobuf.kotlin.FieldMaskInternal.Companion.decodeWith(msg
private fun com.google.protobuf.kotlin.FieldMaskInternal.computeSize(): Int {
var __result = 0
if (paths.isNotEmpty()) {
__result = paths.sumOf { kotlinx.rpc.protobuf.internal.WireSize.string(it) + kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += paths.sumOf { kotlinx.rpc.protobuf.internal.WireSize.string(it) + kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

return __result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public fun com.google.protobuf.kotlin.ListValueInternal.Companion.decodeWith(msg
private fun com.google.protobuf.kotlin.ListValueInternal.computeSize(): Int {
var __result = 0
if (values.isNotEmpty()) {
__result = values.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += values.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

return __result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public fun com.google.protobuf.kotlin.TypeInternal.encodeWith(encoder: kotlinx.r
encoder.writeMessage(fieldNr = 5, value = sourceContext.asInternal()) { encodeWith(it) }
}

if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
encoder.writeEnum(fieldNr = 6, value = syntax.number)
}

Expand Down Expand Up @@ -350,22 +350,22 @@ private fun com.google.protobuf.kotlin.TypeInternal.computeSize(): Int {
}

if (fields.isNotEmpty()) {
__result = fields.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += fields.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (oneofs.isNotEmpty()) {
__result = oneofs.sumOf { kotlinx.rpc.protobuf.internal.WireSize.string(it) + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += oneofs.sumOf { kotlinx.rpc.protobuf.internal.WireSize.string(it) + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (options.isNotEmpty()) {
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(4, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(4, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (presenceMask[0]) {
__result += sourceContext.asInternal()._size.let { kotlinx.rpc.protobuf.internal.WireSize.tag(5, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) + kotlinx.rpc.protobuf.internal.WireSize.int32(it) + it }
}

if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(syntax.number))
}

Expand All @@ -391,11 +391,11 @@ public fun com.google.protobuf.kotlin.FieldInternal.checkRequiredFields() {

@kotlinx.rpc.internal.utils.InternalRpcApi
public fun com.google.protobuf.kotlin.FieldInternal.encodeWith(encoder: kotlinx.rpc.protobuf.internal.WireEncoder) {
if (com.google.protobuf.kotlin.Field.Kind.TYPE_UNKNOWN != kind) {
if (kind != com.google.protobuf.kotlin.Field.Kind.TYPE_UNKNOWN) {
encoder.writeEnum(fieldNr = 1, value = kind.number)
}

if (com.google.protobuf.kotlin.Field.Cardinality.CARDINALITY_UNKNOWN != cardinality) {
if (cardinality != com.google.protobuf.kotlin.Field.Cardinality.CARDINALITY_UNKNOWN) {
encoder.writeEnum(fieldNr = 2, value = cardinality.number)
}

Expand Down Expand Up @@ -491,11 +491,11 @@ public fun com.google.protobuf.kotlin.FieldInternal.Companion.decodeWith(msg: co

private fun com.google.protobuf.kotlin.FieldInternal.computeSize(): Int {
var __result = 0
if (com.google.protobuf.kotlin.Field.Kind.TYPE_UNKNOWN != kind) {
if (kind != com.google.protobuf.kotlin.Field.Kind.TYPE_UNKNOWN) {
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(kind.number))
}

if (com.google.protobuf.kotlin.Field.Cardinality.CARDINALITY_UNKNOWN != cardinality) {
if (cardinality != com.google.protobuf.kotlin.Field.Cardinality.CARDINALITY_UNKNOWN) {
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(cardinality.number))
}

Expand All @@ -520,7 +520,7 @@ private fun com.google.protobuf.kotlin.FieldInternal.computeSize(): Int {
}

if (options.isNotEmpty()) {
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(9, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(9, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (jsonName.isNotEmpty()) {
Expand Down Expand Up @@ -577,7 +577,7 @@ public fun com.google.protobuf.kotlin.EnumInternal.encodeWith(encoder: kotlinx.r
encoder.writeMessage(fieldNr = 4, value = sourceContext.asInternal()) { encodeWith(it) }
}

if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
encoder.writeEnum(fieldNr = 5, value = syntax.number)
}

Expand Down Expand Up @@ -638,18 +638,18 @@ private fun com.google.protobuf.kotlin.EnumInternal.computeSize(): Int {
}

if (enumvalue.isNotEmpty()) {
__result = enumvalue.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += enumvalue.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (options.isNotEmpty()) {
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

if (presenceMask[0]) {
__result += sourceContext.asInternal()._size.let { kotlinx.rpc.protobuf.internal.WireSize.tag(4, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) + kotlinx.rpc.protobuf.internal.WireSize.int32(it) + it }
}

if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(5, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(syntax.number))
}

Expand Down Expand Up @@ -728,7 +728,7 @@ private fun com.google.protobuf.kotlin.EnumValueInternal.computeSize(): Int {
}

if (options.isNotEmpty()) {
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
}

return __result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public interface WireDecoder : AutoCloseable {
if (len < 0) throw ProtobufDecodingException.negativeSize()
val limit = pushLimit(len)
decoder(msg, this)
if (bytesUntilLimit() != 0) {
throw ProtobufDecodingException.truncatedMessage()
}

popLimit(limit)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ public expect fun WireSize.sInt32(value: Int): Int
public expect fun WireSize.sInt64(value: Long): Int

@InternalRpcApi
public fun WireSize.float(value: Float): Int = 32
public fun WireSize.float(value: Float): Int = 4

@InternalRpcApi
public fun WireSize.double(value: Double): Int = 64
public fun WireSize.double(value: Double): Int = 8

@InternalRpcApi
public fun WireSize.fixed32(value: UInt): Int = 32
public fun WireSize.fixed32(value: UInt): Int = 4

@InternalRpcApi
public fun WireSize.fixed64(value: ULong): Int = 64
public fun WireSize.fixed64(value: ULong): Int = 8

@InternalRpcApi
public fun WireSize.sFixed32(value: Int): Int = 32
public fun WireSize.sFixed32(value: Int): Int = 4

@InternalRpcApi
public fun WireSize.sFixed64(value: Long): Int = 64
public fun WireSize.sFixed64(value: Long): Int = 8

@InternalRpcApi
public fun WireSize.bool(value: Boolean): Int = int32(if (value) 1 else 0)
Expand Down Expand Up @@ -86,22 +86,22 @@ public fun WireSize.packedSInt64(value: List<Long>): Int = value.sumOf { sInt64(
public fun WireSize.packedEnum(value: List<Int>): Int = value.sumOf { enum(it) }

@InternalRpcApi
public fun WireSize.packedFloat(value: List<Float>): Int = value.size * 32
public fun WireSize.packedFloat(value: List<Float>): Int = value.size * 4

@InternalRpcApi
public fun WireSize.packedDouble(value: List<Double>): Int = value.size * 64
public fun WireSize.packedDouble(value: List<Double>): Int = value.size * 8

@InternalRpcApi
public fun WireSize.packedFixed32(value: List<UInt>): Int = value.size * 32
public fun WireSize.packedFixed32(value: List<UInt>): Int = value.size * 4

@InternalRpcApi
public fun WireSize.packedFixed64(value: List<ULong>): Int = value.size * 64
public fun WireSize.packedFixed64(value: List<ULong>): Int = value.size * 8

@InternalRpcApi
public fun WireSize.packedSFixed32(value: List<Int>): Int = value.size * 32
public fun WireSize.packedSFixed32(value: List<Int>): Int = value.size * 4

@InternalRpcApi
public fun WireSize.packedSFixed64(value: List<Long>): Int = value.size * 64
public fun WireSize.packedSFixed64(value: List<Long>): Int = value.size * 8

@InternalRpcApi
public fun WireSize.packedBool(value: List<Boolean>): Int = packedSInt32(value.map { if (it) 1 else 0 })
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ message AllPrimitives {
optional bool bool = 13;
optional string string = 14;
optional bytes bytes = 15;
string requiredString = 16;
bytes requiredBytes = 17;
}
32 changes: 32 additions & 0 deletions protobuf/protobuf-core/src/commonTest/proto/default.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
syntax = "proto2";

// https://protobuf.dev/programming-guides/proto2/#default
message WithDefaults {
optional double double = 1 [default = 2.0];
optional float float = 2 [default = 2.0];
optional int32 int32 = 3 [default = 2];
optional int64 int64 = 4 [default = 2];
optional uint32 uint32 = 5 [default = 2];
optional uint64 uint64 = 6 [default = 2];
optional sint32 sint32 = 7 [default = 2];
optional sint64 sint64 = 8 [default = 2];
optional fixed32 fixed32 = 9 [default = 2];
optional fixed64 fixed64 = 10 [default = 2];
optional sfixed32 sfixed32 = 11 [default = 2];
optional sfixed64 sfixed64 = 12 [default = 2];
optional bool bool = 13 [default = true];
optional string string = 14 [default = "str"];
optional bytes bytes = 15 [default = "bytes"];

optional Custom1 enum1 = 16;
optional Custom2 enum2 = 17 [default = BAZ];

enum Custom1 {
FOO = 1; // default
}

enum Custom2 {
BAR = 1; // default
BAZ = 2; // default
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private class WireEncoderJvm(sink: Sink) : WireEncoder {
encode: T.(WireEncoder) -> Unit,
) {
codedOutputStream.writeTag(fieldNr, WireType.LENGTH_DELIMITED.ordinal)
codedOutputStream.writeInt32NoTag(value._size)
codedOutputStream.writeUInt32NoTag(value._size)
value.encode(this)
}

Expand All @@ -190,7 +190,7 @@ private class WireEncoderJvm(sink: Sink) : WireEncoder {
) {
codedOutputStream.writeTag(fieldNr, WireType.LENGTH_DELIMITED.ordinal)
// write the field size of the packed field
codedOutputStream.writeInt32NoTag(fieldSize)
codedOutputStream.writeUInt32NoTag(fieldSize)
for (v in value) {
writer(codedOutputStream, v)
}
Expand All @@ -207,4 +207,4 @@ public actual inline fun checkForPlatformEncodeException(block: () -> Unit) {
} catch (e: IOException) {
throw ProtobufEncodingException("Failed to encode protobuf message.", e)
}
}
}
Loading
Loading