本文整理匯總了Golang中github.com/qedus/nds.Get函數的典型用法代碼示例。如果您正苦於以下問題:Golang Get函數的具體用法?Golang Get怎麽用?Golang Get使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Get函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestDeleteInTransaction
func TestDeleteInTransaction(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "TestEntity", "", 1, nil)
if _, err := nds.Put(c, key, &testEntity{2}); err != nil {
t.Fatal(err)
}
// Prime cache.
if err := nds.Get(c, key, &testEntity{}); err != nil {
t.Fatal(err)
}
if err := nds.RunInTransaction(c, func(tc context.Context) error {
return nds.DeleteMulti(tc, []*datastore.Key{key})
}, nil); err != nil {
t.Fatal(err)
}
if err := nds.Get(c, key, &testEntity{}); err == nil {
t.Fatal("expected no entity")
} else if err != datastore.ErrNoSuchEntity {
t.Fatal(err)
}
}
示例2: TestProcess
func TestProcess(t *testing.T) {
ctx, done, _ := aetest.NewContext()
defer done()
smallTest := strings.NewReader(`{
"User/jsmith": {
"Name": "John Smith",
"LotteryNumbers": [1,2,3,4,5],
"CreatedAt": {
"Type": "time",
"Value": "1993-05-01T12:31:00.000Z"
}
},
"User/jdoe": {
"Name": "Jane Doe",
"LotteryNumbers": [2,4,6,8,10],
"CreatedAt": {
"Type": "time",
"Value": "1992-01-30T08:01:00.000Z"
}
}
}`)
if err := Process(ctx, smallTest); err != nil {
t.Errorf("Unexpected error %s", err)
}
key1 := datastore.NewKey(ctx, "User", "jdoe", 0, nil)
key2 := datastore.NewKey(ctx, "User", "jsmith", 0, nil)
value1 := fakeUser{}
value2 := fakeUser{}
if err := nds.Get(ctx, key1, &value1); err != nil {
t.Errorf("Unexpected error %s retrieving value1", err)
} else if err := nds.Get(ctx, key2, &value2); err != nil {
t.Errorf("Unexpected error %s retrieving value2", err)
}
value1.CreatedAt = value1.CreatedAt.UTC()
value2.CreatedAt = value2.CreatedAt.UTC()
if !reflect.DeepEqual(value1, fakeUser{
Name: "Jane Doe",
LotteryNumbers: []int64{2, 4, 6, 8, 10},
CreatedAt: time.Date(1992, 1, 30, 8, 1, 0, 0, time.UTC),
}) {
t.Errorf("Unexpected value in value1: %+v", value1)
}
if !reflect.DeepEqual(value2, fakeUser{
Name: "John Smith",
LotteryNumbers: []int64{1, 2, 3, 4, 5},
CreatedAt: time.Date(1993, 5, 1, 12, 31, 0, 0, time.UTC),
}) {
t.Errorf("Unexpected value in value2: %+v", value1)
}
}
示例3: TestGetArgs
func TestGetArgs(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int64
}
if err := nds.Get(c, nil, &testEntity{}); err == nil {
t.Fatal("expected error for nil key")
}
key := datastore.NewKey(c, "Entity", "", 1, nil)
if err := nds.Get(c, key, nil); err != datastore.ErrInvalidEntityType {
t.Fatal("expected ErrInvalidEntityType for nil value")
}
if err := nds.Get(c, key, datastore.PropertyList{}); err == nil {
t.Fatal("expected error for datastore.PropertyList")
}
if err := nds.Get(c, key, testEntity{}); err == nil {
t.Fatal("expected error for struct")
}
rte := newReaderTestEntity()
if err := nds.Get(c, key, rte); err == nil {
t.Fatal("expected error for interface")
}
}
示例4: TestGetNamespacedKey
// TestGetNamespacedKey ensures issue https://goo.gl/rXU8nK is fixed so that
// memcache uses the namespace from the key instead of the context.
func TestGetNamespacedKey(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
const intVal = int64(12)
type testEntity struct {
IntVal int64
}
namespacedCtx, err := appengine.Namespace(c, "keyNamespace")
if err != nil {
t.Fatal(err)
}
key := datastore.NewKey(c, "Entity", "", 1, nil)
namespacedKey := datastore.NewKey(namespacedCtx,
"Entity", "", key.IntID(), nil)
entity := &testEntity{intVal}
if namespacedKey, err = nds.Put(c, namespacedKey, entity); err != nil {
t.Fatal(err)
}
// Prime cache.
if err := nds.Get(namespacedCtx, namespacedKey, &testEntity{}); err != nil {
t.Fatal(err)
}
// Ensure that we get a value back from the cache by checking if the
// datastore is called at all.
entityFromCache := true
nds.SetDatastoreGetMulti(func(c context.Context,
keys []*datastore.Key, vals interface{}) error {
if len(keys) != 0 {
entityFromCache = false
}
return nil
})
if err := nds.Get(c, namespacedKey, &testEntity{}); err != nil {
t.Fatal(err)
}
nds.SetDatastoreGetMulti(datastore.GetMulti)
if !entityFromCache {
t.Fatal("entity not obtained from cache")
}
if err := nds.Delete(namespacedCtx, namespacedKey); err != nil {
t.Fatal(err)
}
entity = &testEntity{}
if err := nds.Get(c, namespacedKey, entity); err == nil {
t.Fatalf("expected no such entity error but got %+v", entity)
} else if err != datastore.ErrNoSuchEntity {
t.Fatal(err)
}
}
示例5: New
// New creates and returns a new blank account. It returns an error if an account
// with the specified email address already exists.
func New(ctx context.Context, email, password string) (*Account, error) {
account := new(Account)
account.Email = email
account.CreatedAt = time.Now()
if err := account.SetPassword(password); err != nil {
return nil, err
}
err := nds.RunInTransaction(ctx, func(txCtx context.Context) error {
dsKey := account.Key(txCtx)
if err := nds.Get(txCtx, dsKey, account); err == nil {
return ErrAccountExists
} else if err != datastore.ErrNoSuchEntity {
return err
}
_, err := nds.Put(txCtx, dsKey, account)
return err
}, nil)
if err != nil {
return nil, err
}
account.flag = camethroughus
account.originalEmail = email
return account, nil
}
示例6: TestMemcacheNamespace
func TestMemcacheNamespace(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVal int
}
// Illegal namespace chars.
nds.SetMemcacheNamespace("£££")
key := datastore.NewKey(c, "Entity", "", 1, nil)
if err := nds.Get(c, key, &testEntity{}); err == nil {
t.Fatal("expected namespace error")
}
if _, err := nds.Put(c, key, &testEntity{}); err == nil {
t.Fatal("expected namespace error")
}
if err := nds.Delete(c, key); err == nil {
t.Fatal("expected namespace error")
}
if err := nds.RunInTransaction(c, func(tc context.Context) error {
return nil
}, nil); err == nil {
t.Fatal("expected namespace error")
}
nds.SetMemcacheNamespace("")
}
示例7: Next
// Next processes the next item
func (x *example6) Next(c context.Context, counters mapper.Counters, key *datastore.Key) error {
// we need to load the entity ourselves
photo := new(Photo)
if err := nds.Get(c, key, photo); err != nil {
return err
}
photo.ID = key.IntID()
suffix := photo.Taken.Format("20060102")
_, err := x.bq.Tabledata.InsertAll(x.appID, "datasetName", "tableName", &bigquery.TableDataInsertAllRequest{
TemplateSuffix: suffix,
Rows: []*bigquery.TableDataInsertAllRequestRows{
{
Json: map[string]bigquery.JsonValue{
"id": photo.ID,
"taken": photo.Taken,
"photographer": map[string]bigquery.JsonValue{
"id": photo.Photographer.ID,
"name": photo.Photographer.Name,
},
},
},
},
}).Context(c).Do()
return err
}
示例8: TestClearNamespacedLocks
// TestClearNamespacedLocks tests to make sure that locks are cleared when
// RunInTransaction is using a namespace.
func TestClearNamespacedLocks(t *testing.T) {
c, closeFunc := NewContext(t, nil)
defer closeFunc()
c, err := appengine.Namespace(c, "testnamespace")
if err != nil {
t.Fatal(err)
}
type testEntity struct {
Val int
}
key := datastore.NewKey(c, "TestEntity", "", 1, nil)
// Prime cache.
if err := nds.Get(c, key, &testEntity{}); err == nil {
t.Fatal("expected no such entity")
} else if err != datastore.ErrNoSuchEntity {
t.Fatal(err)
}
if err := nds.RunInTransaction(c, func(tc context.Context) error {
if err := nds.Get(tc, key, &testEntity{}); err == nil {
return errors.New("expected no such entity")
} else if err != datastore.ErrNoSuchEntity {
return err
}
if _, err := nds.Put(tc, key, &testEntity{3}); err != nil {
return err
}
return nil
}, nil); err != nil {
t.Fatal(err)
}
entity := &testEntity{}
if err := nds.Get(c, key, entity); err != nil {
t.Fatal(err)
}
if entity.Val != 3 {
t.Fatal("incorrect val")
}
}
示例9: retrieve
// retrieve obtains the application configuration as a datastore.PropertyList.
func retrieve(ctx context.Context) (datastore.PropertyList, error) {
p := datastore.PropertyList(make([]datastore.Property, 0, 8))
key := datastore.NewKey(ctx, Entity, Entity, 0, nil)
err := nds.Get(ctx, key, &p)
return p, err
}
示例10: Next
// Next processes the next item
func (x *example1) Next(c context.Context, counters mapper.Counters, key *datastore.Key) error {
// we need to load the entity ourselves
photo := new(Photo)
if err := nds.Get(c, key, photo); err != nil {
return err
}
counters.Increment(photo.Photographer.Name, 1)
return nil
}
示例11: HasChanged
// HasChanged checks the current state of an account in the datastore. It returns
// true if the saved version of the account has diverged from the state of the account
// as described in account.
func HasChanged(ctx context.Context, account *Account) (bool, error) {
var currentState Account
key := account.Key(ctx)
if err := nds.Get(ctx, key, ¤tState); err != nil {
return false, err
} else {
return reflect.DeepEqual(*account, currentState), nil
}
}
示例12: Get
// Get retrieves the account identified by email and stores it in
// the value pointed to by account.
func Get(ctx context.Context, email string, account *Account) error {
if err := nds.Get(ctx, datastore.NewKey(ctx, Entity, email, 0, nil), account); err != nil {
return err
} else {
account.flag = camethroughus
account.originalEmail = account.Email
return nil
}
}
示例13: Next
// Next processes the next item
func (x *example3) Next(c context.Context, counters mapper.Counters, key *datastore.Key) error {
photo := new(Photo)
if err := nds.Get(c, key, photo); err != nil {
log.Errorf(c, err.Error())
return err
}
photo.ID = key.IntID()
counters.Increment(photo.Photographer.Name, 1)
return nil
}
示例14: Save
// Save changes the application configuration to
// the values in conf. All HTTP requests subsequent to this one
// are guaranteed to use the new values in their configuration.
//
// Note that subsequent calls to Get with the same request context
// will continue to retrieve the old version of the configuration.
//
// As a special case, calling Save with a *config.Config will replace
// the entire contents of the configuration with the contents of Config.
func Save(ctx context.Context, conf interface{}) error {
if typedConfig, ok := conf.(*Config); ok {
pl := datastore.PropertyList(*typedConfig)
replaceKey := datastore.NewKey(ctx, Entity, Entity, 0, nil)
_, replaceErr := nds.Put(ctx, replaceKey, &pl)
return replaceErr
}
return datastore.RunInTransaction(ctx, func(txCtx context.Context) error {
props := datastore.PropertyList{}
key := datastore.NewKey(txCtx, Entity, Entity, 0, nil)
if err := nds.Get(txCtx, key, &props); err != nil && err != datastore.ErrNoSuchEntity {
return err
}
// merge existing config with the new values
if newProps, err := datastore.SaveStruct(conf); err != nil {
return err
} else {
for _, newProp := range newProps {
newProp.NoIndex = true
replacing := false
for _, prop := range props {
// make sure NoIndex is set
prop.NoIndex = true
if prop.Name == newProp.Name {
replacing = true
prop.Value = newProp.Value
break
}
}
if !replacing {
// append
props = append(props, newProp)
}
}
}
_, err := nds.Put(txCtx, key, &props)
return err
}, nil)
}
示例15: TestGetSliceProperty
func TestGetSliceProperty(t *testing.T) {
c, closeFunc := NewContext(t)
defer closeFunc()
type testEntity struct {
IntVals []int64
}
key := datastore.NewKey(c, "Entity", "", 1, nil)
intVals := []int64{0, 1, 2, 3}
val := &testEntity{intVals}
if _, err := nds.Put(c, key, val); err != nil {
t.Fatal(err)
}
// Get from datastore.
newVal := &testEntity{}
if err := nds.Get(c, key, newVal); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(val.IntVals, intVals) {
t.Fatal("slice properties not equal", val.IntVals)
}
// Get from memcache.
newVal = &testEntity{}
if err := nds.Get(c, key, newVal); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(val.IntVals, intVals) {
t.Fatal("slice properties not equal", val.IntVals)
}
}