Giter Club home page Giter Club logo

filter's Introduction

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

NOTE: To filter and validate Map, Struct data. Please 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]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
f.AddRules(map[string]string{
    "age": "trim|int",
    "name": "trim|ucFirst",
})

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

// get filtered data
newData := f.CleanData()
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

filter's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar inhere avatar nico385412 avatar olianate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

filter's Issues

compiling error after goutil update

System (please complete the following information):

  • OS: linux

Describe the bug

When building app getting

github.com/gookit/filter

/home/gitlab-runner/go/pkg/mod/github.com/gookit/[email protected]/converters.go:55:46: cannot use mathutil.ToUint(in) (value of type uint) as uint64 value in return statement

cannot use mathutil.ToUint(in) (value of type uint) as uint64 value in return statement

System (please complete the following information):

  • OS: win
  • GO Version: go version go1.21.4 windows/amd64
  • Pkg Version: 1github.com/gookit/filter v1.2.0

Describe the bug

github.com/gookit/filter

github.com/gookit/filter

........\vendor\github.com\gookit\filter\converters.go:55:46: cannot use mathutil.ToUint(in) (value of type uint) as uint64 value in return statement
exit status 1

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.