当前位置: 首页>>代码示例>>Golang>>正文


Golang datastore.Put函数代码示例

本文整理汇总了Golang中google/golang.org/appengine/datastore.Put函数的典型用法代码示例。如果您正苦于以下问题:Golang Put函数的具体用法?Golang Put怎么用?Golang Put使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Put函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestGetUser

// Should return the user with the given username, or throw an Entity not found error
func TestGetUser(t *testing.T) {
	ctx, done, err := aetest.NewContext()
	if err != nil {
		t.Fatal(err)
	}
	defer done()
	key := datastore.NewKey(ctx, "User", "testertim", 0, nil)
	if _, err := datastore.Put(ctx, key, &models.User{FirstName: "Tim"}); err != nil {
		t.Fatal(err)
	}

	user := &models.User{Username: "nonexistent"}
	if err = repositories.GetUser(ctx, user); err == nil || err.Error() != "datastore: no such entity" {
		t.Errorf("Error: %v; want datastore: no such entity", err)
	}

	user.Username = "testertim"
	err = repositories.GetUser(ctx, user)
	if err != nil {
		t.Fatal(err)
	}

	if firstName, want := user.FirstName, "Tim"; firstName != want {
		t.Errorf("User First Name %d, want %d", firstName, want)
	}

	if username, want := user.Username, "testertim"; username != want {
		t.Errorf("Username %d, want %d", username, want)
	}
}
开发者ID:the-weekend-project,项目名称:blogApi,代码行数:31,代码来源:users_test.go

示例2: example6

func example6() {
	type Photo struct {
		URL string
	}
	var ctx context.Context

	// [START ancestor_query_example]
	// Create two Photo entities in the datastore with a Person as their ancestor.
	tomKey := datastore.NewKey(ctx, "Person", "Tom", 0, nil)

	wPhoto := Photo{URL: "http://example.com/some/path/to/wedding_photo.jpg"}
	wKey := datastore.NewKey(ctx, "Photo", "", 0, tomKey)
	_, err := datastore.Put(ctx, wKey, wPhoto)
	// check err

	bPhoto := Photo{URL: "http://example.com/some/path/to/baby_photo.jpg"}
	bKey := datastore.NewKey(ctx, "Photo", "", 0, tomKey)
	_, err = datastore.Put(ctx, bKey, bPhoto)
	// check err

	// Now fetch all Photos that have tomKey as an ancestor.
	// This will populate the photos slice with wPhoto and bPhoto.
	q := datastore.NewQuery("Photo").Ancestor(tomKey)
	var photos []Photo
	_, err = q.GetAll(ctx, &photos)
	// check err
	// do something with photos
	// [END ancestor_query_example]
	_ = err
	_ = photos
}
开发者ID:wuman,项目名称:golang-samples,代码行数:31,代码来源:queries.go

示例3: Increment

// Increment increments the named counter.
func Increment(ctx context.Context, name string) error {
	// Get counter config.
	var cfg counterConfig
	ckey := datastore.NewKey(ctx, configKind, name, 0, nil)
	err := datastore.RunInTransaction(ctx, func(ctx context.Context) error {
		err := datastore.Get(ctx, ckey, &cfg)
		if err == datastore.ErrNoSuchEntity {
			cfg.Shards = defaultShards
			_, err = datastore.Put(ctx, ckey, &cfg)
		}
		return err
	}, nil)
	if err != nil {
		return err
	}
	var s shard
	err = datastore.RunInTransaction(ctx, func(ctx context.Context) error {
		shardName := fmt.Sprintf("%s-shard%d", name, rand.Intn(cfg.Shards))
		key := datastore.NewKey(ctx, shardKind, shardName, 0, nil)
		err := datastore.Get(ctx, key, &s)
		// A missing entity and a present entity will both work.
		if err != nil && err != datastore.ErrNoSuchEntity {
			return err
		}
		s.Name = name
		s.Count++
		_, err = datastore.Put(ctx, key, &s)
		return err
	}, nil)
	if err != nil {
		return err
	}
	memcache.IncrementExisting(ctx, memcacheKey(name), 1)
	return nil
}
开发者ID:wuman,项目名称:golang-samples,代码行数:36,代码来源:counter.go

