Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed filter function to accept an extra parameter called bind. #346

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
version: v1.0
name: Go
agent:
machine:
type: e1-standard-2
os_image: ubuntu2004
blocks:
- name: Test
task:
jobs:
- name: go test
commands:
- sem-version go 1.18
- export GOPATH=~/go
- 'export PATH=/home/semaphore/go/bin:$PATH'
- checkout
- go get ./...
- go test ./...
version: v1.0
name: Go
agent:
machine:
type: e1-standard-2
os_image: ubuntu2004
blocks:
- name: Test
task:
jobs:
- name: go test
commands:
- sem-version go 1.18
- export GOPATH=~/go
- 'export PATH=/home/semaphore/go/bin:$PATH'
- checkout
- go get ./...
- go test ./...
47 changes: 29 additions & 18 deletions filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package pongo2

import (
"fmt"
"sync"
)

// FilterFunction is the type filter functions must fulfil
type FilterFunction func(in *Value, param *Value) (out *Value, err *Error)
type FilterFunction func(in *Value, param *Value, bind map[string]any) (out *Value, err *Error)

var filters map[string]FilterFunction
// var filters map[string]FilterFunction
var filters *sync.Map

func init() {
filters = make(map[string]FilterFunction)
filters = new(sync.Map)
}

// FilterExists returns true if the given filter is already registered
func FilterExists(name string) bool {
_, existing := filters[name]
_, existing := filters.Load(name)
return existing
}

Expand All @@ -27,7 +29,8 @@ func RegisterFilter(name string, fn FilterFunction) error {
if FilterExists(name) {
return fmt.Errorf("filter with name '%s' is already registered", name)
}
filters[name] = fn

filters.Store(name, fn)
return nil
}

Expand All @@ -37,23 +40,29 @@ func ReplaceFilter(name string, fn FilterFunction) error {
if !FilterExists(name) {
return fmt.Errorf("filter with name '%s' does not exist (therefore cannot be overridden)", name)
}
filters[name] = fn
filters.Swap(name, fn)
return nil
}

// MustApplyFilter behaves like ApplyFilter, but panics on an error.
func MustApplyFilter(name string, value *Value, param *Value) *Value {
val, err := ApplyFilter(name, value, param)
if err != nil {
panic(err)
}
return val
func OverrideFilter(name string, fn FilterFunction) error {
filters.Delete(name)
filters.Store(name, fn)
return nil
}

// MustApplyFilter behaves like ApplyFilter, but panics on an error.
//func MustApplyFilter(name string, value *Value, param *Value) *Value {
// val, err := ApplyFilter(name, value, param)
// if err != nil {
// panic(err)
// }
// return val
//}

// ApplyFilter applies a filter to a given value using the given parameters.
// Returns a *pongo2.Value or an error.
func ApplyFilter(name string, value *Value, param *Value) (*Value, *Error) {
fn, existing := filters[name]
func ApplyFilter(name string, value *Value, param *Value, bind map[string]any) (*Value, *Error) {
storedValue, existing := filters.Load(name)
if !existing {
return nil, &Error{
Sender: "applyfilter",
Expand All @@ -66,7 +75,8 @@ func ApplyFilter(name string, value *Value, param *Value) (*Value, *Error) {
param = AsValue(nil)
}

return fn(value, param)
fn, _ := storedValue.(FilterFunction)
return fn(value, param, bind)
}

type filterCall struct {
Expand All @@ -91,7 +101,7 @@ func (fc *filterCall) Execute(v *Value, ctx *ExecutionContext) (*Value, *Error)
param = AsValue(nil)
}

filteredValue, err := fc.filterFunc(v, param)
filteredValue, err := fc.filterFunc(v, param, ctx.Public)
if err != nil {
return nil, err.updateFromTokenIfNeeded(ctx.template, fc.token)
}
Expand All @@ -113,10 +123,11 @@ func (p *Parser) parseFilter() (*filterCall, *Error) {
}

// Get the appropriate filter function and bind it
filterFn, exists := filters[identToken.Val]
storedFunc, exists := filters.Load(identToken.Val)
if !exists {
return nil, p.Error(fmt.Sprintf("Filter '%s' does not exist.", identToken.Val), identToken)
}
filterFn, _ := storedFunc.(FilterFunction)

filter.filterFunc = filterFn

Expand Down
Loading