@@ -15,7 +15,7 @@ import (
1515// [RFC 9535]: https://www.rfc-editor.org/rfc/rfc9535.html
1616type Registry struct {
1717 mu sync.RWMutex
18- funcs map [string ]spec.PathFunction
18+ funcs map [string ]* spec.Func
1919}
2020
2121// New returns a new [Registry] loaded with the [RFC 9535]-mandated functions:
@@ -35,104 +35,52 @@ type Registry struct {
3535func New () * Registry {
3636 return & Registry {
3737 mu : sync.RWMutex {},
38- funcs : map [string ]spec.PathFunction {
39- "length" : NewFunction ("length" , spec .FuncValue , checkLengthArgs , lengthFunc ),
40- "count" : NewFunction ("count" , spec .FuncValue , checkCountArgs , countFunc ),
41- "value" : NewFunction ("value" , spec .FuncValue , checkValueArgs , valueFunc ),
42- "match" : NewFunction ("match" , spec .FuncLogical , checkMatchArgs , matchFunc ),
43- "search" : NewFunction ("search" , spec .FuncLogical , checkSearchArgs , searchFunc ),
38+ funcs : map [string ]* spec.Func {
39+ "length" : spec . NewFunction ("length" , spec .FuncValue , checkLengthArgs , lengthFunc ),
40+ "count" : spec . NewFunction ("count" , spec .FuncValue , checkCountArgs , countFunc ),
41+ "value" : spec . NewFunction ("value" , spec .FuncValue , checkValueArgs , valueFunc ),
42+ "match" : spec . NewFunction ("match" , spec .FuncLogical , checkMatchArgs , matchFunc ),
43+ "search" : spec . NewFunction ("search" , spec .FuncLogical , checkSearchArgs , searchFunc ),
4444 },
4545 }
4646}
4747
48- // Validator functions validate that the args expressions to a function can be
49- // processed by the function.
50- type Validator func (args []spec.FuncExprArg ) error
51-
52- // Evaluator functions execute a function against the values returned by args
53- // and returns a result.
54- type Evaluator func (args []spec.JSONPathValue ) spec.JSONPathValue
55-
5648// ErrRegister errors are returned by [Register].
5749var ErrRegister = errors .New ("register" )
5850
59- // Register registers a function extension by its name. Returns an
60- // [ErrRegister] error if r already contains the name of fn.
61- func (r * Registry ) Register (fn spec.PathFunction ) error {
51+ // Register registers a function extension by its name. Returns [ErrRegister]
52+ // if validator or nil or evaluator is nil or if r already contains name.
53+ func (r * Registry ) Register (
54+ name string ,
55+ resultType spec.FuncType ,
56+ validator spec.Validator ,
57+ evaluator spec.Evaluator ,
58+ ) error {
59+ if validator == nil {
60+ return fmt .Errorf ("%w: validator is nil" , ErrRegister )
61+ }
62+ if evaluator == nil {
63+ return fmt .Errorf ("%w: evaluator is nil" , ErrRegister )
64+ }
65+
6266 r .mu .Lock ()
6367 defer r .mu .Unlock ()
64- if _ , dup := r .funcs [fn . Name () ]; dup {
68+ if _ , dup := r .funcs [name ]; dup {
6569 return fmt .Errorf (
6670 "%w: Register called twice for function %v" ,
67- ErrRegister , fn . Name () ,
71+ ErrRegister , name ,
6872 )
6973 }
7074
71- r .funcs [fn . Name () ] = fn
75+ r .funcs [name ] = spec . NewFunction ( name , resultType , validator , evaluator )
7276 return nil
7377}
7478
7579// Get returns a reference to the registered function named name. Returns nil
7680// if no function with that name has been registered.
77- //
78- //nolint:ireturn
79- func (r * Registry ) Get (name string ) spec.PathFunction {
81+ func (r * Registry ) Get (name string ) * spec.Func {
8082 r .mu .RLock ()
8183 defer r .mu .RUnlock ()
8284 function := r .funcs [name ]
8385 return function
8486}
85-
86- // Function defines a JSONPath function. Use [Register] to register a new
87- // function. Implements [spec.PathFunction].
88- type Function struct {
89- // name is the name of the function. Must be unique among all functions in
90- // a registry.
91- name string
92-
93- // resultType defines the type of the function return value.
94- resultType spec.FuncType
95-
96- // validator executes at parse time to validate that all the args to
97- // the function are compatible with the function.
98- validator Validator
99-
100- // evaluator executes the function and returns the result of type
101- // resultType.
102- evaluator Evaluator
103- }
104-
105- // NewFunction creates a new JSONPath function extension. The parameters are:
106- //
107- // - name: the name of the function as used in JSONPath queries.
108- // - resultType: The data type of the function return value.
109- // - validator: A validation function that will be called by at parse time
110- // to validate that all the function args are compatible with the function.
111- // - evaluator: The implementation of the function itself that executes the
112- // against args and returns the result defined by resultType.
113- func NewFunction (
114- name string ,
115- resultType spec.FuncType ,
116- validator func (args []spec.FuncExprArg ) error ,
117- evaluator func (args []spec.JSONPathValue ) spec.JSONPathValue ,
118- ) * Function {
119- return & Function {name , resultType , validator , evaluator }
120- }
121-
122- // Name returns the name of the function.
123- func (f * Function ) Name () string { return f .name }
124-
125- // ResultType returns the data type of the function return value.
126- func (f * Function ) ResultType () spec.FuncType { return f .resultType }
127-
128- // Evaluate executes the function against args and returns the result of type
129- // [ResultType].
130- func (f * Function ) Evaluate (args []spec.JSONPathValue ) spec.JSONPathValue {
131- return f .evaluator (args )
132- }
133-
134- // Validate executes at parse time to validate that all the args to the
135- // function are compatible with the function.
136- func (f * Function ) Validate (args []spec.FuncExprArg ) error {
137- return f .validator ((args ))
138- }
0 commit comments