示例#1
0
文件: load.go 项目: Laller/chill
// Searches all {{load modname/filename.ext}} and replaces that with the proper requires then calls the require module on the file.
func Load(opt_i interface{}, root string, file []byte, get func(string, string) ([]byte, error)) ([]byte, error) {
	r := regexp.MustCompile(beg + "([a-zA-Z_.:/-])*" + end)
	s := r.FindAllString(string(file), -1)
	cut_beg := len(beg)
	cut_end := len(end)
	for _, v := range s {
		replacement := []byte{}
		load_name := v[cut_beg : len(v)-cut_end]
		loads, has := jsonp.Get(opt_i, load_name)
		if has {
			req_paths := jsonp.ToStringSlice(loads.([]interface{}))
			for _, x := range req_paths {
				replacement = append(replacement, []byte(fmt.Sprintf("{{require %v}}", x))...)
			}
		}
		if len(replacement) > 0 {
			str, err := require.RMem(root, replacement, get)
			if err != nil {
				return nil, err
			}
			replacement = []byte(str)
		}
		file = r.ReplaceAll(file, replacement)
	}
	return file, nil
}
示例#2
0
// c: 		collection			string
// q: 		query				map[string]interface{}
// p:		page number key		string							This is used to extract the page nubver from get parameters. Also activates paging.
//																Only works with limit.
// sk: 		skip				float64/int						Hardcoded value, barely useful (see p instead)
// l:		limit				float64/int
// so:		sort				string							Example: "-created"
//
// TODO: check for validity of type assertions.
func RunQueries(db *mgo.Database, queries map[string]interface{}, get map[string][]string, path_n_query string) map[string]interface{} {
	qs := make(map[string]interface{})
	for name, z := range queries {
		v := z.(map[string]interface{})
		_, coll_ok := v["c"]
		_, quer_ok := v["q"]
		if !coll_ok || !quer_ok {
			continue
		}
		q := db.C(v["c"].(string)).Find(v["q"])
		if skip, skok := v["sk"]; skok {
			q.Skip(toInt(skip))
		}
		if limit, lok := v["l"]; lok {
			q.Limit(toInt(limit))
		}
		if sort, sook := v["so"]; sook {
			if sort_string, is_str := sort.(string); is_str {
				q.Sort(sort_string)
			} else if sort_slice, is_sl := sort.([]interface{}); is_sl {
				q.Sort(jsonp.ToStringSlice(sort_slice)...)
			}
		}
		if p, pok := v["p"]; pok {
			if limit, lok := v["l"]; lok { // Only makes sense with limit.
				paging_inf := DoPaging(db, v["c"].(string), v["q"].(map[string]interface{}), p.(string), get, path_n_query, toInt(limit))
				qs[name+"_navi"] = paging_inf
				q.Skip(paging_inf.Skip)
			}
		}
		var res []interface{}
		err := q.All(&res)
		if err != nil {
			qs[name] = err.Error()
			continue
		}
		res = basic.Convert(res).([]interface{})
		if ex, ex_ok := v["ex"]; ex_ok {
			ex_m, ex_is_m := ex.(map[string]interface{})
			if ex_is_m && len(ex_m) == 1 {
				CreateExcerpts(res, ex_m)
			}
		}
		var resolve_fields map[string]interface{}
		if val, has := v["r"]; has {
			resolve_fields = val.(map[string]interface{})
		} else {
			resolve_fields = map[string]interface{}{"password": 0}
		}
		resolver.ResolveAll(db, res, resolve_fields)
		qs[name] = res
	}
	return qs
}
示例#3
0
文件: display.go 项目: Laller/nocrud
func toStringSlice(a interface{}) []string {
	if a == nil {
		return nil
	}
	switch val := a.(type) {
	case []interface{}:
		return jsonp.ToStringSlice(val)
	case []string:
		return val
	}
	return nil
}
示例#4
0
文件: vote.go 项目: Laller/hypecms
// Most return values ever.
func sharedProc(action map[string]interface{}, inp map[string][]string) (map[string]interface{}, string, []string, string, error) {
	collection := action["c"].(string)
	vote_options := jsonp.ToStringSlice(action["vote_options"].([]interface{}))
	rules := map[string]interface{}{
		"document_id": "must",
		"vote_option": "must",
	}
	dat, err := extract.New(rules).Extract(inp)
	if err != nil {
		return nil, "", nil, "", err
	}
	input_vote := dat["vote_option"].(string)
	if !isLegalOption(vote_options, input_vote) {
		return nil, "", nil, "", fmt.Errorf("Not a legal option.")
	}
	return dat, collection, vote_options, input_vote, nil
}