Esempio n. 1
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
}
Esempio n. 2
0
func main() {
	session, err := mgo.Dial("127.0.0.1")
	if err != nil {
		panic(err)
	}
	db := session.DB("resolver_test")
	coll1 := db.C("coll1")
	a := m{
		"a": 1,
		"b": 2,
	}
	a["_id"] = bson.NewObjectId()
	b := m{
		"d": 1,
		"f": 2,
	}
	b["_id"] = bson.NewObjectId()
	err = coll1.Insert(a)
	if err != nil {
		panic(err)
	}
	err = coll1.Insert(b)
	if err != nil {
		panic(err)
	}
	res := []interface{}{
		map[string]interface{}{
			"_coll1_single":   a["_id"],
			"_coll1_multiple": []interface{}{b["_id"], a["_id"], a["_id"]},
			"lol":             20,
		},
		map[string]interface{}{
			"_coll1_single":   b["_id"],
			"_coll1_multiple": []interface{}{b["_id"], b["_id"]},
			"lol":             21,
		},
	}
	resolver.ResolveAll(db, res, nil)
	for _, v := range res {
		fmt.Println(v, "\n")
	}
}
Esempio n. 3
0
func TestSingle(t *testing.T) {
	session, err := mgo.Dial("127.0.0.1")
	if err != nil {
		panic(err)
	}
	db := session.DB("resolver_test")
	coll1 := db.C("coll1")
	a := m{
		"name": "a",
		"a":    1,
		"b":    2,
	}
	a_id := bson.NewObjectId()
	a["_id"] = a_id
	if err != nil {
		panic(err)
	}
	unresolved := []interface{}{
		map[string]interface{}{
			"_coll1_single": a_id,
			"dummy_field":   42,
		},
	}
	resolved := []interface{}{
		map[string]interface{}{
			"_coll1_single": a,
			"dummy_field":   42,
		},
	}
	err = coll1.Insert(a)
	if err != nil {
		panic(err)
	}
	if enc(resolved) != enc(resolved) {
		t.Fatal("The test is inherently flawed.")
	}
	resolver.ResolveAll(db, unresolved, nil)
	if enc(unresolved) != enc(resolved) {
		t.Fatal("Resolved and unresolved values does not match.")
	}
}
Esempio n. 4
0
func TestAll(t *testing.T) {
	session, err := mgo.Dial("127.0.0.1")
	if err != nil {
		panic(err)
	}
	db := session.DB("test")
	coll1 := db.C("coll1")
	a := m{
		"name": "a",
		"a":    1,
		"b":    2,
	}
	a_id := bson.NewObjectId()
	a["_id"] = a_id
	b := m{
		"name": "b",
		"c":    3,
		"d":    4,
	}
	b_id := bson.NewObjectId()
	b["_id"] = b_id
	err = coll1.Insert(a)
	if err != nil {
		panic(err)
	}
	err = coll1.Insert(b)
	if err != nil {
		panic(err)
	}
	coll2 := db.C("coll2")
	c := m{
		"name": "c",
		"e":    4,
		"f":    6,
	}
	c_id := bson.NewObjectId()
	c["_id"] = c_id
	err = coll2.Insert(c)
	if err != nil {
		panic(err)
	}
	// Simulating a query result here.
	unresolved := []interface{}{
		map[string]interface{}{
			"_coll1_single":   a_id,
			"_coll1_multiple": []interface{}{a_id, b_id},
			"dummy_field":     42,
		},
		map[string]interface{}{
			"_coll1_single":   b_id,
			"_coll1_multiple": []interface{}{a_id, b_id, b_id},
			"dummy_field":     43,
		},
		map[string]interface{}{
			"_coll2_single": c_id,
			"_coll1_varied": []interface{}{a_id, 44, m{}, b_id, 45, a_id, 46},
		},
	}
	resolved := []interface{}{
		map[string]interface{}{
			"_coll1_single":   a,
			"_coll1_multiple": []interface{}{a, b},
			"dummy_field":     42,
		},
		map[string]interface{}{
			"_coll1_single":   b,
			"_coll1_multiple": []interface{}{a, b, b},
			"dummy_field":     43,
		},
		map[string]interface{}{
			"_coll2_single": c,
			"_coll1_varied": []interface{}{a, 44, m{}, b, 45, a, 46},
		},
	}
	// Testing if we can use JSON encryption to equality testing.
	if enc(resolved) != enc(resolved) {
		t.Fatal("The test is inherently flawed.")
	}
	resolver.ResolveAll(db, unresolved, nil)
	if enc(unresolved) != enc(resolved) {
		t.Fatal("Resolved and unresolved values does not match.")
	}
}