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

com.bybutter.sisyphus.protobuf.primitives.ValueExtension.kt Maven / Gradle / Ivy

There is a newer version: 2.1.22
Show newest version
package com.bybutter.sisyphus.protobuf.primitives

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ObjectNode

fun JsonNode.toProto(): Value = Value {
    when {
        [email protected] -> {
            listValue = ListValue {
                values += [email protected] { it.toProto() }
            }
        }
        [email protected] -> {
            structValue = Struct {
                for ((field, node) in [email protected]()) {
                    fields[field] = node.toProto()
                }
            }
        }
        [email protected] -> {
            numberValue = [email protected]()
        }
        [email protected] -> {
            boolValue = [email protected]()
        }
        [email protected] -> {
            val text = [email protected]()
            text.toDoubleOrNull()?.let {
                numberValue = it
            } ?: text.toBooleanOrNull()?.let {
                boolValue = it
            } ?: run {
                stringValue = text
            }
        }
        [email protected] -> {
            nullValue = NullValue.NULL_VALUE
        }
        else -> throw UnsupportedOperationException("Unknown json type '$this'.")
    }
}

fun ObjectNode.toProto(): Struct = Struct {
    for ((field, node) in [email protected]()) {
        fields[field] = node.toProto()
    }
}

fun String.toBooleanOrNull(): Boolean? {
    return if (this.equals("true", true) || this.equals("false", true)) {
        this.toBoolean()
    } else {
        null
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy