Skip to content

Commit

Permalink
refactor: remove unnecessary slice types
Browse files Browse the repository at this point in the history
  • Loading branch information
irainia committed Aug 22, 2024
1 parent 236e44e commit b4aaa4a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
27 changes: 8 additions & 19 deletions core/asset/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,7 @@ const (
typeMetric Type = "metric"
)

var supportedTypes = []Type{
typeTable,
typeJob,
typeDashboard,
typeTopic,
typeFeatureTable,
typeApplication,
typeModel,
typeQuery,
typeMetric,
}

var isTypeSupported = map[Type]bool{
var supportedTypeMap = map[Type]bool{
typeTable: true,
typeJob: true,
typeDashboard: true,
Expand All @@ -54,8 +42,10 @@ var isTypeSupported = map[Type]bool{
}

func GetSupportedTypes() []Type {
output := make([]Type, 0, len(supportedTypes))
output = append(output, supportedTypes...)
output := make([]Type, 0, len(supportedTypeMap))
for _type := range supportedTypeMap {
output = append(output, _type)
}
return output
}

Expand All @@ -67,9 +57,8 @@ func RegisterSupportedTypes(types ...Type) error {
}

for _, t := range types {
if supported := isTypeSupported[t]; !supported {
supportedTypes = append(supportedTypes, t)
isTypeSupported[t] = true
if supported := supportedTypeMap[t]; !supported {
supportedTypeMap[t] = true
}
}

Expand All @@ -86,7 +75,7 @@ func (t Type) String() string {

// IsValid will validate whether the typename is valid or not
func (t Type) IsValid() bool {
return isTypeSupported[t]
return supportedTypeMap[t]
}

func (t Type) validate() error {
Expand Down
17 changes: 14 additions & 3 deletions core/asset/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/goto/compass/core/asset"
"github.com/goto/compass/internal/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestGetSupportedTypes(t *testing.T) {

actualTypes := asset.GetSupportedTypes()

assert.EqualValues(t, expectedTypes, actualTypes)
assert.True(t, testutils.AreSlicesEqualIgnoringOrder(expectedTypes, actualTypes, compareTypes))
})
}

Expand Down Expand Up @@ -174,7 +175,7 @@ func TestRegisterSupportedTypes(t *testing.T) {
require.Error(t, actualError)

actualTypes := asset.GetSupportedTypes()
assert.EqualValues(t, expectedTypes, actualTypes)
assert.True(t, testutils.AreSlicesEqualIgnoringOrder(expectedTypes, actualTypes, compareTypes))
})

t.Run("should update supported types if no error is returned", func(t *testing.T) {
Expand All @@ -201,6 +202,16 @@ func TestRegisterSupportedTypes(t *testing.T) {
require.NoError(t, actualError)

actualTypes := asset.GetSupportedTypes()
assert.EqualValues(t, expectedTypes, actualTypes)
assert.True(t, testutils.AreSlicesEqualIgnoringOrder(expectedTypes, actualTypes, compareTypes))
})
}

func compareTypes(left, right asset.Type) int {
if left < right {
return -1
}
if right > left {
return 1
}
return 0
}
15 changes: 12 additions & 3 deletions internal/server/v1beta1/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/goto/compass/core/asset"
"github.com/goto/compass/core/user"
"github.com/goto/compass/internal/server/v1beta1/mocks"
"github.com/goto/compass/internal/testutils"
compassv1beta1 "github.com/goto/compass/proto/gotocompany/compass/v1beta1"
"github.com/goto/salt/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/testing/protocmp"
)

func TestGetTypes(t *testing.T) {
Expand Down Expand Up @@ -56,6 +55,16 @@ func TestGetTypes(t *testing.T) {
}, nil)
},
PostCheck: func(resp *compassv1beta1.GetAllTypesResponse) error {
compare := func(left, right *compassv1beta1.Type) int {
if left.Name == right.Name && left.Count == right.Count {
return 0
}
if left.Name < right.Name {
return -1
}
return 1
}

expected := &compassv1beta1.GetAllTypesResponse{
Data: []*compassv1beta1.Type{
{
Expand Down Expand Up @@ -97,7 +106,7 @@ func TestGetTypes(t *testing.T) {
},
}

if diff := cmp.Diff(resp, expected, protocmp.Transform()); diff != "" {
if !testutils.AreSlicesEqualIgnoringOrder(resp.Data, expected.Data, compare) {
return fmt.Errorf("expected response to be %+v, was %+v", expected, resp)
}
return nil
Expand Down
28 changes: 28 additions & 0 deletions internal/testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"sort"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -43,3 +44,30 @@ type ArgMatcher interface{ Matches(interface{}) bool }
func OfTypeContext() ArgMatcher {
return mock.MatchedBy(func(ctx context.Context) bool { return ctx != nil })
}

func AreSlicesEqualIgnoringOrder[T any](s1, s2 []T, compare func(l, r T) int) bool {
if len(s1) != len(s2) {
return false
}

s1Duplicate := make([]T, len(s1))
copy(s1Duplicate, s1)
s2Duplicate := make([]T, len(s2))
copy(s2Duplicate, s2)

sort.Slice(s1Duplicate, func(i, j int) bool {
return compare(s1Duplicate[i], s1Duplicate[j]) < 0
})

sort.Slice(s2Duplicate, func(i, j int) bool {
return compare(s2Duplicate[i], s2Duplicate[j]) < 0
})

for i := 0; i < len(s1Duplicate); i++ {
if compare(s1Duplicate[i], s2Duplicate[i]) != 0 {
return false
}
}

return true
}

0 comments on commit b4aaa4a

Please sign in to comment.