Created
March 6, 2018 10:45
-
-
Save AdrianRaFo/cb3e6d28540510cc09bdf84212df6354 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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