// WriteKey encodes a key to the buffer. If context is WithContext, then this // encoded value will include the appid and namespace of the key. func WriteKey(buf Buffer, context KeyContext, k *ds.Key) (err error) { // [appid ++ namespace]? ++ [1 ++ token]* ++ NULL defer recoverTo(&err) appid, namespace, toks := k.Split() if context == WithContext { panicIf(buf.WriteByte(1)) _, e := cmpbin.WriteString(buf, appid) panicIf(e) _, e = cmpbin.WriteString(buf, namespace) panicIf(e) } else { panicIf(buf.WriteByte(0)) } for _, tok := range toks { panicIf(buf.WriteByte(1)) panicIf(WriteKeyTok(buf, tok)) } return buf.WriteByte(0) }
// dsF2R (DS fake-to-real) converts a DSKey back to an SDK *Key. func dsF2R(aeCtx context.Context, k *ds.Key) (*datastore.Key, error) { if k == nil { return nil, nil } // drop aid. _, ns, toks := k.Split() err := error(nil) aeCtx, err = appengine.Namespace(aeCtx, ns) if err != nil { return nil, err } ret := datastore.NewKey(aeCtx, toks[0].Kind, toks[0].StringID, toks[0].IntID, nil) for _, t := range toks[1:] { ret = datastore.NewKey(aeCtx, t.Kind, t.StringID, t.IntID, ret) } return ret, nil }