File tree Expand file tree Collapse file tree 3 files changed +32
-7
lines changed
decision/evaluator/matchers Expand file tree Collapse file tree 3 files changed +32
-7
lines changed Original file line number Diff line number Diff line change @@ -28,11 +28,5 @@ type ExistsMatcher struct {
28
28
29
29
// Match returns true if the user's attribute is in the condition
30
30
func (m ExistsMatcher ) Match (user entities.UserContext ) (bool , error ) {
31
-
32
- _ , err := user .GetStringAttribute (m .Condition .Name )
33
- if err != nil {
34
- return false , nil
35
- }
36
-
37
- return true , nil
31
+ return user .CheckAttributeExists (m .Condition .Name ), nil
38
32
}
Original file line number Diff line number Diff line change @@ -31,6 +31,15 @@ type UserContext struct {
31
31
Attributes map [string ]interface {}
32
32
}
33
33
34
+ // CheckAttributeExists returns whether the specified attribute name exists in the attributes map.
35
+ func (u UserContext ) CheckAttributeExists (attrName string ) bool {
36
+ if value , ok := u .Attributes [attrName ]; ok && value != nil {
37
+ return true
38
+ }
39
+
40
+ return false
41
+ }
42
+
34
43
// GetStringAttribute returns the string value for the specified attribute name in the attributes map. Returns error if not found.
35
44
func (u UserContext ) GetStringAttribute (attrName string ) (string , error ) {
36
45
if value , ok := u .Attributes [attrName ]; ok {
Original file line number Diff line number Diff line change @@ -23,6 +23,28 @@ import (
23
23
"github.com/stretchr/testify/assert"
24
24
)
25
25
26
+ func TestUserAttributeExists (t * testing.T ) {
27
+ userContext := UserContext {
28
+ Attributes : map [string ]interface {}{
29
+ "string_foo" : "foo" ,
30
+ "bool_true" : true ,
31
+ "bool_false" : false ,
32
+ "null_value" : nil ,
33
+ },
34
+ }
35
+
36
+ // Test happy path
37
+ assert .Equal (t , true , userContext .CheckAttributeExists ("string_foo" ))
38
+ assert .Equal (t , true , userContext .CheckAttributeExists ("bool_true" ))
39
+ assert .Equal (t , true , userContext .CheckAttributeExists ("bool_false" ))
40
+
41
+ // Test non-existent attr name
42
+ assert .Equal (t , false , userContext .CheckAttributeExists ("invalid" ))
43
+
44
+ // Test null value
45
+ assert .Equal (t , false , userContext .CheckAttributeExists ("null_value" ))
46
+ }
47
+
26
48
func TestUserAttributesGetStringAttribute (t * testing.T ) {
27
49
userContext := UserContext {
28
50
Attributes : map [string ]interface {}{
You can’t perform that action at this time.
0 commit comments