parser

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count(search string, columnsSearchable []string) func(*bun.SelectQuery) *bun.SelectQuery

Count returns a Bun query modifier that applies only the search filter, for use with a COUNT query. Use it via query.Apply(Count(search, columns)).

func Paginate

func Paginate(pq *PaginationQuery, columnsSearchable []string) func(*bun.SelectQuery) *bun.SelectQuery

Paginate returns a Bun query modifier that applies search, sorting, and page/limit offsets. Use it via query.Apply(Paginate(pq, columns)).

pq.Page and pq.Limit are normalised in place to their defaults (1 and 10) when non-positive. Invalid sort/order values are ignored; call pq.Validate first to surface them as errors.

Types

type PaginationQuery

type PaginationQuery struct {
	Page   int    `query:"page"`
	Limit  int    `query:"limit"`
	Search string `query:"search"`
	Sort   string `query:"sort"`
	Order  string `query:"order"`
}

PaginationQuery is a struct for holding pagination query parameters.

func (*PaginationQuery) Validate

func (p *PaginationQuery) Validate(sortOptions []string) error

Validate validates the pagination query parameters.

Example

Validate guards the incoming query against an allow-list of sortable columns and the asc/desc order values before it touches the database.

package main

import (
	"fmt"

	"github.com/rahmadafandi/fibr/parser"
)

func main() {
	pq := &parser.PaginationQuery{Page: 1, Limit: 20, Sort: "name", Order: "asc"}
	if err := pq.Validate([]string{"name", "created_at"}); err != nil {
		fmt.Println("err:", err)
	} else {
		fmt.Println("ok")
	}

	bad := &parser.PaginationQuery{Sort: "password", Order: "sideways"}
	fmt.Println("bad:", bad.Validate([]string{"name", "created_at"}))
}
Output:
ok
bad: validation failed: sort must be one of name, created_at, order must be one of asc or desc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL