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


Golang datastore.NewKey函数代码示例

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


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

示例1: serveDocumentsUpdate

func serveDocumentsUpdate(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	serveAPI(res, req, func() interface{} {
		ctx := appengine.NewContext(req)
		session, _ := sessionStore.Get(req, "session")
		email, ok := session.Values["email"].(string)
		if !ok {
			return HTTPError{403, "access denied"}
		}

		var document Document
		err := json.NewDecoder(req.Body).Decode(&document)
		if err != nil {
			return err
		}
		document.ID = params.ByName("id")

		userKey := datastore.NewKey(ctx, "User", email, 0, nil)
		docKey := datastore.NewKey(ctx, "Document", params.ByName("id"), 0, userKey)
		var originalDocument Document
		err = datastore.Get(ctx, docKey, &originalDocument)
		if err != nil {
			return err
		}

		_, err = datastore.Put(ctx, docKey, &document)
		if err != nil {
			return err
		}

		return document
	})
}
开发者ID:SiroDiaz,项目名称:csuf,代码行数:32,代码来源:api.go

示例2: serveDocumentsDelete

func serveDocumentsDelete(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	serveAPI(res, req, func() interface{} {
		ctx := appengine.NewContext(req)
		session, _ := sessionStore.Get(req, "session")

		email, ok := session.Values["email"].(string)
		if !ok {
			return HTTPError{403, "access denied"}
		}

		id := params.ByName("id")
		userKey := datastore.NewKey(ctx, "User", email, 0, nil)
		docKey := datastore.NewKey(ctx, "Document", id, 0, userKey)
		var document Document
		err := datastore.Get(ctx, docKey, &document)
		if err != nil {
			return err
		}
		err = datastore.Delete(ctx, docKey)
		if err != nil {
			return err
		}
		return true
	})
}
开发者ID:SiroDiaz,项目名称:csuf,代码行数:25,代码来源:api.go

示例3: 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)
	}

}
开发者ID:the-information,项目名称:ori,代码行数:60,代码来源:process_test.go

示例4: serveDocumentsCreate

func serveDocumentsCreate(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	serveAPI(res, req, func() interface{} {
		ctx := appengine.NewContext(req)
		session, _ := sessionStore.Get(req, "session")

		email, ok := session.Values["email"].(string)
		if !ok {
			return HTTPError{403, "access denied"}
		}

		var document Document
		err := json.NewDecoder(req.Body).Decode(&document)
		if err != nil {
			return err
		}
		if document.ID != "" {
			return fmt.Errorf("invalid document: id must not be set")
		}
		document.ID = uuid.NewV1().String()
		userKey := datastore.NewKey(ctx, "User", email, 0, nil)
		docKey := datastore.NewKey(ctx, "Document", document.ID, 0, userKey)
		docKey, err = datastore.Put(ctx, docKey, &document)
		if err != nil {
			return err
		}
		return document
	})
}
开发者ID:SiroDiaz,项目名称:csuf,代码行数:28,代码来源:api.go

示例5: ByID

func (s *SpaceService) ByID(ctx context.Context, id string) (*Space, error) {
	rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
	k := datastore.NewKey(ctx, "Space", id, 0, rootKey)

	csp, ok := CacheFromContext(ctx).Get(k).(*Space)
	if ok {
		return csp, nil
	}
	span := trace.FromContext(ctx).NewChild("trythings.space.ByID")
	defer span.Finish()

	var sp Space
	err := datastore.Get(ctx, k, &sp)
	if err != nil {
		return nil, err
	}

	ok, err = s.IsVisible(ctx, &sp)
	if err != nil {
		return nil, err
	}
	if !ok {
		return nil, errors.New("cannot access space")
	}

	CacheFromContext(ctx).Set(k, &sp)
	return &sp, nil
}
开发者ID:trythings,项目名称:trythings,代码行数:28,代码来源:space.go

示例6: 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

示例7: decodeKey

func decodeKey(c context.Context, v interface{}) (*datastore.Key, error) {
	var result, ancestor *datastore.Key
	p, ok := v.([]interface{})
	if !ok {
		return nil, ErrInvalidKeyElement
	}

	for i := 0; i < len(p); i += 2 {
		kind := p[i].(string)
		id := p[i+1]
		switch id.(type) {
		case string:
			result = datastore.NewKey(c, kind, id.(string), 0, ancestor)
		case json.Number:
			n, err := id.(json.Number).Int64()
			if err != nil {
				return nil, invalidIDError(id)
			}
			result = datastore.NewKey(c, kind, "", n, ancestor)
		default:
			return nil, invalidIDError(id)
		}

		ancestor = result
	}

	return result, nil
}
开发者ID:ronoaldo,项目名称:aetools,代码行数:28,代码来源:marshal.go

示例8: newSceneProcess

func newSceneProcess(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	ctx := appengine.NewContext(req)
	sd := sessionInfo(req)

	t := time.Now()
	time := t.Local()
	s := fmt.Sprintf("%v", time)

	scene := Scene{
		Author:      sd.Username,
		Name:        req.FormValue("name"),
		Content:     req.FormValue("scene"),
		CreatedDate: s,
	}

	storykey := datastore.NewKey(ctx, "Stories", req.FormValue("story"), 0, nil)
	key := datastore.NewKey(ctx, "Scenes", req.FormValue("name"), 0, storykey) //owner is ancestor - eliminates need for owner-story table
	key, err := datastore.Put(ctx, key, &scene)
	if err != nil {
		log.Errorf(ctx, "error adding todo: %v", err)
		http.Error(res, err.Error(), 500)
		return
	}

	// redirect
	link := strings.Replace(req.FormValue("story"), " ", "-", -1)
	http.Redirect(res, req, "/view/"+link+"/"+sd.Username, 302)
}
开发者ID:jennifergarner,项目名称:CS191T,代码行数:28,代码来源:scenes.go

