filter

package module
Version: v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: MIT Imports: 12 Imported by: 3

README

Filter

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

GoDoc

TIP: To filter and validate Map, Struct data, use gookit/validate

Install

go get github.com/gookit/filter

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"}

Filtration

Filtering data:

data := map[string]interface{}{
    "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
f.AddRules(map[string]string{
    "age": "trim|int",
    "name": "trim|ucFirst",
})

// apply all added rules for data.
f.Filtering() 

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

Output:

map[string]interface {}{
    "remember":true, 
    "sub1":[]int{1, 2}, 
    "tags":[]string{"go", "lib"}, 
    "ids":[]int{2, 1}, 
    "str1":"word", 
    "name":"INHERE", 
    "age":50, 
    "money":50.34
}

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)

License

MIT

Documentation

Overview

Package filter provide data filter, sanitize, convert

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

https://github.com/gookit/filter

More usage please see README and test

Index

Constants

This section is empty.

Variables

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.

Functions

func Apply added in v1.0.5

func Apply(name string, val interface{}, args []string) (interface{}, 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.

Support:

"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]interface{}) (interface{}, bool)

GetByPath get value from a map[string]interface{}. eg "top" "top.sub"

func Int

func Int(in interface{}) (int, error)

Int convert string to int

func Int64 added in v1.0.1

func Int64(in interface{}) (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.

func MustFloat

func MustFloat(s string) float64

MustFloat convert string to float

func MustInt

func MustInt(in interface{}) int

MustInt convert string to int

func MustInt64 added in v1.0.1

func MustInt64(in interface{}) int64

MustInt64 convert value to int64

func MustString added in v1.0.11

func MustString(in interface{}) string

MustString convert value to string

func MustUint

func MustUint(in interface{}) uint64

MustUint convert string to uint

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 interface{}) (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 interface{}) (int, error)

ToInt convert string to int

func ToInt64

func ToInt64(val interface{}) (int64, error)

ToInt64 convert value to int64

func ToString added in v1.0.11

func ToString(val interface{}) (string, error)

ToString convert value to string

func ToUint

func ToUint(in interface{}) (uint64, error)

ToUint convert string to uint

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 interface{}) (uint64, error)

Uint convert string to uint

func Unique added in v1.0.1

func Unique(val interface{}) interface{}

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()

Types

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]interface{}) *Filtration

New a Filtration

func (*Filtration) AddRule added in v1.0.4

func (f *Filtration) AddRule(field string, rule interface{}) *Rule

AddRule add filter(s) rule.

Usage:

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.

Usage:

f.AddRules(map[string]string{
	"name": "trim|lower",
	"age": "trim|int",
})

func (*Filtration) BindStruct added in v1.0.8

func (f *Filtration) BindStruct(ptr interface{}) 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]interface{}

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) (interface{}, 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]interface{})

LoadData set raw data for filtering.

func (*Filtration) MustGet added in v1.0.5

func (f *Filtration) MustGet(key string) interface{}

MustGet value by key

func (*Filtration) Raw added in v1.0.4

func (f *Filtration) Raw(key string) (interface{}, bool)

Raw get raw value by key

func (*Filtration) RawData added in v1.0.9

func (f *Filtration) RawData() map[string]interface{}

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) (interface{}, bool)

Safe get filtered value by key

func (*Filtration) SafeVal added in v1.0.7

func (f *Filtration) SafeVal(key string) interface{}

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).

Usage:

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 interface{}) *Rule

SetDefaultVal set default value for the rule

func (*Rule) SetFilterFunc added in v1.0.9

func (r *Rule) SetFilterFunc(fn func(val interface{}) (interface{}, 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