-
Notifications
You must be signed in to change notification settings - Fork 0
Structures
Home ▸ Structure Examples
During our design steps, our team were absolutely certain that the user of this package should have absolute control over their data. In this data-oriented design we have created a system that is as complicated or simple as you wish it to be.
This is a trivial example of how easy it is to access and use all of the functionality Retrievable offers. In this example we will implement KeyRetrievable and show an object then placed into both Memcache and Datastore for later retrieval.
package main
import (
"github.com/Esseh/retrievable"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
// A is an example structure implementing KeyRetrievable
// The goal of A is to return it's value and the ID it was stored with
type A struct {
Value string
// We will mark the ID as not to be stored in either Datastore or Memcache
ID string `datastore:"-" json:"-"`
}
// This is the function to implement Retrievable, A in this context
// knows the Kind it is a part of and that it's key is a string.
func (a *A) Key(ctx context.Context, key interface{}) *datastore.Key {
return datastore.NewKey(ctx, "tableA", key.(string), 0, nil)
}
// This is the other function to implement KeyRetrievable,
// now when retrieved A will have it's key in ID.
func (a *A) StoreKey(key *datastore.Key) {
a.ID = key.StringID()
}
func Example(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(req)
a := A{}
a.Value = "Example Information"
// Now that A implements KeyRetrievable, we may use the associated functions.
retrievable.PlaceInDatastore(ctx, "Key Value", &a)
}Simple usage such as above will cover the largest part of usage, but there are instances in which you will need more control over the key. This example below is one such case.
This example will highlight the simplicity of using Package:Retrievable even in complex examples. This case has a multi-part key with a variable Kind.
package main
import (
"fmt"
"github.com/Esseh/retrievable"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
// A is an example structure implementing Retrievable only.
// The goal of A is to return it's value and the ID it was stored with
type A struct {
Value string
// We will mark the ID as not to be stored in either Datastore or Memcache
ID string `datastore:"-" json:"-"`
}
type A_Key_Info struct {
// In this example, the kind that A is stored in
// may change or be prefixed by more information.
VariableKind string
// Another aspect of this example is the complex key
// This key is specifically a string-integer pairing.
KeyPartA string
KeyPartB int
}
// This is the function to implement Retrievable, A in this context
// knows the Kind it is a part of and that it's key is a complex value.
func (a *A) Key(ctx context.Context, key interface{}) *datastore.Key {
// key interface{} in this example is the structure A_Key_Info,
// as this is the only case here, instead of checking the boolean,
// we will let the program panic if key fails to assert.
aki := key.(A_Key_Info)
// Now that we have key in it's complex form we may put it together.
actualKey := fmt.Sprint(aki.KeyPartA, "-", aki.KeyPartB)
actualKind := fmt.Sprint("tableA-", aki.VariableKind)
return datastore.NewKey(ctx, actualKind, actualKey, 0, nil)
}
func Example(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(req)
a := A{}
a.Value = "Example Information"
aki := A_Key_Info{}
aki.VariableKind = "Example Kind"
aki.KeyPartA, aki.KeyPartB = "Key Value", 42
// Now that A implements Retrievable, we may use the associated functions.
retrievable.PlaceInDatastore(ctx, aki, &a)
}