Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to recent Kotlin naming conventions #90

Merged
merged 1 commit into from
Sep 17, 2020
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
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ data class Person(
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<Person> {
val defaultInstance by lazy { Person() }
override fun unmarshal(u: pbandk.MessageUnmarshaller) = Person.unmarshalImpl(u)
override fun decodeWith(u: pbandk.MessageDecoder) = Person.decodeWithImpl(u)

override val descriptors: pbandk.MessageDescriptor<Person> by lazy {
pbandk.MessageDescriptor(
Expand Down Expand Up @@ -181,7 +181,7 @@ data class Person(
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<Person.PhoneNumber> {
val defaultInstance by lazy { Person.PhoneNumber() }
override fun unmarshal(u: pbandk.MessageUnmarshaller) = Person.PhoneNumber.unmarshalImpl(u)
override fun decodeWith(u: pbandk.MessageDecoder) = Person.PhoneNumber.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<PhoneNumber> by lazy {
pbandk.MessageDescriptor(
Expand Down Expand Up @@ -220,7 +220,7 @@ data class AddressBook(
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<AddressBook> {
val defaultInstance by lazy { AddressBook() }
override fun unmarshal(u: pbandk.MessageUnmarshaller) = AddressBook.unmarshalImpl(u)
override fun decodeWith(u: pbandk.MessageDecoder) = AddressBook.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<AddressBook> by lazy {
pbandk.MessageDescriptor(
Expand Down Expand Up @@ -374,15 +374,16 @@ package is referenced, it is assumed to be a well-known type and is changed to r

### Message

Each Protobuf message extends `pbandk.Message` and has two overloaded `protoMarshal` methods, the most useful of which
marshals to a byte array. The companion object of every message has two overloaded `protoUnmarshal` methods, the most
useful of which accepts a byte array and returns an instance of the class. The other `protoMarshal` and `protoUnmarshal`
methods accept `Marshaller` and `Unmarshaller` instances respectively which are different for each platform. For
example, the JVM `Marshaller` uses `com.google.protobuf.CodedOutputStream`.
Each Protobuf message extends `pbandk.Message` and has an `encodeToByteArray` method to encode the message with the
Protobuf binary encoding into a byte array. The companion object of every message has a `decodeFromByteArray` method: it
accepts a byte array and returns an instance of the class. Each platform also provides additional `encodeTo*` and
`decodeFrom*` methods that are platform-specific. For example, the JVM provides `encodeToStream` and `decodeFromStream`
methods that operate on Java's `OutputStream` and `InputStream`, respectively, and use
`com.google.protobuf.CodedOutputStream` internally.

Messages are immutable Kotlin data classes. This means they automatically implement `hashCode`, `equals`, and
`toString`. Each class has an `unknownFields` map which contains information about extra fields the unmarshaller didn't
recognize. If there are values in this map, they will be marshalled on output. The `Unmarshaller` instances have a
`toString`. Each class has an `unknownFields` map which contains information about extra fields the decoder didn't
recognize. If there are values in this map, they will be encoded on output. The `MessageDecoder` instances have a
constructor option to discard unknown fields when reading.

For proto3, the only nullable fields are other messages and oneof fields. Other values have defaults. For proto2,
Expand Down
20 changes: 10 additions & 10 deletions conformance/lib/src/commonMain/kotlin/pbandk/conformance/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import pbandk.ExperimentalProtoJson
import pbandk.conformance.Platform.runBlockingMain
import pbandk.conformance.pb.*
import pbandk.json.JsonConfig
import pbandk.json.jsonMarshal
import pbandk.json.jsonUnmarshal
import pbandk.protoMarshal
import pbandk.protoUnmarshal
import pbandk.json.encodeToJsonString
import pbandk.json.decodeFromJsonString
import pbandk.encodeToByteArray
import pbandk.decodeFromByteArray

var logDebug = false

Expand All @@ -21,7 +21,7 @@ fun main(args: Array<String>) = runBlockingMain {
while (true) {
val res = doTestIo().also { debug { "Result: $it" } } ?: return@runBlockingMain

ConformanceResponse(res).protoMarshal().also { bytes ->
ConformanceResponse(res).encodeToByteArray().also { bytes ->
Platform.stdoutWriteIntLE(bytes.size)
Platform.stdoutWriteFull(bytes)
}
Expand All @@ -33,7 +33,7 @@ suspend fun doTestIo(): ConformanceResponse.Result<*>? {
// Read the request (starting with by size)
val req = Platform.doTry({
val size = Platform.stdinReadIntLE()?.also { debug { "Reading $it bytes" } } ?: return null
ConformanceRequest.protoUnmarshal(Platform.stdinReadFull(size)).also { debug { "Request: $it" } }
ConformanceRequest.decodeFromByteArray(Platform.stdinReadFull(size)).also { debug { "Request: $it" } }
}) { err -> return ConformanceResponse.Result.RuntimeError("Failed reading request: $err") }
// Parse
val parsed = Platform.doTry({
Expand All @@ -45,7 +45,7 @@ suspend fun doTestIo(): ConformanceResponse.Result<*>? {

when (req.payload) {
is ConformanceRequest.Payload.ProtobufPayload -> {
typeComp.protoUnmarshal(req.payload.value.array).also { debug { "Parsed: $it" } }
typeComp.decodeFromByteArray(req.payload.value.array).also { debug { "Parsed: $it" } }
}
is ConformanceRequest.Payload.JsonPayload -> {
val jsonConfig = when (req.testCategory) {
Expand All @@ -54,7 +54,7 @@ suspend fun doTestIo(): ConformanceResponse.Result<*>? {
)
else -> JsonConfig.DEFAULT
}
typeComp.jsonUnmarshal(req.payload.value, jsonConfig).also { debug { "Parsed: $it" } }
typeComp.decodeFromJsonString(req.payload.value, jsonConfig).also { debug { "Parsed: $it" } }
}
else -> {
return ConformanceResponse.Result.Skipped("Only protobuf and json input supported")
Expand All @@ -67,8 +67,8 @@ suspend fun doTestIo(): ConformanceResponse.Result<*>? {
// Serialize
return Platform.doTry({
when (req.requestedOutputFormat) {
is WireFormat.PROTOBUF -> ConformanceResponse.Result.ProtobufPayload(ByteArr(parsed.protoMarshal()))
is WireFormat.JSON -> ConformanceResponse.Result.JsonPayload(parsed.jsonMarshal())
is WireFormat.PROTOBUF -> ConformanceResponse.Result.ProtobufPayload(ByteArr(parsed.encodeToByteArray()))
is WireFormat.JSON -> ConformanceResponse.Result.JsonPayload(parsed.encodeToJsonString())
else -> {
return ConformanceResponse.Result.Skipped("Only protobuf and json output supported")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ data class FailureSet(
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<FailureSet> {
val defaultInstance by lazy { FailureSet() }
override fun unmarshal(u: pbandk.MessageUnmarshaller) = FailureSet.unmarshalImpl(u)
override fun decodeWith(u: pbandk.MessageDecoder) = FailureSet.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<FailureSet> by lazy {
pbandk.MessageDescriptor(
Expand Down Expand Up @@ -101,7 +101,7 @@ data class ConformanceRequest(
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<ConformanceRequest> {
val defaultInstance by lazy { ConformanceRequest() }
override fun unmarshal(u: pbandk.MessageUnmarshaller) = ConformanceRequest.unmarshalImpl(u)
override fun decodeWith(u: pbandk.MessageDecoder) = ConformanceRequest.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<ConformanceRequest> by lazy {
pbandk.MessageDescriptor(
Expand Down Expand Up @@ -227,7 +227,7 @@ data class ConformanceResponse(
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<ConformanceResponse> {
val defaultInstance by lazy { ConformanceResponse() }
override fun unmarshal(u: pbandk.MessageUnmarshaller) = ConformanceResponse.unmarshalImpl(u)
override fun decodeWith(u: pbandk.MessageDecoder) = ConformanceResponse.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<ConformanceResponse> by lazy {
pbandk.MessageDescriptor(
Expand Down Expand Up @@ -321,7 +321,7 @@ data class JspbEncodingConfig(
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<JspbEncodingConfig> {
val defaultInstance by lazy { JspbEncodingConfig() }
override fun unmarshal(u: pbandk.MessageUnmarshaller) = JspbEncodingConfig.unmarshalImpl(u)
override fun decodeWith(u: pbandk.MessageDecoder) = JspbEncodingConfig.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<JspbEncodingConfig> by lazy {
pbandk.MessageDescriptor(
Expand Down Expand Up @@ -350,7 +350,7 @@ private fun FailureSet.protoMergeImpl(plus: pbandk.Message?): FailureSet = (plus
) ?: this

@Suppress("UNCHECKED_CAST")
private fun FailureSet.Companion.unmarshalImpl(u: pbandk.MessageUnmarshaller): FailureSet {
private fun FailureSet.Companion.decodeWithImpl(u: pbandk.MessageDecoder): FailureSet {
var failure: pbandk.ListWithSize.Builder<String>? = null

val unknownFields = u.readMessage(this) { _fieldNumber, _fieldValue ->
Expand All @@ -370,7 +370,7 @@ private fun ConformanceRequest.protoMergeImpl(plus: pbandk.Message?): Conformanc
) ?: this

@Suppress("UNCHECKED_CAST")
private fun ConformanceRequest.Companion.unmarshalImpl(u: pbandk.MessageUnmarshaller): ConformanceRequest {
private fun ConformanceRequest.Companion.decodeWithImpl(u: pbandk.MessageDecoder): ConformanceRequest {
var requestedOutputFormat: pbandk.conformance.pb.WireFormat = pbandk.conformance.pb.WireFormat.fromValue(0)
var messageType = ""
var testCategory: pbandk.conformance.pb.TestCategory = pbandk.conformance.pb.TestCategory.fromValue(0)
Expand Down Expand Up @@ -403,7 +403,7 @@ private fun ConformanceResponse.protoMergeImpl(plus: pbandk.Message?): Conforman
) ?: this

@Suppress("UNCHECKED_CAST")
private fun ConformanceResponse.Companion.unmarshalImpl(u: pbandk.MessageUnmarshaller): ConformanceResponse {
private fun ConformanceResponse.Companion.decodeWithImpl(u: pbandk.MessageDecoder): ConformanceResponse {
var result: ConformanceResponse.Result<*>? = null

val unknownFields = u.readMessage(this) { _fieldNumber, _fieldValue ->
Expand All @@ -428,7 +428,7 @@ private fun JspbEncodingConfig.protoMergeImpl(plus: pbandk.Message?): JspbEncodi
) ?: this

@Suppress("UNCHECKED_CAST")
private fun JspbEncodingConfig.Companion.unmarshalImpl(u: pbandk.MessageUnmarshaller): JspbEncodingConfig {
private fun JspbEncodingConfig.Companion.decodeWithImpl(u: pbandk.MessageDecoder): JspbEncodingConfig {
var useJspbArrayAnyFormat = false

val unknownFields = u.readMessage(this) { _fieldNumber, _fieldValue ->
Expand Down
Loading