Skip to content

Commit

Permalink
Merge pull request #3 from b1zzu/improve-equal
Browse files Browse the repository at this point in the history
refactor: switch to cmp.Equal for compare objects
  • Loading branch information
b1zzu committed Feb 4, 2022
2 parents 002a23e + 883f188 commit a85b133
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 393 deletions.
110 changes: 22 additions & 88 deletions pkg/rpdac/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"io"
"log"
"reflect"
"sort"
"strings"

"github.com/b1zzu/reportportal-dashboards-as-code/pkg/reportportal"
"github.com/b1zzu/reportportal-dashboards-as-code/pkg/util"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

type IDashboardService interface {
Expand Down Expand Up @@ -432,96 +433,29 @@ func (d *Dashboard) GetKind() ObjectKind {
return d.Kind
}

func (left *Dashboard) Equals(o util.Comparable) bool {
// Compare the two Dashboards ignoring slices order
func (left *Dashboard) Equals(right *Dashboard) bool {

// TODO: Can be substiuted with cmp.Equal() if we can make it order indipendent for slices
opts := cmp.Options{
cmpopts.IgnoreUnexported(Dashboard{}, Widget{}),

if left == nil || o == nil {
return left == o
}

right, ok := o.(*Dashboard)
if !ok {
return false
}

if left.Name != right.Name || left.Description != right.Description {
return false
}
// sort Widgets
cmp.Transformer("SortWidgets", func(in []*Widget) []*Widget {
out := make([]*Widget, len(in))
copy(out, in) // copy input to avoid mutating it
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
return out
}),

if left.Widgets == nil || right.Widgets == nil {
return left.Widgets == nil && right.Widgets == nil
// sort strings (Widget.Filters, WidgetContentParameters.ContentFields)
cmp.Transformer("SortStrings", func(in []string) []string {
out := make([]string, len(in))
copy(out, in) // copy input to avoid mutating it
sort.Strings(out)
return out
}),
}

leftWidgets := make([]util.Comparable, len(left.Widgets))
for i := range left.Widgets {
leftWidgets[i] = left.Widgets[i]
}

rightWidgets := make([]util.Comparable, len(right.Widgets))
for i := range right.Widgets {
rightWidgets[i] = right.Widgets[i]
}

return util.CompareSlices(leftWidgets, rightWidgets)
}

func (d *Dashboard) Key() string {
return d.Name
}

func (left *Widget) Equals(o util.Comparable) bool {
if left == nil || o == nil {
return left == o
}

right, ok := o.(*Widget)
if !ok {
return false
}

return left.Name == right.Name &&
left.Description == right.Description &&
left.WidgetType == right.WidgetType &&
left.WidgetSize.Equals(&right.WidgetSize) &&
util.CompareStringSlices(left.Filters, right.Filters) &&
left.WidgetPosition.Equals(&right.WidgetPosition) &&
left.ContentParameters.Equals(&right.ContentParameters)
}

func (d *Widget) Key() string {
return d.Name
}

func (left *WidgetSize) Equals(right *WidgetSize) bool {

if left == nil || right == nil {
return left == right
}

return left.Height == right.Height &&
left.Width == right.Width
}

func (left *WidgetPosition) Equals(right *WidgetPosition) bool {

if left == nil || right == nil {
return left == right
}

return left.PositionX == right.PositionX &&
left.PositionY == right.PositionY
}

func (left *WidgetContentParameters) Equals(right *WidgetContentParameters) bool {

if left == nil || right == nil {
return left == right
}

return util.CompareStringSlices(left.ContentFields, right.ContentFields) &&
left.ItemsCount == right.ItemsCount &&
reflect.DeepEqual(right.WidgetOptions, left.WidgetOptions)
return cmp.Equal(left, right, opts)
}

func (d *Dashboard) HashName() string {
Expand Down
10 changes: 7 additions & 3 deletions pkg/rpdac/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1840,14 +1840,14 @@ func TestDashboardEquals(t *testing.T) {
expexct: false,
},
{
description: "Compare dashboards wehre one with nil widgets should return false",
description: "Compare dashboards wehre one with nil widgets should return true",
left: &Dashboard{
Widgets: []*Widget{},
},
right: &Dashboard{
Widgets: nil,
},
expexct: false,
expexct: true,
},
{
description: "Compare dashboards with different widgets should return false",
Expand Down Expand Up @@ -2078,7 +2078,11 @@ func TestWidgetEquals(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
r := test.left.Equals(test.right)

left := &Dashboard{Widgets: []*Widget{test.left}}
right := &Dashboard{Widgets: []*Widget{test.right}}

r := left.Equals(right)
if r != test.expexct {
t.Errorf("expected '%t' but got '%t'", test.expexct, r)
}
Expand Down
83 changes: 27 additions & 56 deletions pkg/rpdac/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package rpdac
import (
"fmt"
"log"
"sort"

"github.com/b1zzu/reportportal-dashboards-as-code/pkg/reportportal"
"github.com/b1zzu/reportportal-dashboards-as-code/pkg/util"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -209,60 +211,29 @@ func (f *Filter) GetKind() ObjectKind {
return f.Kind
}

func conditionsToStringSlice(conditions []FilterCondition) []string {
s := make([]string, len(conditions))
for i, c := range conditions {
s[i] = c.Condition + c.FilteringField + c.Value
}
return s
}

func ordersToStringSlice(orders []FilterOrder) []string {
s := make([]string, len(orders))
for i, o := range orders {
sort := "dsc"
if o.IsAsc {
sort = "asc"
}

s[i] = o.SortingColumn + sort
}
return s
}

func (left *Filter) Equals(right *Filter) bool {
if left == nil || right == nil {
return left == right
}

if left.Name != right.Name ||
left.Type != right.Type ||
left.Description != right.Description {

return false
}

// compare conditions
if left.Conditions != nil && right.Conditions != nil {

leftConditions := conditionsToStringSlice(left.Conditions)
rightCondition := conditionsToStringSlice(right.Conditions)

if !util.CompareStringSlices(leftConditions, rightCondition) {
return false
}

} else if !(left.Conditions == nil && right.Conditions == nil) {
return false
}

// compare orders
if left.Orders == nil || right.Orders == nil {
return left.Orders == nil && right.Orders == nil
}

leftOrders := ordersToStringSlice(left.Orders)
rightOrders := ordersToStringSlice(right.Orders)

return util.CompareStringSlices(leftOrders, rightOrders)
opts := cmp.Options{
cmpopts.IgnoreUnexported(Filter{}),

// sort FilterConditions
cmp.Transformer("SortConditions", func(in []FilterCondition) []FilterCondition {
out := make([]FilterCondition, len(in))
copy(out, in) // copy input to avoid mutating it
sort.Slice(out, func(i, j int) bool {
return fmt.Sprintf("%+v", out[i]) < fmt.Sprintf("%+v", out[j])
})
return out
}),

// sort FilterOrders
cmp.Transformer("SortOrders", func(in []FilterOrder) []FilterOrder {
out := make([]FilterOrder, len(in))
copy(out, in) // copy input to avoid mutating it
sort.Slice(out, func(i, j int) bool {
return fmt.Sprintf("%+v", out[i]) < fmt.Sprintf("%+v", out[j])
})
return out
}),
}
return cmp.Equal(left, right, opts)
}
8 changes: 4 additions & 4 deletions pkg/rpdac/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,24 +707,24 @@ func TestFilterEquals(t *testing.T) {
expexct: false,
},
{
description: "Compare filters where one has nil conditions should return false",
description: "Compare filters where one has nil conditions should return true",
left: &Filter{
Conditions: []FilterCondition{},
},
right: &Filter{
Conditions: nil,
},
expexct: false,
expexct: true,
},
{
description: "Compare filters where one has nil orders should return false",
description: "Compare filters where one has nil orders should return true",
left: &Filter{
Orders: []FilterOrder{},
},
right: &Filter{
Orders: nil,
},
expexct: false,
expexct: true,
},
{
description: "Compare filters with different condtions should return false",
Expand Down
93 changes: 0 additions & 93 deletions pkg/util/equal.go

This file was deleted.

0 comments on commit a85b133

Please sign in to comment.