示例#1
0
// HasParent returns true if key or any of its parents equals
// parent (recursively), otherwise false.
func HasParent(parent, key *datastore.Key) bool {
	for key != nil {
		if key.Equal(parent) {
			return true
		}
		key = key.Parent()
	}
	return false
}
示例#2
0
func buildKey(req *wcg.Request, key *datastore.Key) *datastore.Key {
	kind := entities.FindKind(key.Kind(), key.Namespace())
	if kind == nil {
		return nil
	}
	if key.Parent() != nil {
		return kind.NewKey(req, key.StringID(), buildKey(req, key.Parent()))
	}
	return kind.NewKey(req, key.StringID(), nil)
}
示例#3
0
func fromDatastore(ctx context.Context, key *datastore.Key) (p *Passenger, err error) {
	p = new(Passenger)
	if err = datastore.Get(ctx, key, p.AccessToken); err != nil {
		return
	}
	if p.UserKey = key.Parent(); p.UserKey == nil {
		return nil, ErrTokenNotAssociated
	}
	return
}
示例#4
0
// Load - Takes a datastore.Key provided and loads it into the current Ticket object
func (t *Ticket) Load(ctx context.Context, k datastore.Key) error {
	err := datastore.Get(ctx, &k, t)
	t.DatastoreKey = k
	t.EventKey = k.Parent()

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

	return nil
}
示例#5
0
func fromDatastore(ctx context.Context, key *datastore.Key) (*Passenger, error) {
	p := &Passenger{
		Token: &model.Token{},
	}
	if err := datastore.Get(ctx, key, p.Token); err != nil {
		return nil, err
	}
	if p.User = key.Parent(); p.User == nil {
		return nil, ErrTokenNotAssociated
	}
	return p, nil
}
示例#6
0
文件: ws.go 项目: pbochis/api
func lookup(key *datastore.Key) *websocket.Conn {
	conns.RLock()
	defer conns.RUnlock()

	for key != nil {
		conn, ok := conns.m[ktoi(key)]
		if ok {
			return conn
		}
		key = key.Parent()
	}

	return nil
}
示例#7
0
文件: submission.go 项目: pbochis/api
func nameObject(key *datastore.Key) string {
	name := ""
	for key != nil {
		id := key.StringID()
		if id == "" {
			id = strconv.FormatInt(key.IntID(), 10)
		}
		name = "/" + key.Kind() + "/" + id + name
		key = key.Parent()
	}
	// NOTE: The name of a GCS object must not be prefixed "/",
	// this will give you a major headache.
	return name[1:]
}
示例#8
0
// EncodeToken translates the key and raw secret of a newly generated token to
// a form suitable for the client.
func encodeToken(key *datastore.Key, raw *[tokenLength]byte) (string, error) {
	// Buffer size will be 8 (size of an int64) times the number of keys
	// in the hirarchy plus the length of the raw token itself.
	var b [len(kinds)*8 + tokenLength]byte

	for i := range kinds {
		if n := binary.PutVarint(b[i*8:(i+1)*8], key.IntID()); n < 8 {
			return "", errors.New("short write when encoding token")
		}
		if key != nil {
			key = key.Parent()
		}
	}

	copy(b[len(kinds)*8:len(kinds)*8+tokenLength], raw[:])

	return hex.EncodeToString(b[:]), nil
}
示例#9
0
// dsR2F (DS real-to-fake) converts an SDK Key to a ds.Key
func dsR2F(k *datastore.Key) *ds.Key {
	if k == nil {
		return nil
	}
	aid := k.AppID()
	ns := k.Namespace()

	count := 0
	for nk := k; nk != nil; nk = nk.Parent() {
		count++
	}

	toks := make([]ds.KeyTok, count)

	for ; k != nil; k = k.Parent() {
		count--
		toks[count].Kind = k.Kind()
		toks[count].StringID = k.StringID()
		toks[count].IntID = k.IntID()
	}
	return ds.NewKeyToks(aid, ns, toks)
}
示例#10
0
文件: entity.go 项目: laco0416/goon
func (g *Goon) setStructKey(src interface{}, key *datastore.Key) error {
	v := reflect.ValueOf(src)
	t := v.Type()
	k := t.Kind()

	if k != reflect.Ptr {
		return fmt.Errorf("goon: Expected pointer to struct, got instead: %v", k)
	}

	v = reflect.Indirect(v)
	t = v.Type()
	k = t.Kind()

	if k != reflect.Struct {
		return fmt.Errorf(fmt.Sprintf("goon: Expected struct, got instead: %v", k))
	}

	idSet := false
	kindSet := false
	parentSet := false
	for i := 0; i < v.NumField(); i++ {
		tf := t.Field(i)
		vf := v.Field(i)

		if !vf.CanSet() {
			continue
		}

		tag := tf.Tag.Get("goon")
		tagValues := strings.Split(tag, ",")
		if len(tagValues) > 0 {
			tagValue := tagValues[0]
			if tagValue == "id" {
				if idSet {
					return fmt.Errorf("goon: Only one field may be marked id")
				}

				switch vf.Kind() {
				case reflect.Int64:
					vf.SetInt(key.IntID())
					idSet = true
				case reflect.String:
					vf.SetString(key.StringID())
					idSet = true
				}
			} else if tagValue == "kind" {
				if kindSet {
					return fmt.Errorf("goon: Only one field may be marked kind")
				}
				if vf.Kind() == reflect.String {
					if (len(tagValues) <= 1 || key.Kind() != tagValues[1]) && g.KindNameResolver(src) != key.Kind() {
						vf.Set(reflect.ValueOf(key.Kind()))
					}
					kindSet = true
				}
			} else if tagValue == "parent" {
				if parentSet {
					return fmt.Errorf("goon: Only one field may be marked parent")
				}
				dskeyType := reflect.TypeOf(&datastore.Key{})
				vfType := vf.Type()
				if vfType.ConvertibleTo(dskeyType) {
					vf.Set(reflect.ValueOf(key.Parent()).Convert(vfType))
					parentSet = true
				}
			}
		}
	}

	if !idSet {
		return fmt.Errorf("goon: Could not set id field")
	}

	return nil
}