Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion pkg/datastore/filedb/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ func filter(col datastore.Collection, e interface{}, filters []datastore.ListFil
return false, nil
}

cmp, err := compare(val, filter.Value, filter.Operator)
operand, err := normalizeFieldValue(filter.Value)
if err != nil {
return false, err
}

cmp, err := compare(val, operand, filter.Operator)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -220,3 +225,33 @@ func normalizeFieldName(key string) string {
}
return strings.ToLower(string(key[0])) + key[1:]
}

// normalizeFieldValue converts value of any type to the primitive type
// Note: Find a better way to handle this instead of marshal/unmarshal.
func normalizeFieldValue(val interface{}) (interface{}, error) {
var needConvert = false
switch val.(type) {
case int, int8, int16, int32, int64:
case uint, uint8, uint16, uint32:
case float32, float64:
case string:
case bool:
default:
needConvert = true
}

if !needConvert {
return val, nil
}

raw, err := json.Marshal(val)
if err != nil {
return nil, err
}

var out interface{}
if err = json.Unmarshal(raw, &out); err != nil {
return nil, err
}
return out, nil
}
26 changes: 25 additions & 1 deletion pkg/datastore/filedb/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func TestFilter(t *testing.T) {
{
Field: "Kind",
Operator: datastore.OperatorEqual,
Value: 0,
Value: model.ApplicationKind_KUBERNETES,
},
},
expect: true,
Expand Down Expand Up @@ -316,6 +316,30 @@ func TestFilter(t *testing.T) {
},
expect: true,
},
{
name: "filter with IN operator - passed",
entity: &model.Deployment{Status: model.DeploymentStatus_DEPLOYMENT_PENDING},
filters: []datastore.ListFilter{
{
Field: "Status",
Operator: datastore.OperatorIn,
Value: model.GetNotCompletedDeploymentStatuses(),
},
},
expect: true,
},
{
name: "filter with IN operator - not passed",
entity: &model.Deployment{Status: model.DeploymentStatus_DEPLOYMENT_CANCELLED},
filters: []datastore.ListFilter{
{
Field: "Status",
Operator: datastore.OperatorIn,
Value: model.GetNotCompletedDeploymentStatuses(),
},
},
expect: false,
},
}

for _, tc := range testcases {
Expand Down