package module
v1.2.1 Latest Latest

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

Go to latest
Published: Jan 5, 2024 License: MIT Imports: 12 Imported by: 12



GitHub tag (latest SemVer) Actions Status Coverage Status Go Report Card Go Reference

filter - provide filtering, sanitizing, and conversion of Golang data.

中文说明请查看 README.zh-CN


NOTE: To filter and validate Map, Struct data. Please use gookit/validate


go get

Func Usage

Quick usage:

str := filter.MustString(23) // "23"

intVal, err := filter.Int("20") // int(20)
strings := filter.Str2Slice("a,b, c", ",") // []string{"a", "b", "c"}


Filtering data:

data := map[string]any{
    "name":     " inhere ",
    "age":      "50",
    "money":    "50.34",
    "remember": "yes",
    "sub1": []string{"1", "2"},
    "tags": "go;lib",
    "str1": " word ",
    "ids":  []int{1, 2, 2, 1},
f := filter.New(data)
f.AddRule("money", "float")
f.AddRule("remember", "bool")
f.AddRule("sub1", "strings2ints")
f.AddRule("tags", "str2arr:;")
f.AddRule("ids", "unique")
f.AddRule("str1", "ltrim|rtrim")
f.AddRule("not-exist", "unique")
// add multi
    "age": "trim|int",
    "name": "trim|ucFirst",

// apply all added rules for data.

// get filtered data
newData := f.CleanData()
fmt.Printf("%#v\n", newData)
// f.BindStruct(&user)


map[string]interface {}{
    "sub1":[]int{1, 2}, 
    "tags":[]string{"go", "lib"}, 
    "ids":[]int{2, 1}, 

Filters & Converters

  • ToBool/Bool(s string) (bool, error)
  • ToFloat/Float(v interface{}) (float64, error)
  • ToInt/Int(v interface{}) (int, error)
  • ToUint/Uint(v interface{}) (uint64, error)
  • ToInt64/Int64(v interface{}) (int64, error)
  • ToString/String(v interface{}) (string, error)
  • MustBool(s string) bool
  • MustFloat(s string) float64
  • MustInt(s string) int
  • MustInt64(s string) int64
  • MustUint(s string) uint64
  • MustString(v interface{}) string
  • Trim(s string, cutSet ...string) string
  • TrimLeft(s string, cutSet ...string) string
  • TrimRight(s string, cutSet ...string) string
  • TrimStrings(ss []string, cutSet ...string) (ns []string)
  • Substr(s string, pos, length int) string
  • Lower/Lowercase(s string) string
  • Upper/Uppercase(s string) string
  • LowerFirst(s string) string
  • UpperFirst(s string) string
  • UpperWord(s string) string
  • Camel/CamelCase(s string, sep ...string) string
  • Snake/SnakeCase(s string, sep ...string) string
  • Email(s string) string
  • URLDecode(s string) string
  • URLEncode(s string) string
  • EscapeJS(s string) string
  • EscapeHTML(s string) string
  • Unique(val interface{}) interface{} Will remove duplicate values, use for []int []int64 []string
  • StrToSlice(s string, sep ...string) []string
  • StrToInts(s string, sep ...string) (ints []int, err error)
  • StrToTime(s string, layouts ...string) (t time.Time, err error)
  • StringsToInts(ss []string) (ints []int, err error)





Package filter provide data filter, sanitize, convert process.

Source code and other details for the project are available at GitHub:

More usage please see README and tests



This section is empty.


View Source
var (
	Lower = strings.ToLower
	Upper = strings.ToUpper
	Title = strings.ToTitle

	// EscapeJS escape javascript string
	EscapeJS = template.JSEscapeString
	// EscapeHTML escape html string
	EscapeHTML = template.HTMLEscapeString

Some alias methods.


func Apply added in v1.0.5

func Apply(name string, val any, args []string) (any, error)

Apply a filter by name. for filter value.

func Bool added in v1.0.1

func Bool(s string) (bool, error)

Bool parse string to bool

func Camel added in v1.0.10

func Camel(s string, sep ...string) string

Camel alias of the CamelCase

func CamelCase added in v1.0.1

func CamelCase(s string, sep ...string) string

CamelCase convert string to camel case.


"range_price" -> "rangePrice"
"range price" -> "rangePrice"
"range-price" -> "rangePrice"

func Email added in v1.0.1

func Email(s string) string

Email filter, clear invalid chars.

func Float added in v1.0.1

func Float(s string) (float64, error)

Float convert string to float

func GetByPath

func GetByPath(key string, mp map[string]any) (any, bool)

GetByPath get value from a map[string]any. eg "top" "top.sub"

func Int

func Int(in any) (int, error)

Int convert string to int

func Int64 added in v1.0.1

func Int64(in any) (int64, error)

Int64 convert value to int64

func LowerFirst added in v1.0.1

func LowerFirst(s string) string

LowerFirst lower first char

func Lowercase added in v1.0.5

func Lowercase(s string) string

Lowercase alias of the strings.ToLower()

func MustBool added in v1.0.1

func MustBool(s string) bool

MustBool convert, will ignore error.

func MustFloat

func MustFloat(s string) float64

MustFloat convert string to float, will ignore error

func MustInt

func MustInt(in any) int

MustInt convert string to int, alias of the mathutil.SafeInt

func MustInt64 added in v1.0.1

func MustInt64(in any) int64

MustInt64 convert value to int64, alias of the mathutil.SafeInt64

func MustString added in v1.0.11

func MustString(in any) string

MustString convert value to string, will ignore error

func MustUint

func MustUint(in any) uint

MustUint convert string to uint, will ignore error

func MustUint64 added in v1.2.1

func MustUint64(in any) uint64

MustUint64 convert string to uint64, alias of the mathutil.SafeUint64

func Name added in v1.0.5

func Name(name string) string

Name get real filter name.

func Snake added in v1.0.10

func Snake(s string, sep ...string) string

Snake alias of the SnakeCase

func SnakeCase added in v1.0.1

func SnakeCase(s string, sep ...string) string

SnakeCase convert. eg "RangePrice" -> "range_price"

func StrToArray added in v1.0.1

func StrToArray(s string, sep ...string) []string

StrToArray alias of the StrToSlice()

func StrToInts added in v1.0.7

func StrToInts(s string, sep ...string) ([]int, error)

StrToInts split string to slice and convert item to int.

func StrToSlice added in v1.0.2

func StrToSlice(s string, sep ...string) []string

StrToSlice split string to array.

func StrToTime added in v1.0.1

func StrToTime(s string, layouts ...string) (time.Time, error)

StrToTime convert date string to time.Time

func String

func String(val any) (string, error)

String convert val to string

func StringsToInts added in v1.0.5

func StringsToInts(ss []string) ([]int, error)

StringsToInts string slice to int slice

func Substr added in v1.0.1

func Substr(s string, pos, length int) string

Substr cut string

func ToBool added in v1.0.1

func ToBool(s string) (bool, error)

ToBool convert string to bool

func ToFloat

func ToFloat(s string) (float64, error)

ToFloat convert string to float

func ToInt

func ToInt(in any) (int, error)

ToInt convert string to int

func ToInt64

func ToInt64(val any) (int64, error)

ToInt64 convert value to int64

func ToString added in v1.0.11

func ToString(val any) (string, error)

ToString convert value to string

func ToUint

func ToUint(in any) (uint, error)

ToUint convert string to uint

func ToUint64 added in v1.2.1

func ToUint64(in any) (uint64, error)

ToUint64 convert string to uint64

func Trim

func Trim(s string, cutSet ...string) string

Trim string

func TrimLeft added in v1.0.1

func TrimLeft(s string, cutSet ...string) string

TrimLeft char in the string.

func TrimRight added in v1.0.1

func TrimRight(s string, cutSet ...string) string

TrimRight char in the string.

func TrimStrings added in v1.0.5

func TrimStrings(ss []string, cutSet ...string) []string

TrimStrings trim string slice item.

func URLDecode added in v1.0.5

func URLDecode(s string) string

URLDecode decode url string.

func URLEncode added in v1.0.5

func URLEncode(s string) string

URLEncode encode url string.

func Uint

func Uint(in any) (uint, error)

Uint convert string to uint

func Uint64 added in v1.2.1

func Uint64(in any) (uint64, error)

Uint64 convert string to uint64

func Unique added in v1.0.1

func Unique(val any) any

Unique value in the given array, slice.

func UpperFirst added in v1.0.1

func UpperFirst(s string) string

UpperFirst upper first char

func UpperWord added in v1.0.4

func UpperWord(s string) string

UpperWord Change the first character of each word to uppercase

func Uppercase added in v1.0.5

func Uppercase(s string) string

Uppercase alias of the strings.ToUpper()


type Filtration

type Filtration struct {
	// contains filtered or unexported fields

Filtration definition. Sanitization Sanitizing Sanitize

func New added in v1.0.4

func New(data map[string]any) *Filtration

New a Filtration

func (*Filtration) AddRule added in v1.0.4

func (f *Filtration) AddRule(field string, rule any) *Rule

AddRule add filter(s) rule.


f.AddRule("name", "trim")
f.AddRule("age", "int")
f.AddRule("age", "trim|int")

func (*Filtration) AddRules added in v1.0.5

func (f *Filtration) AddRules(rules map[string]string) *Filtration

AddRules add multi rules.


	"name": "trim|lower",
	"age": "trim|int",

func (*Filtration) BindStruct added in v1.0.8

func (f *Filtration) BindStruct(ptr any) error

BindStruct bind the filtered data to struct.

func (*Filtration) Bool added in v1.0.5

func (f *Filtration) Bool(key string) bool

Bool value get from the filtered data.

func (*Filtration) CleanData added in v1.0.9

func (f *Filtration) CleanData() map[string]any

CleanData get filtered data

func (*Filtration) Clear added in v1.0.9

func (f *Filtration) Clear()

Clear all data and rules

func (*Filtration) Err added in v1.0.10

func (f *Filtration) Err() error

Err get error

func (*Filtration) Filtering added in v1.0.3

func (f *Filtration) Filtering() error

Filtering apply all filter rules, filtering data

func (*Filtration) Get

func (f *Filtration) Get(key string) (any, bool)

Get value by key

func (*Filtration) Int added in v1.0.4

func (f *Filtration) Int(key string) int

Int get a int value from filtered data.

func (*Filtration) Int64 added in v1.0.5

func (f *Filtration) Int64(key string) int64

Int64 get a int value from filtered data.

func (*Filtration) IsOK added in v1.0.5

func (f *Filtration) IsOK() bool

IsOK of to apply filters

func (*Filtration) LoadData added in v1.0.9

func (f *Filtration) LoadData(data map[string]any)

LoadData set raw data for filtering.

func (*Filtration) MustGet added in v1.0.5

func (f *Filtration) MustGet(key string) any

MustGet value by key

func (*Filtration) Raw added in v1.0.4

func (f *Filtration) Raw(key string) (any, bool)

Raw get raw value by key

func (*Filtration) RawData added in v1.0.9

func (f *Filtration) RawData() map[string]any

RawData get raw data

func (*Filtration) ResetData added in v1.0.9

func (f *Filtration) ResetData(resetRaw bool)

ResetData reset raw and filtered data

func (*Filtration) ResetRules added in v1.0.9

func (f *Filtration) ResetRules()

ResetRules reset rules and filtered data

func (*Filtration) Safe added in v1.0.5

func (f *Filtration) Safe(key string) (any, bool)

Safe get filtered value by key

func (*Filtration) SafeVal added in v1.0.7

func (f *Filtration) SafeVal(key string) any

SafeVal get filtered value by key

func (*Filtration) Sanitize added in v1.0.5

func (f *Filtration) Sanitize() error

Sanitize is alias of the Filtering()

func (*Filtration) String added in v1.0.5

func (f *Filtration) String(key string) string

String get a string value from filtered data.

type Rule added in v1.0.5

type Rule struct {
	// contains filtered or unexported fields

Rule definition

func (*Rule) AddFilters added in v1.0.5

func (r *Rule) AddFilters(filters ...string) *Rule

AddFilters add multi filter(s).


r.AddFilters("int", "str2arr:,")

func (*Rule) Apply added in v1.0.5

func (r *Rule) Apply(f *Filtration) (err error)

Apply rule for the rule fields

func (*Rule) Fields added in v1.0.5

func (r *Rule) Fields() []string

Fields name get

func (*Rule) SetDefaultVal added in v1.0.9

func (r *Rule) SetDefaultVal(defaultVal any) *Rule

SetDefaultVal set default value for the rule

func (*Rule) SetFilterFunc added in v1.0.9

func (r *Rule) SetFilterFunc(fn func(val any) (any, error)) *Rule

SetFilterFunc user custom filter func

Jump to

Keyboard shortcuts

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