Пример #1
0
// Special fields in query:
// parentf, sort, limit, skip, page
func processMap(inp map[string]interface{}, ev iface.Event) *data {
	d := &data{}
	if inp == nil {
		inp = map[string]interface{}{}
	}
	int_sch := map[string]interface{}{
		"type": "int",
	}
	sch := map[string]interface{}{
		//"parentf": 1,
		"sort": map[string]interface{}{
			"slice": true,
			"type":  "string",
		},
		"skip":  int_sch,
		"limit": int_sch,
		"page":  int_sch,
	}
	ex, err := sanitize.New(sch)
	if err != nil {
		panic(err)
	}
	dat, err := ex.Extract(inp)
	if err != nil {
		panic(err)
	}
	for i := range sch {
		delete(inp, i)
	}
	mods := &Mods{}
	//if dat["parentf"] != nil {
	//	d.parentField = dat["parentf"].(string)
	//}
	if dat["sort"] != nil {
		mods.sort = convert.ToStringSlice(dat["sort"].([]interface{}))
	}
	if dat["skip"] != nil {
		mods.skip = int(dat["skip"].(int64))
	}
	if dat["limit"] != nil {
		mods.limit = int(dat["limit"].(int64))
	} else {
		mods.limit = 20
	}
	if dat["page"] != nil {
		page := int(dat["page"].(int64))
		mods.skip = (page - 1) * mods.limit
	}
	d.mods = mods
	ev.Fire("ProcessMap", inp) // We should let the subscriber now the subject name.
	d.query = toQuery(inp)
	return d
}
Пример #2
0
// Builds a user from his Id and information in http_header.
func BuildUser(a iface.Filter, ev iface.Event, user_id bson.ObjectId, http_header map[string][]string) (map[string]interface{}, error) {
	user, err := FindUser(a, user_id)
	if err != nil || user == nil {
		user = EmptyUser()
	}
	_, langs_are_set := user["languages"]
	if !langs_are_set {
		langs, has := http_header["Accept-Language"]
		if has {
			user["languages"] = ParseAcceptLanguage(langs[0])
		} else {
			user["languages"] = []string{"en"}
		}
	}
	ev.Fire("user.build", user)
	return user, nil
}