本文整理汇总了Golang中google/golang.org/cloud/datastore.Client类的典型用法代码示例。如果您正苦于以下问题:Golang Client类的具体用法?Golang Client怎么用?Golang Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: AddTask
// AddTask adds a task with the given description to the datastore,
// returning the key of the newly created entity.
func AddTask(ctx context.Context, client *datastore.Client, desc string) (*datastore.Key, error) {
task := &Task{
Desc: desc,
Created: time.Now(),
}
key := datastore.NewIncompleteKey(ctx, "Task", nil)
return client.Put(ctx, key, task)
}
示例2: newDatastoreDB
// newDatastoreDB creates a new BookDatabase backed by Cloud Datastore.
// See the cloud and google packages for details on creating a suitable context:
// https://godoc.org/google.golang.org/cloud
// https://godoc.org/golang.org/x/oauth2/google
func newDatastoreDB(client *datastore.Client) (BookDatabase, error) {
ctx := context.Background()
// Verify that we can communicate and authenticate with the datastore service.
t, err := client.NewTransaction(ctx)
if err != nil {
return nil, fmt.Errorf("datastoredb: could not connect: %v", err)
}
if err := t.Rollback(); err != nil {
return nil, fmt.Errorf("datastoredb: could not connect: %v", err)
}
return &datastoreDB{
client: client,
}, nil
}
示例3: MarkDone
// [START update_entity]
// MarkDone marks the task done with the given ID.
func MarkDone(ctx context.Context, client *datastore.Client, taskID int64) error {
// Create a key using the given integer ID.
key := datastore.NewKey(ctx, "Task", "", taskID, nil)
// In a transaction load each task, set done to true and store.
_, err := client.RunInTransaction(ctx, func(tx *datastore.Transaction) error {
var task Task
if err := tx.Get(key, &task); err != nil {
return err
}
task.Done = true
_, err := tx.Put(key, &task)
return err
})
return err
}
示例4: ListTasks
// [START retrieve_entities]
// ListTasks returns all the tasks in ascending order of creation time.
func ListTasks(ctx context.Context, client *datastore.Client) ([]*Task, error) {
var tasks []*Task
// Create a query to fetch all queries, ordered by "created".
query := datastore.NewQuery("Task").Order("created")
keys, err := client.GetAll(ctx, query, &tasks)
if err != nil {
return nil, err
}
// Set the id field on each Task from the corresponding key.
for i, key := range keys {
tasks[i].id = key.ID()
}
return tasks, nil
}
示例5: isValidNewConfig
func (fe *FeServiceImpl) isValidNewConfig(ctx context.Context, dsClient *datastore.Client, config *api.BiosphereCreationConfig) (bool, error) {
if config == nil {
return false, nil
}
if config.Name == "" || config.Nx <= 0 || config.Ny <= 0 {
return false, nil
}
// Name must be unique.
qSameName := datastore.NewQuery("BiosphereMeta").Filter("Name =", config.Name)
numSameName, err := dsClient.Count(ctx, qSameName)
if err != nil {
return false, err
}
if numSameName > 0 {
return false, nil
}
return true, nil
}
示例6: DeleteTask
// [START delete_entity]
// DeleteTask deletes the task with the given ID.
func DeleteTask(ctx context.Context, client *datastore.Client, taskID int64) error {
return client.Delete(ctx, datastore.NewKey(ctx, "Task", "", taskID, nil))
}