Skip to content

Commit

Permalink
cleanup types files
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewparmet committed Jun 7, 2020
1 parent 7bfa566 commit 6af8477
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 Toast Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.toasttab.protokt.rt

interface Boxed {
val value: Number
}

inline class Int32(override val value: Int) : Boxed

inline class Fixed32(override val value: Int) : Boxed

inline class SFixed32(override val value: Int) : Boxed

inline class UInt32(override val value: Int) : Boxed

inline class SInt32(override val value: Int) : Boxed

inline class Int64(override val value: Long) : Boxed

inline class Fixed64(override val value: Long) : Boxed

inline class SFixed64(override val value: Long) : Boxed

inline class UInt64(override val value: Long) : Boxed

inline class SInt64(override val value: Long) : Boxed
40 changes: 40 additions & 0 deletions protokt-runtime/src/main/kotlin/com/toasttab/protokt/rt/Bytes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 Toast Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.toasttab.protokt.rt

class Bytes(internal val value: ByteArray) {
val bytes
get() = value.clone()

fun isNotEmpty() =
value.isNotEmpty()

fun isEmpty() =
value.isEmpty()

override fun equals(other: Any?) =
other is Bytes && value.contentEquals(other.value)

override fun hashCode() =
value.contentHashCode()

override fun toString() =
value.contentToString()

companion object {
val empty = Bytes(ByteArray(0))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Toast Inc.
* Copyright (c) 2020 Toast Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,54 +15,6 @@

package com.toasttab.protokt.rt

interface Boxed {
val value: Number
}

inline class Int32(override val value: Int) : Boxed

inline class Fixed32(override val value: Int) : Boxed

inline class SFixed32(override val value: Int) : Boxed

inline class UInt32(override val value: Int) : Boxed

inline class SInt32(override val value: Int) : Boxed

inline class Int64(override val value: Long) : Boxed

inline class Fixed64(override val value: Long) : Boxed

inline class SFixed64(override val value: Long) : Boxed

inline class UInt64(override val value: Long) : Boxed

inline class SInt64(override val value: Long) : Boxed

class Bytes(internal val value: ByteArray) {
val bytes
get() = value.clone()

fun isNotEmpty() =
value.isNotEmpty()

fun isEmpty() =
value.isEmpty()

override fun equals(other: Any?) =
other is Bytes && value.contentEquals(other.value)

override fun hashCode() =
value.contentHashCode()

override fun toString() =
value.contentToString()

companion object {
val empty = Bytes(ByteArray(0))
}
}

class BytesSlice(
internal val array: ByteArray,
internal val offset: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ annotation class KtGeneratedMessage(
* The full protocol buffer type name of this message
* used for packing into an Any.
*/
val fullTypeName: String = ""
val fullTypeName: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fun deserializer(
val fieldNum = WireFormat.getTagFieldNumber(tag)
return when (WireFormat.getTagWireType(tag)) {
WireFormat.WIRETYPE_VARINT ->
Unknown(fieldNum, VarIntVal(stream.readInt64()))
Unknown(fieldNum, VarIntVal(UInt64(stream.readInt64())))
WireFormat.WIRETYPE_FIXED64 ->
Unknown(fieldNum, Fixed64Val(Fixed64(stream.readFixed64())))
WireFormat.WIRETYPE_LENGTH_DELIMITED ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.toasttab.protokt.rt

import kotlin.reflect.KClass

private val type0 =
private val TYPE_0 by lazy {
listOf(
Boolean::class,
KtEnum::class,
Expand All @@ -28,33 +28,38 @@ private val type0 =
UInt32::class,
UInt64::class
)
}

private val type1 =
private val TYPE_1 by lazy {
listOf(
Double::class,
Fixed64::class,
SFixed64::class
)
}

private val type2 =
private val TYPE_2 by lazy {
listOf(
Bytes::class,
KtMessage::class,
String::class
)
}

private val type5 =
private val TYPE_5 by lazy {
listOf(
Float::class,
Fixed32::class,
SFixed32::class
)
}

private val wireTypes =
type0.associateWith { 0 } +
type1.associateWith { 1 } +
type2.associateWith { 2 } +
type5.associateWith { 5 }
private val WIRE_TYPES by lazy {
TYPE_0.associateWith { 0 } +
TYPE_1.associateWith { 1 } +
TYPE_2.associateWith { 2 } +
TYPE_5.associateWith { 5 }
}

fun wireType(klass: KClass<*>) =
wireTypes.getValue(klass)
WIRE_TYPES.getValue(klass)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CollectionTest {
f.addField(
it.fieldNum,
UnknownFieldSet.Field.newBuilder()
.addVarint((it.value as VarIntVal).value).build()
.addVarint((it.value as VarIntVal).value.value).build()
)
}
f.build()
Expand Down

0 comments on commit 6af8477

Please sign in to comment.