-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilters.go
111 lines (92 loc) · 2.66 KB
/
filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package template
import (
"fmt"
"regexp"
)
// FilterFunc represents the signature of functions that can be applied as filters.
type FilterFunc func(interface{}, ...string) (interface{}, error)
var filters = make(map[string]FilterFunc)
// Global variable for validating filter names
var validFilterNameRegex = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*$`)
// RegisterFilter adds a filter to the global registry with name validation.
func RegisterFilter(name string, fn FilterFunc) error {
if !validFilterNameRegex.MatchString(name) {
return fmt.Errorf("%w: '%s'", ErrInvalidFilterName, name)
}
filters[name] = fn
return nil
}
// ApplyFilters executes a series of filters on a value within a context, supporting variable arguments.
func ApplyFilters(value interface{}, fs []Filter, ctx Context) (interface{}, error) {
var err error
for _, f := range fs {
fn, exists := filters[f.Name]
if !exists {
return value, fmt.Errorf("%w: filter '%s' not found", ErrFilterNotFound, f.Name)
}
// Prepare arguments by checking their types and extracting values for VariableArg.
args := make([]string, len(f.Args))
for i, arg := range f.Args {
switch arg := arg.(type) {
case StringArg:
args[i] = arg.Value().(string)
case NumberArg:
args[i] = fmt.Sprint(arg.Value())
case VariableArg:
val, err := ctx.Get(arg.Value().(string))
if err != nil {
return value, fmt.Errorf("%w: variable '%s' not found in context", ErrContextKeyNotFound, arg.Value().(string))
}
args[i] = fmt.Sprint(val)
default:
return value, fmt.Errorf("%w for filter '%s'", ErrUnknownFilterArgumentType, f.Name)
}
}
// Apply each filter with the prepared arguments.
value, err = fn(value, args...)
if err != nil {
return value, fmt.Errorf("error applying '%s' filter: %w", f.Name, err)
}
}
return value, nil
}
// Filter defines a transformation to apply to a template variable.
type Filter struct {
Name string
Args []FilterArg
}
// FilterArg represents the interface for filter arguments.
type FilterArg interface {
Value() interface{}
Type() string
}
// StringArg holds a string argument.
type StringArg struct {
val string
}
func (a StringArg) Value() interface{} {
return a.val
}
func (a StringArg) Type() string {
return "string"
}
// NumberArg holds a number argument.
type NumberArg struct {
val float64
}
func (a NumberArg) Value() interface{} {
return a.val
}
func (a NumberArg) Type() string {
return "number"
}
// VariableArg holds a variable argument.
type VariableArg struct {
name string
}
func (a VariableArg) Value() interface{} {
return a.name
}
func (a VariableArg) Type() string {
return "variable"
}