Skip to content

Commit

Permalink
add in operator conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovannaMonti committed Apr 27, 2023
1 parent 706d53c commit 0e9f266
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 8 additions & 0 deletions internal/opatranslator/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
EqOp = "eq"
EqualOp = "equal"
NeqOp = "neq"
InOp = "internal.member_2"
)

var rangeOperatorStrategies = map[string]func(pipeline *[]bson.M, fieldName string, fieldValue interface{}){
Expand All @@ -40,6 +41,7 @@ var rangeOperatorStrategies = map[string]func(pipeline *[]bson.M, fieldName stri
EqOp: HandleEquals,
EqualOp: HandleEquals,
NeqOp: HandleNotEquals,
InOp: HandleIn,
}

func HandleOperations(operation string, pipeline *[]bson.M, fieldName string, fieldValue interface{}) bool {
Expand All @@ -56,6 +58,12 @@ func HandleEquals(pipeline *[]bson.M, fieldName string, fieldValue interface{})
*pipeline = append(*pipeline, filter)
}

// Parse the in operator into equivalent mongo query.
func HandleIn(pipeline *[]bson.M, fieldName string, fieldValue interface{}) {
filter := bson.M{fieldName: bson.M{"$in": fieldValue}}
*pipeline = append(*pipeline, filter)
}

// Parse the != into equivalent mongo query.
func HandleNotEquals(pipeline *[]bson.M, fieldName string, fieldValue interface{}) {
filter := bson.M{fieldName: bson.M{"$ne": fieldValue}}
Expand Down
5 changes: 4 additions & 1 deletion service/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,9 @@ allow {

t.Run("sends filter query with $in", func(t *testing.T) {
policy := `package policies
import future.keywords.in
allow {
input.request.method == "GET"
Expand Down Expand Up @@ -860,7 +863,7 @@ allow {

require.Equal(t, http.StatusOK, w.Result().StatusCode, "Unexpected status code.")
filterQuery := r.Header.Get("rowfilterquery")
expectedQuery := `{"$or":[{"$and":[{"membership":{"$in":["manager_test"]}}]},{"$and":[{"salary":{"$gt":0}}]}]}`
expectedQuery := `{"$or":[{"$and":[{"membership":{"$in":"member_test"}}]},{"$and":[{"salary":{"$gt":0}}]}]}`
require.Equal(t, expectedQuery, filterQuery)
})

Expand Down

0 comments on commit 0e9f266

Please sign in to comment.