示例9: Update

func (s *SearchService) Update(ctx context.Context, se *Search) error {
	span := trace.FromContext(ctx).NewChild("trythings.search.Update")
	defer span.Finish()

	if se.ID == "" {
		return errors.New("cannot update search with no ID")
	}

	// Make sure we have access to the search before it was modified.
	_, err := s.ByID(ctx, se.ID)
	if err != nil {
		return err
	}

	// Make sure we continue to have access to the task after our update.
	ok, err := s.IsVisible(ctx, se)
	if err != nil {
		return err
	}

	if !ok {
		return errors.New("cannot update search to lose access")
	}

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

	CacheFromContext(ctx).Set(k, se)
	return nil
}
开发者ID:trythings,项目名称:trythings,代码行数:34,代码来源:search.go

示例10: 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

示例11: viewStory

func viewStory(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {
	ctx := appengine.NewContext(req)
	sd := sessionInfo(req)

	link := ps.ByName("story")
	title := strings.Replace(link, "-", " ", -1) //translate link to title
	owner := ps.ByName("owner")

	userkey := datastore.NewKey(ctx, "Users", owner, 0, nil)
	key := datastore.NewKey(ctx, "Stories", title, 0, userkey) //owner is ancestor - eliminates need for owner-story table
	var story Story
	err := datastore.Get(ctx, key, &story)
	if err != nil {
		panic(err)
	}
	var user User
	user.Username = owner
	err = datastore.Get(ctx, userkey, &user)
	if err != nil {
		panic(err)
	}

	sd.ViewingStory = story
	sd.ViewingUser = user
	tpl.ExecuteTemplate(res, "view.html", &sd)
}
开发者ID:sgkadle,项目名称:AdvWebProg,代码行数:26,代码来源:stories.go

示例12: ByID

func (s *ViewService) ByID(ctx context.Context, id string) (*View, error) {
	rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
	k := datastore.NewKey(ctx, "View", id, 0, rootKey)

	cv, ok := CacheFromContext(ctx).Get(k).(*View)
	if ok {
		return cv, nil
	}
	span := trace.FromContext(ctx).NewChild("trythings.view.ByID")
	defer span.Finish()

	var v View
	err := datastore.Get(ctx, k, &v)
	if err != nil {
		return nil, err
	}

	ok, err = s.IsVisible(ctx, &v)
	if err != nil {
		return nil, err
	}
	if !ok {
		return nil, errors.New("cannot access view")
	}

	CacheFromContext(ctx).Set(k, &v)
	return &v, nil
}
开发者ID:trythings,项目名称:trythings,代码行数:28,代码来源:view.go

示例13: index

func index(res http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		name := req.FormValue("name")
		descrip := req.FormValue("descrip")

		ctx := appengine.NewContext(req)
		parentKey := datastore.NewKey(ctx, "House", "Garage", 0, nil)
		key := datastore.NewKey(ctx, "Tools", name, 0, parentKey)
		// 1 write / second
		// We will eventually use memcache to buffer our writes

		entity := &Tool{
			Name:        name,
			Description: descrip,
		}

		_, err := datastore.Put(ctx, key, entity)
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
	}
	res.Header().Set("Content-Type", "text/html")
	fmt.Fprintln(res, `
		<form method="POST">
			<h1>Tool</h1>
			<input type="text" name="name"><br>
			<h1>Descrip</h1>
			<textarea name="descrip"></textarea>
			<input type="submit">
		</form>`)
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:32,代码来源:tools.go

示例14: newStoryProcess

func newStoryProcess(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	ctx := appengine.NewContext(req)
	sd := sessionInfo(req)

	t := time.Now()
	// y, m, d := t.Date()
	time := t.UTC()
	s := fmt.Sprintf("%v", time)

	story := Story{
		Owner:       sd.Username,
		Title:       req.FormValue("story"),
		Description: req.FormValue("description"),
		Link:        strings.Replace(req.FormValue("story"), " ", "-", -1),
		CreatedDate: s,
	}

	userkey := datastore.NewKey(ctx, "Users", sd.Username, 0, nil)
	key := datastore.NewKey(ctx, "Stories", story.Title, 0, userkey) //owner is ancestor - eliminates need for owner-story table
	key, err := datastore.Put(ctx, key, &story)
	if err != nil {
		log.Errorf(ctx, "error adding todo: %v", err)
		http.Error(res, err.Error(), 500)
		return
	}

	// redirect
	http.Redirect(res, req, "/browse", 302)
}
开发者ID:sgkadle,项目名称:AdvWebProg,代码行数:29,代码来源:stories.go

示例15: ByID

func (s *TaskService) ByID(ctx context.Context, id string) (*Task, error) {
	rootKey := datastore.NewKey(ctx, "Root", "root", 0, nil)
	k := datastore.NewKey(ctx, "Task", id, 0, rootKey)

	ct, ok := CacheFromContext(ctx).Get(k).(*Task)
	if ok {
		return ct, nil
	}
	span := trace.FromContext(ctx).NewChild("trythings.task.ByID")
	defer span.Finish()

	var t Task
	err := datastore.Get(ctx, k, &t)
	if err != nil {
		return nil, err
	}

	ok, err = s.IsVisible(ctx, &t)
	if err != nil {
		return nil, err
	}

	if !ok {
		return nil, errors.New("cannot access task")
	}

	CacheFromContext(ctx).Set(k, &t)
	return &t, nil
}
开发者ID:trythings,项目名称:trythings,代码行数:29,代码来源:task.go


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