File: condition.go

package info (click to toggle)
golang-github-ovn-org-libovsdb 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,440 kB
  • sloc: makefile: 52; sh: 14
file content (223 lines) | stat: -rw-r--r-- 6,073 bytes parent folder | download | duplicates (2)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package ovsdb

import (
	"encoding/json"
	"fmt"
	"reflect"
)

type ConditionFunction string
type WaitCondition string

const (
	// ConditionLessThan is the less than condition
	ConditionLessThan ConditionFunction = "<"
	// ConditionLessThanOrEqual is the less than or equal condition
	ConditionLessThanOrEqual ConditionFunction = "<="
	// ConditionEqual is the equal condition
	ConditionEqual ConditionFunction = "=="
	// ConditionNotEqual is the not equal condition
	ConditionNotEqual ConditionFunction = "!="
	// ConditionGreaterThan is the greater than condition
	ConditionGreaterThan ConditionFunction = ">"
	// ConditionGreaterThanOrEqual is the greater than or equal condition
	ConditionGreaterThanOrEqual ConditionFunction = ">="
	// ConditionIncludes is the includes condition
	ConditionIncludes ConditionFunction = "includes"
	// ConditionExcludes is the excludes condition
	ConditionExcludes ConditionFunction = "excludes"

	// WaitConditionEqual is the equal condition
	WaitConditionEqual WaitCondition = "=="
	// WaitConditionNotEqual is the not equal condition
	WaitConditionNotEqual WaitCondition = "!="
)

// Condition is described in RFC 7047: 5.1
type Condition struct {
	Column   string
	Function ConditionFunction
	Value    interface{}
}

func (c Condition) String() string {
	return fmt.Sprintf("where column %s %s %v", c.Column, c.Function, c.Value)
}

// NewCondition returns a new condition
func NewCondition(column string, function ConditionFunction, value interface{}) Condition {
	return Condition{
		Column:   column,
		Function: function,
		Value:    value,
	}
}

// MarshalJSON marshals a condition to a 3 element JSON array
func (c Condition) MarshalJSON() ([]byte, error) {
	v := []interface{}{c.Column, c.Function, c.Value}
	return json.Marshal(v)
}

// UnmarshalJSON converts a 3 element JSON array to a Condition
func (c *Condition) UnmarshalJSON(b []byte) error {
	var v []interface{}
	err := json.Unmarshal(b, &v)
	if err != nil {
		return err
	}
	if len(v) != 3 {
		return fmt.Errorf("expected a 3 element json array. there are %d elements", len(v))
	}
	c.Column = v[0].(string)
	function := ConditionFunction(v[1].(string))
	switch function {
	case ConditionEqual,
		ConditionNotEqual,
		ConditionIncludes,
		ConditionExcludes,
		ConditionGreaterThan,
		ConditionGreaterThanOrEqual,
		ConditionLessThan,
		ConditionLessThanOrEqual:
		c.Function = function
	default:
		return fmt.Errorf("%s is not a valid function", function)
	}
	vv, err := ovsSliceToGoNotation(v[2])
	if err != nil {
		return err
	}
	c.Value = vv
	return nil
}

// Evaluate will evaluate the condition on the two provided values
// The conditions operately differently depending on the type of
// the provided values. The behavior is as described in RFC7047
func (c ConditionFunction) Evaluate(a interface{}, b interface{}) (bool, error) {
	x := reflect.ValueOf(a)
	y := reflect.ValueOf(b)
	if x.Kind() != y.Kind() {
		return false, fmt.Errorf("comparison between %s and %s not supported", x.Kind(), y.Kind())
	}
	switch c {
	case ConditionEqual:
		return reflect.DeepEqual(a, b), nil
	case ConditionNotEqual:
		return !reflect.DeepEqual(a, b), nil
	case ConditionIncludes:
		switch x.Kind() {
		case reflect.Slice:
			return sliceContains(x, y), nil
		case reflect.Map:
			return mapContains(x, y), nil
		case reflect.Int, reflect.Float64, reflect.Bool, reflect.String:
			return reflect.DeepEqual(a, b), nil
		default:
			return false, fmt.Errorf("condition not supported on %s", x.Kind())
		}
	case ConditionExcludes:
		switch x.Kind() {
		case reflect.Slice:
			return !sliceContains(x, y), nil
		case reflect.Map:
			return !mapContains(x, y), nil
		case reflect.Int, reflect.Float64, reflect.Bool, reflect.String:
			return !reflect.DeepEqual(a, b), nil
		default:
			return false, fmt.Errorf("condition not supported on %s", x.Kind())
		}
	case ConditionGreaterThan:
		switch x.Kind() {
		case reflect.Int:
			return x.Int() > y.Int(), nil
		case reflect.Float64:
			return x.Float() > y.Float(), nil
		case reflect.Bool, reflect.String, reflect.Slice, reflect.Map:
		default:
			return false, fmt.Errorf("condition not supported on %s", x.Kind())
		}
	case ConditionGreaterThanOrEqual:
		switch x.Kind() {
		case reflect.Int:
			return x.Int() >= y.Int(), nil
		case reflect.Float64:
			return x.Float() >= y.Float(), nil
		case reflect.Bool, reflect.String, reflect.Slice, reflect.Map:
		default:
			return false, fmt.Errorf("condition not supported on %s", x.Kind())
		}
	case ConditionLessThan:
		switch x.Kind() {
		case reflect.Int:
			return x.Int() < y.Int(), nil
		case reflect.Float64:
			return x.Float() < y.Float(), nil
		case reflect.Bool, reflect.String, reflect.Slice, reflect.Map:
		default:
			return false, fmt.Errorf("condition not supported on %s", x.Kind())
		}
	case ConditionLessThanOrEqual:
		switch x.Kind() {
		case reflect.Int:
			return x.Int() <= y.Int(), nil
		case reflect.Float64:
			return x.Float() <= y.Float(), nil
		case reflect.Bool, reflect.String, reflect.Slice, reflect.Map:
		default:
			return false, fmt.Errorf("condition not supported on %s", x.Kind())
		}
	default:
		return false, fmt.Errorf("unsupported condition function %s", c)
	}
	// we should never get here
	return false, fmt.Errorf("unreachable condition")
}

func sliceContains(x, y reflect.Value) bool {
	for i := 0; i < y.Len(); i++ {
		found := false
		vy := y.Index(i)
		for j := 0; j < x.Len(); j++ {
			vx := x.Index(j)
			if vy.Kind() == reflect.Interface {
				if vy.Elem() == vx.Elem() {
					found = true
					break
				}
			} else {
				if vy.Interface() == vx.Interface() {
					found = true
					break
				}
			}
		}
		if !found {
			return false
		}
	}
	return true
}

func mapContains(x, y reflect.Value) bool {
	iter := y.MapRange()
	for iter.Next() {
		k := iter.Key()
		v := iter.Value()
		vx := x.MapIndex(k)
		if !vx.IsValid() {
			return false
		}
		if v.Kind() != reflect.Interface {
			if v.Interface() != vx.Interface() {
				return false
			}
		} else {
			if v.Elem() != vx.Elem() {
				return false
			}
		}
	}
	return true
}