forked from Knetic/govaluate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameters.go
49 lines (39 loc) · 1.04 KB
/
parameters.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
package govaluate
import (
"errors"
"strings"
)
/*
Parameters is a collection of named parameters that can be used by an EvaluableExpression to retrieve parameters
when an expression tries to use them.
*/
type Parameters interface {
/*
Get gets the parameter of the given name, or an error if the parameter is unavailable.
Failure to find the given parameter should be indicated by returning an error.
*/
Get(name string) (interface{}, error)
}
type MapParameters map[string]interface{}
func (p MapParameters) Get(name string) (interface{}, error) {
parts := strings.Split(name, ".")
var value interface{}
current := p
for i, part := range parts {
var found bool
value, found = current[part]
if !found {
errorMessage := "No parameter '" + name + "' found."
return nil, errors.New(errorMessage)
}
if i != len(parts) - 1 {
var ok bool
current, ok = value.(map[string]interface{})
if !ok {
errorMessage := "No parameter '" + name + "' found."
return nil, errors.New(errorMessage)
}
}
}
return value, nil
}