Skip to content

Commit fbaeab1

Browse files
yasirfolio3Michael Ng
authored andcommitted
fix(exists-condition) Fixed exists comparison for leaf condition. (#185)
1 parent 8f5043e commit fbaeab1

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

pkg/decision/evaluator/matchers/exists.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,5 @@ type ExistsMatcher struct {
2828

2929
// Match returns true if the user's attribute is in the condition
3030
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
3832
}

pkg/entities/user_context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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.
3544
func (u UserContext) GetStringAttribute(attrName string) (string, error) {
3645
if value, ok := u.Attributes[attrName]; ok {

pkg/entities/user_context_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
2648
func TestUserAttributesGetStringAttribute(t *testing.T) {
2749
userContext := UserContext{
2850
Attributes: map[string]interface{}{

0 commit comments

Comments
 (0)