forked from raystack/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add validation in deletion query and remove redundant code
- Loading branch information
Muhammad Luthfi Fahlevi
committed
Aug 12, 2024
1 parent
57897ea
commit e4903d0
Showing
15 changed files
with
264 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package generichelper_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
generichelper "github.com/goto/compass/pkg/generic_helper" | ||
) | ||
|
||
func TestContains(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
arr []string | ||
target string | ||
expected bool | ||
}{ | ||
{"Found", []string{"Alice", "Bob", "Carol"}, "Bob", true}, | ||
{"Not Found", []string{"Alice", "Bob", "Carol"}, "Dave", false}, | ||
{"Empty Array", []string{}, "Bob", false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := generichelper.Contains(tt.arr, tt.target) | ||
if result != tt.expected { | ||
t.Errorf("Contains(%v, %v) = %v; want %v", tt.arr, tt.target, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetJSONTags(t *testing.T) { | ||
type Person struct { | ||
ID int `json:"id"` | ||
Name string `json:"name"` | ||
Age int `json:"age"` | ||
CreatedAt string `json:"created_at"` | ||
} | ||
|
||
p := Person{} | ||
expectedTags := []string{"id", "name", "age", "created_at"} | ||
|
||
result := generichelper.GetJSONTags(p) | ||
if !reflect.DeepEqual(result, expectedTags) { | ||
t.Errorf("GetJSONTags(%v) = %v; want %v", p, result, expectedTags) | ||
} | ||
} | ||
|
||
func TestGetMapKeys(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input map[string]int | ||
expected []string | ||
}{ | ||
{"Simple Map", map[string]int{"Alice": 30, "Bob": 25, "Carol": 27}, []string{"Alice", "Bob", "Carol"}}, | ||
{"Empty Map", map[string]int{}, nil}, | ||
{"Single Element", map[string]int{"Alice": 30}, []string{"Alice"}}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := generichelper.GetMapKeys(tt.input) | ||
if !CompareSlices(result, tt.expected) { | ||
t.Errorf("GetMapKeys(%v) = %v; want %v", tt.input, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// CompareSlices checks if two slices contain the same elements, regardless of order. | ||
func CompareSlices[T comparable](a, b []T) bool { | ||
if a == nil && b == nil { | ||
return true | ||
} | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
counts := make(map[T]int) | ||
|
||
for _, item := range a { | ||
counts[item]++ | ||
} | ||
|
||
for _, item := range b { | ||
if counts[item] == 0 { | ||
return false | ||
} | ||
counts[item]-- | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package queryexpr | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/goto/compass/core/asset" | ||
generichelper "github.com/goto/compass/pkg/generic_helper" | ||
) | ||
|
||
type DeleteAssetExpr struct { | ||
ExprStr | ||
} | ||
|
||
func (d DeleteAssetExpr) ToQuery() (string, error) { | ||
return d.ExprStr.ToQuery() | ||
} | ||
|
||
func (d DeleteAssetExpr) Validate() error { | ||
identifiersWithOperator, err := GetIdentifiersMap(d.ExprStr.String()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
isExist := func(jsonTag string) bool { | ||
return identifiersWithOperator[jsonTag] != "" | ||
} | ||
mustExist := isExist("refreshed_at") && isExist("type") && isExist("service") | ||
if !mustExist { | ||
return fmt.Errorf("must exists these identifiers: refreshed_at, type, and service") | ||
} | ||
|
||
getOperator := func(jsonTag string) string { | ||
return identifiersWithOperator[jsonTag] | ||
} | ||
if getOperator("type") != "==" || getOperator("service") != "==" { | ||
return fmt.Errorf("identifier type and service must be equals operator (==)") | ||
} | ||
|
||
identifiers := generichelper.GetMapKeys(identifiersWithOperator) | ||
jsonTagsSchema := generichelper.GetJSONTags(asset.Asset{}) | ||
for _, identifier := range identifiers { | ||
isFieldValid := generichelper.Contains(jsonTagsSchema, identifier) | ||
if !isFieldValid { | ||
return fmt.Errorf("%s is not a valid identifier", identifier) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
pkg/query_expr/es_test_data/equals-or-not-in-condition.json
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.