Exemple #1
0
// NewPager returns a new *Pager instance. Note that the
// Context must satisfy the conditions mentioned in the
// Pager type documentation. Otherwise an error will be
// returned.
func NewPager(ctx *app.Context) (*Pager, error) {
	name := ctx.HandlerName()
	if name == "" {
		return nil, errors.New("can't generate a pager from an unnamed handler")
	}
	names := ctx.Provider().ParamNames()
	if len(names) == 0 {
		return nil, errors.New("can't generate a pager from a handler with no parameters, must have a page parameter")
	}
	if names[len(names)-1] != "page" {
		return nil, fmt.Errorf("last named parameter is %q, not page", names[len(names)-1])
	}
	params := make([]interface{}, ctx.Count()-1)
	for ii := 0; ii < ctx.Count()-1; ii++ {
		params[ii] = ctx.IndexValue(ii)
	}
	return &Pager{
		Name:       name,
		Parameters: params,
		Ctx:        ctx,
	}, nil
}