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

Conversation

garyp
Copy link
Collaborator

@garyp garyp commented Sep 12, 2020

Kotlin is starting to standardize on some naming patterns in its standard library and related libraries (like kotlinx-serialization and kotlinx-coroutines). This change implements similar naming conventions so that pbandk's API feels more idiomatic and familiar to Kotlin developers.

  • Replace "marshal"/"unmarshal" terminology with "encode"/"decode" in all methods and classes

  • Standardize encoding/decoding methods to be named encodeTo* and decodeFrom*, similar to naming used in the standard libraries

  • Rename the low-level marshal(MessageMarshaller) and unmarshal(MessageUnmarshaller) methods to encodeWith(MessageEncoder) and decodeWith(MessageDecoder)

Fixes #89

- Rename "marshal"/"unmarshal" to "encode"/"decode" in all methods and
classes

- Standardize encoding/decoding methods to be named `encodeTo*` and
`decodeFrom*`, similar to naming used in the standard libraries

- Rename the low-level `marshal(MessageMarshaller)` and
`unmarshal(MessageUnmarshaller)` methods to `encodeWith(MessageEncoder)`
and `decodeWith(MessageDecoder)`

Fixes #89
@garyp garyp force-pushed the kotlin-naming-conventions branch from 6e4c0e7 to 962ce2b Compare September 14, 2020 19:07
@garyp garyp marked this pull request as ready for review September 15, 2020 21:47
Copy link
Contributor

@Mehequanna Mehequanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. I'm happy to see the marshal and unmarshal language get updated. Thanks for doing this!

I saw a bunch of files needing a new line. I added comments on them, but maybe it should just be a TODO for us to run Kotlin lint on the codebase in the future? Feel free to take or leave the comments.

companion object {
fun allocate(size: Int) = KotlinBinaryMessageEncoder(ByteArrayWireWriter.allocate(size))
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Need a new line after }

data: String,
jsonConfig: JsonConfig = JsonConfig.DEFAULT
): T = decodeWith(JsonMessageDecoder.fromString(data, jsonConfig))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Needs new line at the end

assertEquals((byteArrayOf(10, 12) + "Hello world!".map { it.toByte() }.toByteArray()).asList(), bytes.asList())
assertEquals(foo, Foo.protoUnmarshal(bytes))
assertEquals(foo, Foo.decodeFromByteArray(bytes))
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Add new line


internal actual fun BinaryMessageEncoder.Companion.allocate(size: Int): ByteArrayMessageEncoder {
return ProtobufjsBinaryMessageEncoder.allocate(size)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Add new line

companion object {
fun allocate(size: Int) = ProtobufjsBinaryMessageEncoder(Writer.create(), size)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Add new line


internal fun BinaryMessageDecoder.Companion.fromByteBuffer(buffer: ByteBuffer): MessageDecoder {
return BinaryMessageDecoder(CodedStreamBinaryWireDecoder(CodedInputStream.newInstance(buffer)))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Add a new line


internal actual fun BinaryMessageEncoder.Companion.allocate(size: Int): ByteArrayMessageEncoder {
return ByteArrayCodedStreamBinaryMessageEncoder.allocate(size)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Add a new line


override fun toByteArray() = backingBytes

companion object {
fun allocate(size: Int) = ByteArrayCodedStreamBinaryMessageMarshaller(ByteArray(size))
fun allocate(size: Int) = ByteArrayCodedStreamBinaryMessageEncoder(ByteArray(size))
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Add a new line

assertEquals(builtJavaObj, gendJavaObj)
assertEquals(builtKotlinObj, gendKotlinObj)

// Check that map-with-size
assertTrue(gendKotlinObj.map is MessageMap)
assertEquals(builtKotlinObj.protoSize, gendKotlinObj.protoSize)
assertEquals(builtKotlinBytes.toList(), gendKotlinObj.protoMarshal().toList())
assertEquals(builtKotlinBytes.toList(), gendKotlinObj.encodeToByteArray().toList())
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Add a new line


internal actual fun BinaryMessageEncoder.Companion.allocate(size: Int): ByteArrayMessageEncoder {
return KotlinBinaryMessageEncoder.allocate(size)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧 Add a new line

@garyp
Copy link
Collaborator Author

garyp commented Sep 17, 2020

Hmm, yeah. We should probably add the ktlint gradle plugin to this project so that it's run automatically. Until then though, I'd prefer to not play whack-a-mole with stray newlines (or lack thereof), so I'll leave it as-is.

@garyp garyp merged commit 6437972 into master Sep 17, 2020
@garyp garyp deleted the kotlin-naming-conventions branch September 17, 2020 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Align pbandk's naming with the conventions in recent Kotlin releases
2 participants