示例4:

// Put will put this JunitTestResult into Datastore using the given key.
func (ƨ JunitTestResult) Put(ctx context.Context, key *datastore.Key) (*datastore.Key, error) {
	if key != nil {
		return datastore.Put(ctx, key, &ƨ)
	}

	return datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "JunitTestResult", nil), &ƨ)
}
开发者ID:pbochis,项目名称:api,代码行数:8,代码来源:junit_test_result_impl.go

示例5:

// Put will put this Challenge into Datastore using the given key.
func (ƨ Challenge) Put(ctx context.Context, key *datastore.Key) (*datastore.Key, error) {
	if key != nil {
		return datastore.Put(ctx, key, &ƨ)
	}

	return datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "Challenge", nil), &ƨ)
}
开发者ID:pbochis,项目名称:api,代码行数:8,代码来源:challenge_impl.go

示例6:

// Put will put this Submission into Datastore using the given key.
func (ƨ Submission) Put(ctx context.Context, key *datastore.Key) (*datastore.Key, error) {
	if key != nil {
		return datastore.Put(ctx, key, &ƨ)
	}

	return datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "Submission", nil), &ƨ)
}
开发者ID:pbochis,项目名称:api,代码行数:8,代码来源:submission_impl.go

示例7: Increment

// Increment increments the named counter.
func Increment(c context.Context, valName string) error {

	// Get counter config.
	shardsTotal := dsu.WrapInt{}
	dsu.McacheGet(c, mcKeyShardsTotal(valName), &shardsTotal)
	if shardsTotal.I < 1 {
		ckey := datastore.NewKey(c, dsKindNumShards, mcKeyShardsTotal(valName), 0, nil)
		errTx := datastore.RunInTransaction(c,
			func(c context.Context) error {
				err := datastore.Get(c, ckey, &shardsTotal)
				if err == datastore.ErrNoSuchEntity {
					shardsTotal.I = defaultNumShards
					_, err = datastore.Put(c, ckey, &shardsTotal)
				}
				return err
			}, nil)
		if errTx != nil {
			return errTx
		}
		dsu.McacheSet(c, mcKeyShardsTotal(valName), dsu.WrapInt{shardsTotal.I})
	}

	// pick random counter and increment it
	errTx := datastore.RunInTransaction(c,
		func(c context.Context) error {
			shardId := rand.Intn(shardsTotal.I)
			dsKey := datastore.NewKey(c, dsKindShard, keySingleShard(valName, shardId), 0, nil)
			var sd WrapShardData
			err := datastore.Get(c, dsKey, &sd)
			// A missing entity and a present entity will both work.
			if err != nil && err != datastore.ErrNoSuchEntity {
				return err
			}
			sd.Name = valName
			sd.ShardId = shardId
			sd.I++
			_, err = datastore.Put(c, dsKey, &sd)
			if ll > 2 {
				aelog.Infof(c, "ds put %v %v", dsKey, sd)
			}
			return err
		}, nil)
	if errTx != nil {
		return errTx
	}

	memcache.Increment(c, mcKey(valName), 1, 0)

	// collect number of updates
	//    per valName per instance in memory
	//    for every interval of 10 minutes
	//
	//  a batch job checks if the number of shards should be increased or decreased
	//    and truncates this map
	updateSamplingFrequency[valName+util.TimeMarker()[:len("2006-01-02 15:0")]] += 1

	return nil
}
开发者ID:aarzilli,项目名称:tools,代码行数:59,代码来源:sharded_counter.go

示例8: SaveBoardState

