Skip to content

Commit

Permalink
remove interface
Browse files Browse the repository at this point in the history
Signed-off-by: Samsondeen Dare <samsondeen.dare@hashicorp.com>
  • Loading branch information
dsa0x committed Aug 17, 2022
1 parent 12b38af commit ec6f3e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 49 deletions.
45 changes: 14 additions & 31 deletions pkg/api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import (

func SearchIndexHandler(params index.SearchIndexParams) middleware.Responder {
httpReqCtx := params.HTTPRequest.Context()
var result Collection

if params.Query.Operator != "" {
result = NewSuperSet(params.Query.Operator)
} else {
result = &Arr{}
queryOperator := params.Query.Operator
// default to "or" if no operator is specified
if params.Query.Operator == "" {
queryOperator = "or"
}
var result = NewCollection(queryOperator)

if params.Query.Hash != "" {
// This must be a valid sha256 hash
Expand Down Expand Up @@ -107,15 +107,6 @@ func storeAttestation(ctx context.Context, uuid string, attestation []byte) erro
return storageClient.StoreAttestation(ctx, uuid, attestation)
}

// Collection is a collection of elements.
type Collection interface {
// Add adds elements to the collection.
Add([]string)

// Values returns the collection as a slice of strings.
Values() []string
}

// Uniq is a collection of unique elements.
type Uniq map[string]struct{}

Expand Down Expand Up @@ -159,29 +150,31 @@ func (u Uniq) Union(other Uniq) Uniq {
return result
}

// SuperSet is a collection of sets.
// Collection is a collection of sets.
//
// its result is a union or intersection of all the sets, depending on the operator.
type SuperSet struct {
type Collection struct {
subsets []Uniq
operator string
}

// NewSuperSet creates a new SuperSet.
func NewSuperSet(operator string) *SuperSet {
return &SuperSet{
// NewCollection creates a new collection.
func NewCollection(operator string) *Collection {
return &Collection{
subsets: []Uniq{},
operator: operator,
}
}

func (u *SuperSet) Add(elements []string) {
// Add adds elements to a subset in the collection.
func (u *Collection) Add(elements []string) {
subset := Uniq{}
subset.Add(elements)
u.subsets = append(u.subsets, subset)
}

func (u *SuperSet) Values() []string {
// Values returns the collection as a slice of strings.
func (u *Collection) Values() []string {
if len(u.subsets) == 0 {
return []string{}
}
Expand All @@ -195,13 +188,3 @@ func (u *SuperSet) Values() []string {
}
return subset.Values()
}

// Arr is a collection of elements.
type Arr []string

func (a *Arr) Add(elements []string) {
*a = append(*a, elements...)
}
func (a Arr) Values() []string {
return a
}
21 changes: 3 additions & 18 deletions pkg/api/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,14 @@ func Test_Collection(t *testing.T) {
}
})

t.Run("Array", func(t *testing.T) {
arr := new(Arr)
arr.Add(vals)

if len(arr.Values()) != 5 {
t.Errorf("expected 5 values, got %d", len(arr.Values()))
}
expected := []string{"foo", "bar", "baz", "baz", "baz"}
for i, v := range arr.Values() {
if v != expected[i] {
t.Errorf("expected %s, got %s", expected[i], v)
}
}
})

t.Run("Superset", func(t *testing.T) {
t.Run("Collection", func(t *testing.T) {

uniq1 := []string{"foo", "bar", "baz"}
uniq2 := []string{"foo", "bar", "baz"}
uniq3 := []string{"corge", "grault", "garply", "foo"}

t.Run("with 'and' operator", func(t *testing.T) {
set := NewSuperSet("and")
set := NewCollection("and")
set.Add(uniq1)
set.Add(uniq2)
set.Add(uniq3)
Expand All @@ -76,7 +61,7 @@ func Test_Collection(t *testing.T) {
}
})
t.Run("with 'or' operator", func(t *testing.T) {
set := NewSuperSet("or")
set := NewCollection("or")
set.Add(uniq1)
set.Add(uniq2)
set.Add(uniq3)
Expand Down

0 comments on commit ec6f3e5

Please sign in to comment.