All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.provenance.client.protobuf.extensions.Protobuf.kt Maven / Gradle / Ivy

There is a newer version: 1.20.1
Show newest version
package io.provenance.client.protobuf.extensions

import com.google.protobuf.Any
import com.google.protobuf.GeneratedMessageV3
import com.google.protobuf.Message
import cosmos.tx.v1beta1.TxOuterClass

/*
 * Check if a protobuf field is set
 */
fun GeneratedMessageV3?.isSet() = when {
    this != null && this != this.defaultInstanceForType -> true
    else -> false
}

fun GeneratedMessageV3.isNotSet() = !this.isSet()

fun  T.whenSet(block: (T) -> Unit) =
    this.takeIf { it.isSet() }?.also { block(it) }

fun  T.whenNotSet(block: (T) -> Unit) =
    this.takeIf { it.isNotSet() }?.also { block(it) }

fun  T.whenSetLet(block: (T) -> K) =
    this.takeIf { it.isSet() }?.let { block(it) }

fun  T.whenNotSetLet(block: (T) -> K) =
    this.takeIf { it.isNotSet() }?.let { block(it) }

fun Message.toAny(typeUrlPrefix: String = ""): Any = Any.pack(this, typeUrlPrefix)

fun Iterable.toTxBody(memo: String? = null, timeoutHeight: Long? = null): TxOuterClass.TxBody =
    TxOuterClass.TxBody.newBuilder()
        .addAllMessages(this)
        .also { builder ->
            memo?.run { builder.memo = this }
            timeoutHeight?.run { builder.timeoutHeight = this }
        }
        .build()

fun Any.toTxBody(memo: String? = null, timeoutHeight: Long? = null): TxOuterClass.TxBody =
    listOf(this).toTxBody(memo, timeoutHeight)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy