Ejemplo n.º 1
0
func (q *queryImpl) Ancestor(k ds.Key) ds.Query {
	return q.checkMutateClone(
		func() error {
			if k == nil {
				// SDK has an explicit nil-check
				return errors.New("datastore: nil query ancestor")
			}
			if k.Namespace() != q.ns {
				return fmt.Errorf("bad namespace: %q (expected %q)", k.Namespace(), q.ns)
			}
			if !k.Valid(false, globalAppID, q.ns) {
				// technically the SDK implementation does a Weird Thing (tm) if both the
				// stringID and intID are set on a key; it only serializes the stringID in
				// the proto. This means that if you set the Ancestor to an invalid key,
				// you'll never actually hear about it. Instead of doing that insanity, we
				// just swap to an error here.
				return ds.ErrInvalidKey
			}
			if q.eqFilters["__ancestor__"] != nil {
				return errors.New("cannot have more than one ancestor")
			}
			return nil
		},
		func(q *queryImpl) {
			q.addEqFilt("__ancestor__", ds.MkProperty(k))
		})
}