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 {
2828
2929// Match returns true if the user's attribute is in the condition
3030func (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
3832}
Original file line number Diff line number Diff line change @@ -31,6 +31,15 @@ type UserContext struct {
3131 Attributes map [string ]interface {}
3232}
3333
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+
3443// GetStringAttribute returns the string value for the specified attribute name in the attributes map. Returns error if not found.
3544func (u UserContext ) GetStringAttribute (attrName string ) (string , error ) {
3645 if value , ok := u .Attributes [attrName ]; ok {
Original file line number Diff line number Diff line change @@ -23,6 +23,28 @@ import (
2323 "github.com/stretchr/testify/assert"
2424)
2525
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+
2648func TestUserAttributesGetStringAttribute (t * testing.T ) {
2749 userContext := UserContext {
2850 Attributes : map [string ]interface {}{
You can’t perform that action at this time.
0 commit comments