jsMain.com.squareup.wire.ProtoAdapter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wire-runtime-js Show documentation
Show all versions of wire-runtime-js Show documentation
gRPC and protocol buffers for Android, Kotlin, and Java.
/*
* Copyright 2015 Square 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.squareup.wire
import okio.BufferedSink
import okio.BufferedSource
import okio.ByteString
import kotlin.reflect.KClass
actual abstract class ProtoAdapter actual constructor(
internal actual val fieldEncoding: FieldEncoding,
actual val type: KClass<*>?,
actual val typeUrl: String?,
actual val syntax: Syntax,
actual val identity: E?,
actual val sourceFile: String?
) {
internal actual val packedAdapter: ProtoAdapter>? = when {
this is PackedProtoAdapter<*> || this is RepeatedProtoAdapter<*> -> null
fieldEncoding == FieldEncoding.LENGTH_DELIMITED -> null
else -> commonCreatePacked()
}
internal actual val repeatedAdapter: ProtoAdapter>? = when {
this is RepeatedProtoAdapter<*> || this is PackedProtoAdapter<*> -> null
else -> commonCreateRepeated()
}
/** Returns the redacted form of `value`. */
actual abstract fun redact(value: E): E
/**
* The size of the non-null data `value`. This does not include the size required for a
* length-delimited prefix (should the type require one).
*/
actual abstract fun encodedSize(value: E): Int
/**
* The size of `tag` and `value` in the wire format. This size includes the tag, type,
* length-delimited prefix (should the type require one), and value. Returns 0 if `value` is
* null.
*/
actual open fun encodedSizeWithTag(tag: Int, value: E?): Int {
return commonEncodedSizeWithTag(tag, value)
}
/** Write non-null `value` to `writer`. */
actual abstract fun encode(writer: ProtoWriter, value: E)
/** Write non-null `value` to `writer`. */
actual open fun encode(writer: ReverseProtoWriter, value: E) {
delegateEncode(writer, value)
}
/** Write `tag` and `value` to `writer`. If value is null this does nothing. */
actual open fun encodeWithTag(writer: ProtoWriter, tag: Int, value: E?) {
commonEncodeWithTag(writer, tag, value)
}
/** Write `tag` and `value` to `writer`. If value is null this does nothing. */
actual open fun encodeWithTag(writer: ReverseProtoWriter, tag: Int, value: E?) {
commonEncodeWithTag(writer, tag, value)
}
/** Encode `value` and write it to `stream`. */
actual fun encode(sink: BufferedSink, value: E) {
commonEncode(sink, value)
}
/** Encode `value` as a `byte[]`. */
actual fun encode(value: E): ByteArray {
return commonEncode(value)
}
/** Encode `value` as a [ByteString]. */
actual fun encodeByteString(value: E): ByteString {
return commonEncodeByteString(value)
}
/** Read a non-null value from `reader`. */
actual abstract fun decode(reader: ProtoReader): E
/** Read an encoded message from `bytes`. */
actual fun decode(bytes: ByteArray): E {
return commonDecode(bytes)
}
/** Read an encoded message from `bytes`. */
actual fun decode(bytes: ByteString): E {
return commonDecode(bytes)
}
/** Read an encoded message from `source`. */
actual fun decode(source: BufferedSource): E {
return commonDecode(source)
}
/** Returns a human-readable version of the given `value`. */
actual open fun toString(value: E): String {
return commonToString(value)
}
internal actual fun withLabel(label: WireField.Label): ProtoAdapter<*> {
return commonWithLabel(label)
}
/** Returns an adapter for `E` but as a packed, repeated value. */
actual fun asPacked(): ProtoAdapter> {
require(fieldEncoding != FieldEncoding.LENGTH_DELIMITED) {
"Unable to pack a length-delimited type."
}
return packedAdapter ?: throw UnsupportedOperationException(
"Can't create a packed adapter from a packed or repeated adapter.")
}
/**
* Returns an adapter for `E` but as a repeated value.
*
* Note: Repeated items are not required to be encoded sequentially. Thus, when decoding using
* the returned adapter, only single-element lists will be returned and it is the caller's
* responsibility to merge them into the final list.
*/
actual fun asRepeated(): ProtoAdapter> {
return repeatedAdapter ?: throw UnsupportedOperationException(
"Can't create a repeated adapter from a repeated or packed adapter.")
}
actual class EnumConstantNotFoundException actual constructor(
actual val value: Int,
type: KClass<*>?
) : IllegalArgumentException("Unknown enum tag $value for ${type?.simpleName}")
actual companion object {
/**
* Creates a new proto adapter for a map using `keyAdapter` and `valueAdapter`.
*
* Note: Map entries are not required to be encoded sequentially. Thus, when decoding using
* the returned adapter, only single-element maps will be returned and it is the caller's
* responsibility to merge them into the final map.
*/
actual fun newMapAdapter(
keyAdapter: ProtoAdapter,
valueAdapter: ProtoAdapter
): ProtoAdapter