Skip to content

EgorBron/grfilt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grfilt [logo]

A package that provides filtering primitives for your modules in a idiomatic Go way.

The idea of such filters was inspired by some other projects proposing a combo of builder pattern on variadic functions.

Installation

go get txts.su/grfilt@latest

And you're ready to go!

Usage examples

You can find them on godoc, but the drop-in one is below.

Basically, you should do the following: install 3rd-party filters or write some by your own with NewFilter:

const MyUserDescriptor grfilt.FieldDescriptor = "myuser"
var MyNiceFilter = grfilt.NewFilter(MyUserDescriptor, func(v MyUser)bool{
  return ...
})

And then use them by composing with each other:

filter := MyNiceFilter.And(
  grfilt.Not(thirdparty.Header("X-User-Id", "100"))
).
Or(othermod.Ratelimit("5/minute"))

result := filter.Eval(map[grfilt.FieldDescriptor]any{
  MyUserDescriptor: ...,
  thirdparty.HttpDescriptor: ...,
  othermod.RatelimitStoreDescriptor: ...,
})

Filter packages made with grfilt

  • None yet!

Guildelines for making own filters module

  1. Follow the conventions.
    1. Always prefix your packages with grf_ to distinct it. Example: grf_sql, grf_mylib_filters.
    2. Filters must be functions that return type implementing the Filter interface and be named like [Subject]Filter.
    3. For each custom input type or possible input field you must create a constant typed with FieldDescriptor and named like [Object]Descriptor.
  2. Do use NewFilter for the simple cases. But you should implement a custom filter type if your Eval logic differs or if you need extra methods/fields for it.
  3. You should provide a test suite sources and should grant a permission to run them.
  4. You must explicitly state that filters you've published made with grfilt.

After publishing a Go module that sticks with these guidelines, you can open a PR to add your nice module to the README section above!

License

grfilt is licensed under the MIT License.

About

Filtering primitives for Go in idiomatic way

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages