File: predicate.go

package info (click to toggle)
golang-github-vulcand-predicate 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 11
file content (102 lines) | stat: -rw-r--r-- 3,021 bytes parent folder | download
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
/*
Copyright 2014-2018 Vulcand Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

/*
Predicate package used to create interpreted mini languages with Go syntax - mostly to define
various predicates for configuration, e.g. Latency() > 40 || ErrorRate() > 0.5.

Here's an example of fully functional predicate language to deal with division remainders:

    // takes number and returns true or false
    type numberPredicate func(v int) bool

    // Converts one number to another
    type numberMapper func(v int) int

    // Function that creates predicate to test if the remainder is 0
    func divisibleBy(divisor int) numberPredicate {
	    return func(v int) bool {
		    return v%divisor == 0
        }
    }

    // Function - logical operator AND that combines predicates
    func numberAND(a, b numberPredicate) numberPredicate {
        return func(v int) bool {
            return a(v) && b(v)
        }
    }

    p, err := NewParser(Def{
		Operators: Operators{
			AND: numberAND,
		},
		Functions: map[string]interface{}{
			"DivisibleBy": divisibleBy,
		},
	})

	pr, err := p.Parse("DivisibleBy(2) && DivisibleBy(3)")
    if err == nil {
        fmt.Fatalf("Error: %v", err)
    }
    pr.(numberPredicate)(2) // false
    pr.(numberPredicate)(3) // false
    pr.(numberPredicate)(6) // true
*/
package predicate

// Def contains supported operators (e.g. LT, GT) and functions passed in as a map.
type Def struct {
	Operators Operators
	// Function matching is case sensitive, e.g. Len is different from len
	Functions map[string]interface{}
	// GetIdentifier returns value of any identifier passed in
	// in the form []string{"id", "field", "subfield"}
	GetIdentifier GetIdentifierFn
	// GetProperty returns property from a map
	GetProperty GetPropertyFn
}

// GetIdentifierFn function returns identifier based on selector
// e.g. id.field.subfield will be passed as.
// GetIdentifierFn([]string{"id", "field", "subfield"})
type GetIdentifierFn func(selector []string) (interface{}, error)

// GetPropertyFn reuturns property from a mapVal by key keyVal
type GetPropertyFn func(mapVal, keyVal interface{}) (interface{}, error)

// Operators contain functions for equality and logical comparison.
type Operators struct {
	EQ  interface{}
	NEQ interface{}

	LT interface{}
	GT interface{}

	LE interface{}
	GE interface{}

	OR  interface{}
	AND interface{}
	NOT interface{}
}

// Parser takes the string with expression and calls the operators and functions.
type Parser interface {
	Parse(string) (interface{}, error)
}