// SaveBoardState commits the state to the datastore
func SaveBoardState(c context.Context, state *BoardState) (*BoardState, error) {
	sessionKey, err := datastore.DecodeKey(state.SessionID)
	if err != nil {
		return nil, err
	}
	stateModel := &boardStateModel{}
	stateModel.parentKey = sessionKey
	stateModel.lastModified = time.Now()
	stateModel.key, err = datastore.Put(c, datastore.NewIncompleteKey(c, "BoardState", stateModel.parentKey), stateModel)
	if err != nil {
		return nil, err
	}

	// Initialize the result
	result := &BoardState{
		ID:           stateModel.key.Encode(),
		SessionID:    stateModel.parentKey.Encode(),
		LastModified: time.Now(),
		Players:      make(map[string]*Player),
	}

	// Save the players
	for k, v := range state.Players {
		p := &playerModel{
			Name:     k,
			Location: v.Location,
		}
		p.parentKey = stateModel.key
		p.key, err = datastore.Put(c, datastore.NewIncompleteKey(c, "PlayerState", p.parentKey), p)
		if err != nil {
			return nil, err
		}

		for _, card := range v.Hand {
			cardModel := &cardModel{
				Name:  card.Name,
				Color: card.Color,
			}
			cardModel.parentKey = p.key
			cardModel.key, err = datastore.Put(c, datastore.NewIncompleteKey(c, "PlayerCard", p.parentKey), cardModel)
			if err != nil {
				return nil, err
			}

		}

		// Added player to result
		result.Players[k] = &Player{
			Location: p.Location,
			Hand:     v.Hand,
		}
	}

	return result, nil
}
开发者ID:jward6,项目名称:gopandemus,代码行数:56,代码来源:datastore.go

示例9:

// Save will put this JunitSubmission into Datastore using the given key.
func (ƨ JunitSubmission) Save(ctx context.Context, key ...*datastore.Key) (*datastore.Key, error) {
	if len(key) > 1 {
		panic("zero or one key expected")
	}

	if len(key) == 1 && key[0] != nil {
		return datastore.Put(ctx, key[0], &ƨ)
	}

	return datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "JunitSubmission", nil), &ƨ)
}
开发者ID:tudorgergely,项目名称:api,代码行数:12,代码来源:junit_submission_impl.go

示例10: Save

// Save will put this AccessToken into Datastore using the given key.
func (x AccessToken) Save(ctx context.Context, key ...*datastore.Key) (*datastore.Key, error) {
	if len(key) > 1 {
		panic("zero or one key expected")
	}

	if len(key) == 1 && key[0] != nil {
		return datastore.Put(ctx, key[0], &x)
	}

	return datastore.Put(ctx, datastore.NewIncompleteKey(ctx, AccessTokenKind, nil), &x)
}
开发者ID:pbochis,项目名称:engine,代码行数:12,代码来源:access_token.go

示例11: example6

func example6() {
	// [START parent]
	// Create Employee entity
	employee := &Employee{ /* ... */ }
	employeeKey, err := datastore.Put(ctx, datastore.NewIncompleteKey(ctx, "Employee", nil), employee)

	// Use Employee as Address entity's parent
	// and save Address entity to datastore
	address := &Address{ /* ... */ }
	addressKey := datastore.NewIncompleteKey(ctx, "Address", employeeKey)
	_, err = datastore.Put(ctx, addressKey, address)
	// [END parent]
	_ = err
}
开发者ID:GoogleCloudPlatform,项目名称:golang-samples,代码行数:14,代码来源:entities.go

示例12: uploadHandler

