-
Notifications
You must be signed in to change notification settings - Fork 5
/
condition_empty.go
76 lines (59 loc) · 1.87 KB
/
condition_empty.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
package restrict
import (
"fmt"
"reflect"
)
const (
// EmptyConditionType - EmptyCondition's type identifier.
EmptyConditionType = "EMPTY"
// NotEmptyConditionType - NotEmptyCondition's type identifier.
NotEmptyConditionType = "NOT_EMPTY"
)
// baseEmptyCondition - describes fields needed by Empty/NotEmpty Conditions.
type baseEmptyCondition struct {
// ID - Condition's id, useful when there is a need to identify failing Condition.
ID string `json:"name,omitempty" yaml:"name,omitempty"`
// Value - ValueDescriptor for the value being checked.
Value *ValueDescriptor `json:"value" yaml:"value"`
}
// EmptyCondition - Condition for testing whether given value is empty.
type EmptyCondition baseEmptyCondition
// Type - returns Condition's type.
func (c *EmptyCondition) Type() string {
return EmptyConditionType
}
// Check - returns true if value is empty (zero-like), false otherwise.
func (c *EmptyCondition) Check(request *AccessRequest) error {
value, err := c.Value.GetValue(request)
if err != nil {
return err
}
if value == nil {
return nil
}
empty := reflect.ValueOf(value).IsZero()
if !empty {
return NewConditionNotSatisfiedError(c, request, fmt.Errorf("value \"%v\" is not empty", value))
}
return nil
}
type NotEmptyCondition baseEmptyCondition
// Type - returns Condition's type.
func (c *NotEmptyCondition) Type() string {
return NotEmptyConditionType
}
// Check - returns true if value is not empty (zero-like), false otherwise.
func (c *NotEmptyCondition) Check(request *AccessRequest) error {
value, err := c.Value.GetValue(request)
if err != nil {
return err
}
if value == nil {
return NewConditionNotSatisfiedError(c, request, fmt.Errorf("value \"%v\" is empty", value))
}
empty := reflect.ValueOf(value).IsZero()
if empty {
return NewConditionNotSatisfiedError(c, request, fmt.Errorf("value \"%v\" is empty", value))
}
return nil
}