通过字符串制作Google App Engine数据存储区密钥 [英] Making a Google App Engine datastore key from a string

查看:59
本文介绍了通过字符串制作Google App Engine数据存储区密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将实体"id"作为字符串传递给URL,例如:

I'm passing an entities "id" as a string on a URL, for example:

..../foo/234565

我想在以下查询中使用ID:

I want to use the id in the following query:

//...i hace some code that gets stringId from the URL, and I verified that it works
stringId = ....
theKey, err := datastore.DecodeKey(stringId)
q := datastore.NewQuery("Foo").Filter("__key__ =", theKey)

我得到的错误:

proto: can't skip unknown wire type 7 for datastore.Reference

是否有一种简单的方法可以将stringId转换为"Key"?

Is there a simple way to convert stringId into a "Key"?

推荐答案

c := appengine.NewContext(r)
//Retrieve the entity ID from the submitted form. Convert to an int64
entity_id := r.FormValue("entity_id")
entity_id_int, err := strconv.ParseInt(entity_id, 10, 64) 
if err != nil {
  fmt.Fprint(w, "Unable to parse key")
  return;
}
//We manufacture a datastore key based on the Kind and the 
//entity ID (passed to us via the HTTP request parameter.
key := datastore.NewKey(c, kind, "", entity_id_int, nil)
//Load the Entity this key represents.
//We have to state the variable first, then conduct the Get operation 
//so the datastore understands the struct representing the entity.
var entity CustomStruct
datastore.Get(c, key, &entity)

这篇关于通过字符串制作Google App Engine数据存储区密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