示例#1
0
文件: revmgo.go 项目: hura/revmgo
	case "clone", "copy", "new":
		return nil
	}
	return fmt.Errorf("revmgo: Invalid session instantiation method '%s'", m)
}

// Custom TypeBinder for bson.ObjectId
// Makes additional Id parameters in actions obsolete
var ObjectIdBinder = revel.Binder{
	// Make a ObjectId from a request containing it in string format.
	Bind: revel.ValueBinder(func(val string, typ reflect.Type) reflect.Value {
		if len(val) == 0 {
			return reflect.Zero(typ)
		}
		if bson.IsObjectIdHex(val) {
			objId := bson.ObjectIdHex(val)
			return reflect.ValueOf(objId)
		} else {
			revel.ERROR.Print("ObjectIdBinder.Bind - invalid ObjectId!")
			return reflect.Zero(typ)
		}
	}),
	// Turns ObjectId back to hexString for reverse routing
	Unbind: func(output map[string]string, name string, val interface{}) {
		var hexStr string
		hexStr = fmt.Sprintf("%s", val.(bson.ObjectId).Hex())
		// not sure if this is too carefull but i wouldn't want invalid ObjectIds in my App
		if bson.IsObjectIdHex(hexStr) {
			output[name] = hexStr
		} else {
			revel.ERROR.Print("ObjectIdBinder.Unbind - invalid ObjectId!")
			output[name] = ""
示例#2
0
文件: app.go 项目: pombredanne/goqdb
		"date":   ` Created `,
		"rating": ` Rating `,
		"relevance": ` CASE ` +
			` WHEN Quote LIKE :search_leading THEN 0 ` +
			` WHEN Quote LIKE :search THEN 1 ` +
			` WHEN Tags LIKE :search THEN 2 ` +
			` ELSE 3 END `,
	}

	// form input "c"sv tags binder
	TagsBinder = revel.Binder{

		Bind: revel.ValueBinder(func(val string, typ reflect.Type) reflect.Value {
			if len(val) == 0 {
				return reflect.Zero(typ)
			}
			s := strings.Split(val, INPUT_TAG_DELIM)

			return reflect.ValueOf(s)
		}),
		Unbind: nil,
	}

	PaginationBinder = revel.Binder{
		Bind: func(params *revel.Params, name string, typ reflect.Type) reflect.Value {
			var p models.Pagination

			params.Bind(&p.Page, "page")

			if p.Page == 0 {
				p.Page = 1
			}