Пример #1
0
// Get loads the entity stored for k into dst, which may be either a struct
// pointer or a Map. If there is no such entity for the key, Get returns
// ErrNoSuchEntity.
//
// The values of dst's unmatched struct fields or Map entries are not modified.
// In particular, it is recommended to pass either a pointer to a zero valued
// struct or an empty Map on each Get call.
//
// ErrFieldMismatch is returned when a field is to be loaded into a different
// type than the one it was stored from, or when a field is missing or
// unexported in the destination struct. ErrFieldMismatch is only returned if
// dst is a struct pointer.
func Get(c appengine.Context, k *Key, dst interface{}) os.Error {
	if !k.valid() {
		return ErrInvalidKey
	}
	req := &pb.GetRequest{
		Key: []*pb.Reference{
			keyToProto(c.AppID(), k),
		},
	}
	res := &pb.GetResponse{}
	err := c.Call("datastore_v3", "Get", req, res)
	if err != nil {
		return err
	}
	if len(res.Entity) == 0 || res.Entity[0].Entity == nil {
		return ErrNoSuchEntity
	}
	if m, ok := dst.(Map); ok {
		return loadMap(m, k, res.Entity[0].Entity)
	}
	sv, err := asStructValue(dst)
	if err != nil {
		return err
	}
	return loadStruct(sv, k, res.Entity[0].Entity)
}
Пример #2
0
// Delete deletes the entity for the given key.
func Delete(c appengine.Context, k *Key) os.Error {
	if !k.valid() {
		return ErrInvalidKey
	}
	req := &pb.DeleteRequest{
		Key: []*pb.Reference{
			keyToProto(c.AppID(), k),
		},
	}
	res := &pb.DeleteResponse{}
	return c.Call("datastore_v3", "Delete", req, res)
}
Пример #3
0
// Put saves the entity src into the datastore with key k. src may be either a
// struct pointer or a Map; if the former then any unexported fields of that
// struct will be skipped.
// If k is an incomplete key, the returned key will be a unique key
// generated by the datastore.
func Put(c appengine.Context, k *Key, src interface{}) (*Key, os.Error) {
	if !k.valid() {
		return nil, ErrInvalidKey
	}
	var e *pb.EntityProto
	if m, ok := src.(Map); ok {
		var err os.Error
		e, err = saveMap(c.AppID(), k, m)
		if err != nil {
			return nil, err
		}
	} else {
		sv, err := asStructValue(src)
		if err != nil {
			return nil, err
		}
		e, err = saveStruct(c.AppID(), k, sv)
		if err != nil {
			return nil, err
		}
	}
	req := &pb.PutRequest{
		Entity: []*pb.EntityProto{e},
	}
	res := &pb.PutResponse{}
	err := c.Call("datastore_v3", "Put", req, res)
	if err != nil {
		return nil, err
	}
	if len(res.Key) == 0 {
		return nil, os.NewError("datastore: internal error: server did not return a key")
	}
	key, err := protoToKey(res.Key[0])
	if err != nil || key.Incomplete() {
		return nil, os.NewError("datastore: internal error: server returned an invalid key")
	}
	return key, nil
}
// fullAppID returns the full AppID for c.
func fullAppID(c appengine.Context) string {
	if c, ok := c.(fullAppIDer); ok {
		return c.FullAppID()
	}
	return c.AppID()
}