Skip to content

Instantly share code, notes, and snippets.

@AdrianRaFo
Created March 6, 2018 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdrianRaFo/cb3e6d28540510cc09bdf84212df6354 to your computer and use it in GitHub Desktop.
Save AdrianRaFo/cb3e6d28540510cc09bdf84212df6354 to your computer and use it in GitHub Desktop.
import com.google.cloud.datastore.Entity.Builder
import com.google.cloud.datastore._
import scala.util.Random
object GCPAccess {
val datastore: Datastore = DatastoreOptions.getDefaultInstance.getService
val rnd = Random
def add(kind: Kind, key: Long): Entity = {
val newKey = datastore.newKeyFactory.setKind(kind.name).newKey(key)
put(kind, newKey)
}
def add(kind: Kind, name: String): Entity = {
val newKey = datastore.newKeyFactory.setKind(kind.name).newKey(name)
put(kind, newKey)
}
private def put(kind: Kind, newKey: Key): Entity = {
val newEntity = Entity.newBuilder(newKey)
datastore.put(kind match {
case foo: Foo => putFoo(newEntity, foo)
})
}
private def putFoo(entityBuilder: Builder, foo: Foo): Entity = {
entityBuilder
.set("bar", foo.bar)
.build()
}
}
sealed trait Kind {
val name = this.getClass.getSimpleName
}
case class Foo(bar:String) extends Kind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment