Skip to content

Commit

Permalink
Rename Accessor to OrderedLookup
Browse files Browse the repository at this point in the history
  • Loading branch information
dprotaso committed Nov 19, 2021
1 parent 80bb29a commit 0743cb6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
33 changes: 23 additions & 10 deletions kmap/accessor.go → kmap/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,37 @@ limitations under the License.

package kmap

// Accessor is a utility struct for getting values from a map
// given a list of keys
// OrderedLookup is a utility struct for getting values from a map
// given a list of ordered keys
//
// This is to help the migration/renaming of annotations & labels
type Accessor struct {
// Access will be done in order
type OrderedLookup struct {
Keys []string
}

// Key returns the default key that should be used for
// accessing the map
func (a *Accessor) Key() string {
func (a *OrderedLookup) Key() string {
return a.Keys[0]
}

// Value returns maps value for the Accessor's keys
func (a *Accessor) Value(m map[string]string) string {
// Value iterates looks up the ordered keys in the map and returns
// a string value. An empty string will be returned if the keys
// are not present in the map
func (a *OrderedLookup) Value(m map[string]string) string {
_, v, _ := a.Get(m)
return v
}

func (a *Accessor) Get(m map[string]string) (string, string, bool) {
// Get iterates over the ordered keys and looks up the corresponding
// values in the map
//
// It returns the key, value, and true|false signaling whether the
// key was present in the map
//
// If no key is present the default key (lowest ordinal) is returned
// with an empty string as the value
func (a *OrderedLookup) Get(m map[string]string) (string, string, bool) {
var k, v string
var ok bool
for _, k = range a.Keys {
Expand All @@ -50,9 +59,13 @@ func (a *Accessor) Get(m map[string]string) (string, string, bool) {
return a.Keys[0], "", false
}

func NewAccessor(keys ...string) *Accessor {
// NewOrderedLookup builds a utilty struct for looking up N keys
// in a map in a specific order
//
// If no keys are supplied this method will panic
func NewOrderedLookup(keys ...string) *OrderedLookup {
if len(keys) == 0 {
panic("expected to have at least a single key")
}
return &Accessor{Keys: keys}
return &OrderedLookup{Keys: keys}
}
14 changes: 7 additions & 7 deletions kmap/accessor_test.go → kmap/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestNewAccessor(t *testing.T) {
func TestNewOrderedLookup(t *testing.T) {
keys := []string{"1", "2", "3"}
a := NewAccessor(keys...)
a := NewOrderedLookup(keys...)

if diff := cmp.Diff(keys, a.Keys); diff != "" {
t.Error("NewAccessor unexpected diff (-want, +got):", diff)
t.Error("NewOrderedLookup unexpected diff (-want, +got):", diff)
}
}

func TestNewAccessor_BadInput(t *testing.T) {
func TestNewOrderedLookup_BadInput(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected no keys to panic")
}
}()
NewAccessor()
NewOrderedLookup()
}

func TestAccessor_Get(t *testing.T) {
func TestOrderedLookup_Get(t *testing.T) {
tests := []struct {
name string
keys []string
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestAccessor_Get(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
a := NewAccessor(tc.keys...)
a := NewOrderedLookup(tc.keys...)

k, v, ok := a.Get(tc.in)

Expand Down

0 comments on commit 0743cb6

Please sign in to comment.