func uploadHandler(w http.ResponseWriter, r *http.Request) {

	//ducapple name

	name := r.FormValue("fileName")
	file, _, err := r.FormFile("uploadFile")
	if err != nil {
		//add error handling
		return
	}
	defer file.Close()
	b, err := ioutil.ReadAll(file)
	if err != nil {
	}

	c := appengine.NewContext(r)
	// get user data
	u, err := getUser(r)
	if err != nil {
	}

	key := datastore.NewKey(c, "File", u.UserKey+"/"+name, 0, nil)
	rtn := File{}
	if err = datastore.Get(c, key, &rtn); err != nil {
		if err != datastore.ErrNoSuchEntity {
			return
		}
	} else {
		u.Size -= int64(len(rtn.Data))
	}

	f := File{
		UserKey: u.UserKey,
		Data:    b,
	}
	u.Size += int64(len(b))
	lu := user.Current(c)

	_, err = datastore.Put(c, datastore.NewKey(c, "User", lu.ID, 0, nil), u)
	if err != nil {
	}

	// add empty slide data
	_, err = datastore.Put(c, key, &f)
	if err != nil {
	}

	http.Redirect(w, r, "/me/file/view", 301)
}
开发者ID:shizuokago,项目名称:gopredit,代码行数:49,代码来源:file.go

示例13: newPlayer

func newPlayer(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	id := r.FormValue("id")
	if id == "" {
		http.Error(w, "need a non-blank id from the form data", http.StatusBadRequest)
		return
	}

	k := datastore.NewKey(ctx, "Player", id, 0, nil)
	err := datastore.RunInTransaction(ctx, func(ctx context.Context) error {
		var p *Player
		if err := datastore.Get(ctx, k, p); err != datastore.ErrNoSuchEntity {
			return codeErr{fmt.Errorf("player(%s) already exists", id), http.StatusConflict}
		}

		// TODO(jaguilar): Add another system. Add a system Registry. Leagues
		// can have ratings.
		p = &Player{Rating: eloSys.InitialRating(), key: k}
		if _, err := datastore.Put(ctx, p.key, p); err != nil {
			return err
		}
		return nil
	}, nil)
	if err != nil {
		httpError(w, err)
		return
	}
	fmt.Fprintf(
		w,
		`<html><head><meta http-equiv="refresh" content="5,/"></head>
		<body>Successfully added %s. Redirecting you in five seconds.</body></html>`,
		id)
}
开发者ID:jaguilar,项目名称:rating,代码行数:34,代码来源:service.go

示例14: Get

func Get(c context.Context, key, def string, values ...interface{}) string {
	configLock.Lock()
	defer configLock.Unlock()
	val, ok := configCache[key]
	if ok {
		return fmt.Sprintf(val, values...)
	}
	value := Value{}
	dskey := datastore.NewKey(c, ConfigName, key, 0, nil)
	err := datastore.Get(c, dskey, &value)
	if err == datastore.ErrNoSuchEntity {
		value.Val = def
		log.Infof(c, "Creating default config for key - %s - default value is - %s", key, value.Val)
		_, err = datastore.Put(c, dskey, &value)
		if err != nil {
			log.Errorf(c, "Error creating default config for key - %s - error is - %v", key, err)
		}
		return fmt.Sprintf(def, values...) // return default, totally new config setting
	}
	if err != nil {
		log.Errorf(c, "Error fetching config for key - %s - error is - %v", key, err)
		return def // error, return the default
	}
	configCache[key] = value.Val
	return fmt.Sprintf(value.Val, values...)
}
开发者ID:mzimmerman,项目名称:appengineconfig,代码行数:26,代码来源:appengineconfig.go

示例15: Create

func (s *UserService) Create(ctx context.Context, u *User) error {
	span := trace.FromContext(ctx).NewChild("trythings.user.Create")
	defer span.Finish()

	// TODO Make sure u.GoogleID == user.Current(ctx).ID

	if u.ID != "" {
		return fmt.Errorf("u already has id %q", u.ID)
	}

	if u.CreatedAt.IsZero() {
		u.CreatedAt = time.Now()
	}

	id, _, err := datastore.AllocateIDs(ctx, "User", nil, 1)
	if err != nil {
		return err
	}
	u.ID = fmt.Sprintf("%x", id)

	rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
	k := datastore.NewKey(ctx, "User", u.ID, 0, rootKey)
	k, err = datastore.Put(ctx, k, u)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:trythings,项目名称:trythings,代码行数:29,代码来源:user.go


注:本文中的google/golang.org/appengine/datastore.Put函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。