filter

package
v0.0.0-...-4a56adb Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func OffSetAndLimitParse

func OffSetAndLimitParse(r *api2go.Request) (offset string, limit string)

OffSetAndLimitParse parsers the limit and offset of the requests

Example
package main

import (
	"fmt"

	"github.com/bmc-toolbox/dora/filter"
	"github.com/manyminds/api2go"
)

func main() {
	request := api2go.Request{
		QueryParams: map[string][]string{
			"page[offset]": {"100"},
			"page[limit]":  {"10"},
		},
	}

	offset, limit := filter.OffSetAndLimitParse(&request)
	fmt.Println(offset)
	fmt.Println(limit)

	request.QueryParams = map[string][]string{
		"page[offset]": {"100"},
	}

	offset, limit = filter.OffSetAndLimitParse(&request)
	fmt.Println(offset)
	fmt.Println(limit)

}
Output:

100
10
100
100

Types

type Filter

type Filter struct {
	Filter   map[string][]string
	Operator string
}

Filter is meant to store the filters of requested via api

type Filters

type Filters struct {
	// contains filtered or unexported fields
}

Filters is is the collection of filters received on the api call

Example
package main

import (
	"fmt"

	"github.com/jinzhu/gorm"
	mocket "github.com/selvatico/go-mocket"

	"github.com/bmc-toolbox/dora/filter"
	"github.com/bmc-toolbox/dora/model"
	"github.com/manyminds/api2go"
)

func setupDB() *gorm.DB {
	mocket.Catcher.Register()
	mocket.Catcher.Logging = true

	db, _ := gorm.Open(mocket.DriverName, "connection_string")

	return db
}

func main() {
	db := setupDB()

	request := api2go.Request{
		QueryParams: map[string][]string{
			"filter[bmc_type]": {"iLO4"},
		},
	}

	filters, hasFilters := filter.NewFilterSet(&request)
	fmt.Println(hasFilters)

	q, err := filters.BuildQuery(model.Blade{}, db)
	if err != nil {
		panic(err)
	}
	fmt.Println(q.QueryExpr())

	request.QueryParams = map[string][]string{
		"filter[bmc_type]!": {"iLO4"},
	}

	filters, hasFilters = filter.NewFilterSet(&request)
	fmt.Println(hasFilters)

	q, err = filters.BuildQuery(model.Blade{}, db)
	if err != nil {
		panic(err)
	}
	fmt.Println(q.QueryExpr())

	filters.Clean()
	q, err = filters.BuildQuery(model.Blade{}, db)
	if err != nil {
		panic(err)
	}
	fmt.Println(q.QueryExpr())

	request.QueryParams = map[string][]string{}
	filters, hasFilters = filter.NewFilterSet(&request)
	fmt.Println(hasFilters)

}
Output:

`MOCK_FAKE_DRIVER` is not officially supported, running under compatibility mode.
true
&{SELECT * FROM ""  WHERE ("bmc_type" in (?)) [iLO4]}
true
&{SELECT * FROM ""  WHERE ("bmc_type" not in (?)) [iLO4]}
&{SELECT * FROM ""   []}
false

func NewFilterSet

func NewFilterSet(r *api2go.Request) (f *Filters, hasFilters bool)

NewFilterSet returns an empty new filter structure

func (*Filters) Add

func (f *Filters) Add(name string, values []string, operator string)

Add adds a new filter to the filter map

func (*Filters) BuildQuery

func (f *Filters) BuildQuery(m interface{}, db *gorm.DB) (q *gorm.DB, err error)

BuildQuery receive a model as an interface and builds a query out of it

func (*Filters) Clean

func (f *Filters) Clean()

Clean cleanup the current filter list

func (*Filters) Get

func (f *Filters) Get() []*Filter

Get retrieve all filters

Jump to

Keyboard shortcuts

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