Skip to content
Open
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
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.0-all.zip
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
}

dependencies {
compile project(':runtime')
compile project(':runtime-lite')
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile 'com.github.cretz.pbandk:protoc-gen-kotlin-jvm:0.3.0'
compile 'com.squareup:kotlinpoet:1.0.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.improve_future.case_changer.toSnakeCase
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import pbandk.gen.File
import java.io.Serializable

class MessageGenerator(private val file: File, private val kotlinTypeMappings: Map<String, String>) {

Expand Down
47 changes: 22 additions & 25 deletions runtime-lite/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'guru.stefma.artifactorypublish'
apply plugin: 'kotlin-multiplatform'

sourceCompatibility = 1.8

Expand All @@ -13,27 +11,26 @@ repositories {

}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile "com.google.protobuf:protobuf-lite:3.0.1"
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile('com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0')
testCompile("org.assertj:assertj-core:3.11.1")
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
kotlin {
targets {
jvm()
}

artifactoryPublish {
groupId = 'jp.co.panpanini'
artifactId = 'protok-runtime-lite'
publishVersion = versionName()
artifactoryUrl = artifactoryUrl
artifactoryRepo = artifactoryRepo
artifactoryUser = artifactoryUsername
artifactoryKey = artifactoryPassword
sourceSets {
commonMain {}
commonTest {}
jvmMain {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "com.google.protobuf:protobuf-lite:3.0.1"
}
}
jvmTest {
dependencies {
implementation group: 'junit', name: 'junit', version: '4.12'
implementation('com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0')
implementation("org.assertj:assertj-core:3.11.1")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package jp.co.panpanini

import java.io.Serializable

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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package jp.co.panpanini

import java.io.InputStream
import java.io.Serializable

interface Message<T : Message<T>> : Serializable {
fun protoUnmarshal(u: Unmarshaller): T
fun protoUnmarshal(arr: ByteArray) = protoUnmarshal(Unmarshaller.fromByteArray(arr))
fun protoUnmarshal(inputStream: InputStream) = protoUnmarshal(Unmarshaller(Reader(inputStream.readBytes())))

operator fun plus(other: T?): T
val protoSize: Int
fun protoMarshal(m: Marshaller)
fun protoMarshal(): ByteArray = Marshaller.allocate(protoSize).also(::protoMarshal).complete()!!
fun protoMarshal(): ByteArray = Marshaller.allocate(protoSize).also(::protoMarshal).complete()

interface Companion<T : Message<T>> {
fun protoUnmarshal(u: Unmarshaller): T
Expand All @@ -25,5 +21,4 @@ interface Message<T : Message<T>> : Serializable {
fun fromValue(value: Int): T
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ class Reader(private val byteArray: ByteArray) {
* nothing is skipped. Otherwise, returns `true`.
*/
fun skipField(tag: Int): Boolean {
return when (com.google.protobuf.WireFormat.getTagWireType(tag)) {
return when (WireFormat.getTagWireType(tag)) {
WireFormat.WIRETYPE_VARINT -> {
skipRawVarint()
true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package jp.co.panpanini

expect interface Serializable
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package jp.co.panpanini

import com.google.protobuf.CodedOutputStream

object Sizer {
fun tagSize(fieldNum: Int): Int {
Expand Down Expand Up @@ -48,7 +47,30 @@ object Sizer {

fun int64Size(value: Long) = uInt64Size(value)

fun uInt64Size(value: Long) = CodedOutputStream.computeUInt64SizeNoTag(value)
fun uInt64Size(value: Long): Int {
var value = value
// handle two popular special cases up front ...
if (value and (0L.inv() shl 7) == 0L) {
return 1
}
if (value < 0L) {
return 10
}
// ... leaving us with 8 remaining, which we can divide and conquer
var n = 2
if (value and (0L.inv() shl 35) != 0L) {
n += 4
value = value ushr 28
}
if (value and (0L.inv() shl 21) != 0L) {
n += 2
value = value ushr 14
}
if (value and (0L.inv() shl 14) != 0L) {
n += 1
}
return n
}

fun bytesSize(value: ByteArray) = uInt32Size(value.size) + value.size

Expand All @@ -72,7 +94,7 @@ object Sizer {

fun boolSize(value: Boolean) = 1

fun stringSize(value: String) = CodedOutputStream.computeStringSizeNoTag(value)
fun stringSize(value: String) = try { Utf8.encodedLength(value) } catch(e: Exception) { value.toByteArray().size }

private fun Int.zigZagEncode() = (this shl 1) xor (this shr 31)
private fun Long.zigZagEncode() = (this shl 1) xor (this shr 63)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package jp.co.panpanini

expect fun String.toByteArray(): ByteArray
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package jp.co.panpanini

import java.io.Serializable


data class UnknownField(val fieldNum: Int, val value: Value) : Serializable {
constructor(fieldNum: Int, value: Long, fixed: Boolean = false) :
this(fieldNum, if (fixed) Value.Fixed64(value) else Value.Varint(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Unmarshaller(private val reader: Reader, private val discardUnknownFields:
}
val message = companion.protoUnmarshal(this)
require(reader.isAtEnd) {
"Unable to completely read stream for message ${message::class.java}"
"Unable to completely read stream for message ${message::class}"
}
reader.popLimit(previousLimit)
currentUnknownFields = unknownFields
Expand Down Expand Up @@ -87,9 +87,9 @@ class Unmarshaller(private val reader: Reader, private val discardUnknownFields:
}

fun <T: Message<T>> readRepeatedMessage(
appendTo: List<T>?,
companion: Message.Companion<T>,
neverPacked: Boolean
appendTo: List<T>?,
companion: Message.Companion<T>,
neverPacked: Boolean
): List<T> {
return readRepeated(appendTo, neverPacked) {
readMessage(companion)
Expand Down
7 changes: 7 additions & 0 deletions runtime-lite/src/jvmMain/kotlin/jp/co/panpanini/Message.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package jp.co.panpanini

import java.io.InputStream

fun <T: Message<T>> Message<T>.protoUnmarshal(inputStream: InputStream) = protoUnmarshal(
Unmarshaller(Reader(inputStream.readBytes()))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package jp.co.panpanini

actual typealias Serializable = java.io.Serializable
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package jp.co.panpanini

actual fun String.toByteArray(): ByteArray = this.toByteArray(Charsets.UTF_8)
6 changes: 6 additions & 0 deletions sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Sample

A sample project to show how to use the protok library to generate Kotlin classes and how to use the generated classes themselves.

## usage
Use the gradle task `generateProto` to generate the required Kotlin protocol buffer implementations. This expects protoc to be installed and available on the PATH.
29 changes: 29 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm'
}

version '1.0.0'

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
testCompile group: 'junit', name: 'junit', version: '4.12'

implementation project(":runtime-lite")
}

task generateProto(type: Exec) {
dependsOn(":library:installDist")
commandLine(
'protoc',
"--plugin=protoc-gen-custom=${rootDir}/library/build/install/protoc-gen-kotlin/bin/protoc-gen-kotlin",
"--custom_out=${projectDir}/src/main/kotlin/",
"--proto_path=${projectDir}/src/main/proto/",
"user.proto"
)

}
38 changes: 38 additions & 0 deletions sample/src/main/kotlin/Sample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main.kotlin

class Sample(private val name: String) {
companion object {
private var LATEST_ID = 1
}

fun generateUser(): User {
return User.with {
id = LATEST_ID++
name = this@Sample.name
programingLanguages = listOf(
ProgramingLanguage.with {
name = "Kotlin"
yearsLearning(5)
},
ProgramingLanguage.with {
name = "Java"
yearsLearning(10)
}
)
}
}

}


fun main() {
val sample = Sample("Panini")

val user = sample.generateUser()

println(user)
println(user.name)
user.programingLanguages.forEach {
println("learned ${it.name} for ${it.yearsLearning} years")
}
}
14 changes: 14 additions & 0 deletions sample/src/main/proto/user.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package proto;

message User {
int32 id = 1;
string name = 2;
repeated ProgramingLanguage programingLanguages = 3;
}

message ProgramingLanguage {
string name = 1;
int32 yearsLearning = 2;
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include ':library', ':retrofit-converter', ':runtime', ':runtime-lite', ':retrofit-converter-lite'
include ':library', ':retrofit-converter', ':runtime', ':runtime-lite', ':retrofit-converter-lite',':sample'
rootProject.name = 'protok'