Skip to content

Commit

Permalink
updating filter framework with generics (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
jriddle-linode authored Jun 29, 2022
1 parent 8939503 commit bf1276f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (

type FilterNode interface {
Key() string
JSONValueSegment() interface{}
JSONValueSegment() any
}

type Filter struct {
Expand All @@ -33,12 +33,12 @@ type Filter struct {
Order string
}

func (f *Filter) AddField(op FilterOperator, key string, value interface{}) {
func (f *Filter) AddField(op FilterOperator, key string, value any) {
f.Children = append(f.Children, &Comp{key, op, value})
}

func (f *Filter) MarshalJSON() ([]byte, error) {
result := make(map[string]interface{})
result := make(map[string]any)

if f.OrderBy != "" {
result["+order_by"] = f.OrderBy
Expand All @@ -56,9 +56,9 @@ func (f *Filter) MarshalJSON() ([]byte, error) {
return json.Marshal(result)
}

fields := make([]map[string]interface{}, len(f.Children))
fields := make([]map[string]any, len(f.Children))
for i, c := range f.Children {
fields[i] = map[string]interface{}{
fields[i] = map[string]any{
c.Key(): c.JSONValueSegment(),
}
}
Expand All @@ -71,19 +71,19 @@ func (f *Filter) MarshalJSON() ([]byte, error) {
type Comp struct {
Column string
Operator FilterOperator
Value interface{}
Value any
}

func (c *Comp) Key() string {
return c.Column
}

func (c *Comp) JSONValueSegment() interface{} {
func (c *Comp) JSONValueSegment() any {
if c.Operator == Eq {
return c.Value
}

return map[string]interface{}{
return map[string]any{
string(c.Operator): c.Value,
}
}
Expand Down
24 changes: 12 additions & 12 deletions comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

func TestFilter(t *testing.T) {
expected := map[string]interface{}{
"vcpus": map[string]interface{}{
expected := map[string]any{
"vcpus": map[string]any{
"+gte": 12,
},
"class": "standard",
Expand All @@ -34,8 +34,8 @@ func TestFilter(t *testing.T) {
}

func TestFilterAscending(t *testing.T) {
expected := map[string]interface{}{
"vcpus": map[string]interface{}{
expected := map[string]any{
"vcpus": map[string]any{
"+gte": 12,
},
"class": "standard",
Expand Down Expand Up @@ -66,8 +66,8 @@ func TestFilterAscending(t *testing.T) {
}

func TestFilterDescending(t *testing.T) {
expected := map[string]interface{}{
"vcpus": map[string]interface{}{
expected := map[string]any{
"vcpus": map[string]any{
"+gte": 12,
},
"class": "standard",
Expand Down Expand Up @@ -98,10 +98,10 @@ func TestFilterDescending(t *testing.T) {
}

func TestFilterAnd(t *testing.T) {
expected := map[string]interface{}{
"+and": []map[string]interface{}{
expected := map[string]any{
"+and": []map[string]any{
{
"vcpus": map[string]interface{}{
"vcpus": map[string]any{
"+gte": 12,
},
},
Expand Down Expand Up @@ -131,10 +131,10 @@ func TestFilterAnd(t *testing.T) {
}

func TestFilterOr(t *testing.T) {
expected := map[string]interface{}{
"+or": []map[string]interface{}{
expected := map[string]any{
"+or": []map[string]any{
{
"vcpus": map[string]interface{}{
"vcpus": map[string]any{
"+gte": 12,
},
},
Expand Down

0 comments on commit bf1276f

Please sign in to comment.