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

main.misk.cloud.gcp.datastore.DatastoreExtensions.kt Maven / Gradle / Ivy

There is a newer version: 2024.09.17.200749-4708422
Show newest version
// Extensions to Google Cloud Datastore types
package misk.cloud.gcp.datastore

import com.google.cloud.datastore.Blob
import com.google.cloud.datastore.Entity
import com.google.cloud.datastore.Key
import com.google.cloud.datastore.QueryResults
import com.squareup.wire.ProtoAdapter
import okio.ByteString
import okio.ByteString.Companion.toByteString

fun Blob.toByteString(): ByteString = asReadOnlyByteBuffer().toByteString()

fun Entity.getByteString(name: String) = getBlob(name).toByteString()

fun  Entity.getProto(name: String, protoAdapter: ProtoAdapter): T =
  protoAdapter.decode(getByteString(name))

fun Entity.Builder.set(name: String, bytes: ByteString): Entity.Builder =
  set(name, Blob.copyFrom(bytes.asByteBuffer()))

fun  QueryResults.asList(): List = asSequence().toList()

object Keys {
  fun newKey(projectId: String, kind: String, id: Long): Key =
    Key.newBuilder(projectId, kind, id).build()

  fun newKey(projectId: String, kind: String, name: String): Key =
    Key.newBuilder(projectId, kind, name).build()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy