Exemple #1
0
// returns true iff the key is part of the current library
func (self *context) isInLibrary(key *datastore.Key) bool {
	// check if the library is an ancestor of this key
	for key != nil {
		if self.lid.Equal(key) {
			return true
		}
		key = key.Parent()
	}
	return false
}
Exemple #2
0
func FromGAE(k *datastore.Key) (result key.Key, err error) {
	if k == nil {
		return key.Key(""), nil
	}
	parent, err := FromGAE(k.Parent())
	if err != nil {
		return
	}
	return key.New(k.Kind(), k.StringID(), k.IntID(), parent)
}
Exemple #3
0
// loop through the results, adding them to a two level map keyed on the entity
//  kind (Dish,Ingredient) and then the item itself, counting how many
// times we come across each item
func addResult(key *datastore.Key, results map[string]map[string]uint) {
	parent := key.Parent()
	parentEnc := parent.Encode()
	if kind, ok := results[parent.Kind()]; ok {
		if _, ok := kind[parentEnc]; ok {
			kind[parentEnc] += 1
		} else {
			kind[parentEnc] = 1
		}
	} else {
		results[parent.Kind()] = make(map[string]uint)
		results[parent.Kind()][parentEnc] = 1
	}
}
Exemple #4
0
func recursiveJson(key *datastore.Key) Response {
	var parentJson Response
	if key.Parent() != nil {
		parentJson = recursiveJson(key.Parent())
	}
	return Response{
		"stringID":  key.StringID(),
		"intID":     key.IntID(),
		"kind":      key.Kind(),
		"appID":     key.AppID(),
		"namespace": key.Namespace(),
		"parent":    parentJson,
	}
}
func translate(sourceKey *datastore.Key, translateAppId bool, targetAppId string, translateNamespace bool, targetNamespace string) (*datastore.Key, error) {
	if !translateAppId {
		targetAppId = sourceKey.AppID()
	}
	if !translateNamespace {
		targetNamespace = sourceKey.Namespace()
	}
	var translatedParent *datastore.Key = nil
	if sourceKey.Parent() != nil {
		var err error
		translatedParent, err = translate(sourceKey.Parent(), translateAppId, targetAppId, translateNamespace, targetNamespace)
		if err != nil {
			return nil, err
		}
	}
	return datastorekey.CreateKey(nil, targetAppId, targetNamespace, sourceKey.Kind(), sourceKey.StringID(), sourceKey.IntID(), translatedParent)
}
Exemple #6
0
// Builds a CommentData object from a models.Comment object with the proper fields
func BuildCommentData(ctx appengine.Context, comObj models.Comment, commentKey *datastore.Key) (models.CommentData, error) {
	var author models.User
	var sim models.Simulation
	var com models.CommentData

	authorKey := datastore.NewKey(ctx, "User", comObj.AuthorKeyName, 0, nil)
	err := datastore.Get(ctx, authorKey, &author)
	if err != nil {
		return com, err
	}

	var profileImageSrc string
	if len(string(author.ImageBlobKey)) > 0 {
		profileImageSrc = "/api/img/" + string(author.ImageBlobKey)
	}

	err = datastore.Get(ctx, commentKey.Parent(), &sim)
	if err != nil {
		return com, err
	}

	// Get all of the display names for each simulation author
	if author.DisplayName == "" {
		author.DisplayName = "Anonymous"
	}

	com.KeyName = comObj.KeyName
	com.Contents = comObj.Contents
	com.CreationDate = comObj.CreationDate
	com.PrettyCreationDate = comObj.CreationDate.Format("January _2, 2006")
	com.AuthorName = author.DisplayName
	com.AuthorID = author.KeyName
	com.AuthorImageSrcUrl = profileImageSrc
	com.SimulationName = sim.Name
	com.SimulationID = sim.KeyName
	com.SimulationType = sim.Type

	return com, nil
}
func extract(key *datastore.Key, field string) (string, error) {
	switch strings.ToLower(field) {
	case "kind":
		return key.Kind(), nil
	case "appid":
		return key.AppID(), nil
	case "namespace":
		return key.Namespace(), nil
	case "name":
		return key.StringID(), nil
	case "id":
		return fmt.Sprintf("%v", key.IntID()), nil
	case "parent":
		if key.Parent() == nil {
			return "", nil
		} else {
			return key.Parent().Encode(), nil
		}
	default:
		return "", fmt.Errorf("Unsupported field [%v]. Supported fields are kind, appID, namespace, name, id, parentkey.", field)
	}
}
Exemple #8
0
func starID(key *datastore.Key) string {
	return fmt.Sprintf("%s|%s", key.Parent().StringID(), key.StringID())
}
Exemple #9
0
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
}
Exemple #10
0
func (model *Model) SetKey(key *datastore.Key) {
	model.key = key
	model.parentKey = key.Parent()
}
Exemple #11
0
func rebuildKey(c appengine.Context, key *datastore.Key) *datastore.Key {
	if key == nil {
		return nil
	}
	return datastore.NewKey(c, key.Kind(), key.StringID(), key.IntID(), rebuildKey(c, key.Parent()))
}
Exemple #12
0
func FromGAEWithoutValidate(k *datastore.Key) (result key.Key) {
	if k == nil {
		return key.Key("")
	}
	result = key.NewWithoutValidate(k.Kind(), k.StringID(), k.IntID(), FromGAEWithoutValidate(k.Parent()))
	return
}
Exemple #13
0
func fillFields(key *datastore.Key, data map[string]interface{}) {
	data["kind"] = key.Kind()
	data["stringid"] = key.StringID()
	data["intid"] = key.IntID()
	data["appid"] = key.AppID()
	data["namespace"] = key.Namespace()
	if key.Parent() != nil {
		data["kind2"] = key.Parent().Kind()
		data["stringid2"] = key.Parent().StringID()
		data["intid2"] = key.Parent().IntID()
		if key.Parent().Parent() != nil {
			data["kind3"] = key.Parent().Parent().Kind()
			data["stringid3"] = key.Parent().Parent().StringID()
			data["intid3"] = key.Parent().Parent().IntID()
		}
	}
}