filter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: MIT Imports: 8 Imported by: 1

README

filter

Simple and extensible data filtering framework. You can simply create filters from json/yaml data, filter definition can immbeded in business data. e.g.

DATA

{
    "banner" : {
        "type" : "image",
        "src" : "https://example.com/working-hard.png",
        "link" : "https://example.com/activity.html"
    },
    //... other config

    "filter" : [
        ["time", "between", "18:00,23:00"],
        ["ctx.user.group", "=", "programer"],
        ["banner", "+", {
            "src" : "https://example.com/chat-with-beaty.png",
            "link" : "https://chat.com"
        }]
    ]
}

CODE

import (
    "encoding/json"

	"github.com/techxmind/filter"
	"github.com/techxmind/filter/core"
)

func main() {
    var dataStr = `...` // data above

	var data map[string]interface{}

	json.Unmarshal([]byte(dataStr), &data)

	// your business context
	ctx := context.Background()

	filterCtx := core.WithContext(ctx)

	// you can also set context value in your business ctx with context.WithValue("group", ...)
	filterCtx.Set("user", map[string]interface{}{"group": "programer"})

	f, _ := filter.New(data["filter"].([]interface{}))
	f.Run(filterCtx, data)

    // if current time is between 18:00 ~ 23:00, output:
    // map[link:https://chat.com src:https://example.com/chat-with-beaty.png type:image]
	fmt.Println(data["banner"])
}

Variables

Register your custom variable:

import (
	"github.com/techxmind/filter/core"
)

// Register variable "username" that fetch value from context
core.GetVariableFactory().Register(
    core.SingletonVariableCreator(core.NewSimpleVariable("username", core.Cacheable, &ContextValue{USER_AGENT})),
    "username"
)

Check ext folder to see more examples.

Operations

Register your custom operation:

import (
    "errors"
    "strings"

	"github.com/techxmind/filter/core"
	"github.com/techxmind/go-utils/itype"
)

// Register operation "contains" to check if variable contains specified substring
// ["url", "contains", "something"]
core.GetOperationFactory().Register(&ContainsOperation{}, "contains")

type ContainsOperation struct {}

func (o *ContainsOperation) String() string {
    return "contains"
}

func (o *ContainsOperation) Run(ctx *core.Context, variable core.Variable, value interface{}) bool {
	v := core.GetVariableValue(ctx, variable)

    return strings.Contains(itype.String(v), itype.String(value))
}

func (o *ContainsOperation) PrepareValue(v interface{}) (interface{}, error) {
    if str, ok := v.(string); ok {
        return v, nil
    }

    return nil, errors.New("[contains] operation require value of type string")
}

Assignments

...

Trace

Trace each step of filter's execution.

import (
    "context"
    "os"

	"github.com/techxmind/filter/core"
	"github.com/techxmind/filter/filter"
)

//other code...

// your business context
ctx := context.Background()

// Initialize filter context with trace option
// You can use your custom Trace instaed of the default implementtion
filterCtx := core.WithContext(ctx, WithTrace(core.NewTrace(os.Stderr)))

youfilter.Run(filterCtx, data)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PickIndexByWeight

func PickIndexByWeight(items []Weighter, totalWeight int64) int

Types

type EnableRank

type EnableRank bool

EnableRank Option

type Filter

type Filter interface {
	Name() string
	Run(ctx context.Context, data interface{}) bool
}

Filter interface

func New

func New(items []interface{}, options ...Option) (Filter, error)

New build filter with specified data struct.

items:
  single filter:
   [
     "$filter-name"  // filter name, first item, optional
     ["$var-name", "$op", "$op-value"],  // condition
     ["$var-name", "$op", "$op-value"],  // condition
     ["$data-key", "$assign", "$assign-value"] // executor, last item
   ]

  filter group:
  [
    // filter
    [
     "$filter-name"  // filter name, first item, optional
     ["$var-name", "$op", "$op-value"],  // condition
     ["$var-name", "$op", "$op-value"],  // condition
     ["$data-key", "$assign", "$assign-value"] // executor, last item
   ],
   // filter
   [
     "$filter-name"  // filter name, first item, optional
     ["$var-name", "$op", "$op-value"],  // condition
     ["$var-name", "$op", "$op-value"],  // condition
     ["$data-key", "$assign", "$assign-value"] // executor, last item
   ]
  ]

options:
  ShortMode(true)     // enable short mode, only active in group filter
  EnableRank(true)    // enable rank mode, and set short mode only active in group filter
  Name("filter-name") // specify filter name

type FilterGroup

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

FilterGroup contains multiple filters

func NewFilterGroup

func NewFilterGroup(options ...Option) *FilterGroup

func (*FilterGroup) Add

func (f *FilterGroup) Add(filter Filter, options ...Option)

func (*FilterGroup) Name

func (f *FilterGroup) Name() string

func (*FilterGroup) Run

func (f *FilterGroup) Run(pctx context.Context, data interface{}) (succ bool)

type Name

type Name string

Name Option

type NamePrefix

type NamePrefix string

NamePrefix Option

type Option

type Option interface {
	// contains filtered or unexported methods
}

type Options

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

type Priority

type Priority uint64

Priority Option

type ShortMode

type ShortMode bool

ShortMode Option

type Weight

type Weight uint64

Weight Option

type Weighter

type Weighter interface {
	Weight() int64
}

Directories

Path Synopsis
ext
location
Define location variables from client ip So you should import package that defines ip var, e.g.
Define location variables from client ip So you should import package that defines ip var, e.g.
request
Variables in client-server enviroment url : request url, setted with context.WithValue("request-url", "....") ua : client user-agent, setted with context.WithValue("user-agent", "...") ip : client_ip, setted with context.WithValue("client-ip", "...") get.xxx : query value from url.
Variables in client-server enviroment url : request url, setted with context.WithValue("request-url", "....") ua : client user-agent, setted with context.WithValue("user-agent", "...") ip : client_ip, setted with context.WithValue("client-ip", "...") get.xxx : query value from url.

Jump to

Keyboard shortcuts

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