diff --git a/kmap/accessor.go b/kmap/lookup.go similarity index 50% rename from kmap/accessor.go rename to kmap/lookup.go index 0ed0dcffc4..6066c00b61 100644 --- a/kmap/accessor.go +++ b/kmap/lookup.go @@ -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 { @@ -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} } diff --git a/kmap/accessor_test.go b/kmap/lookup_test.go similarity index 87% rename from kmap/accessor_test.go rename to kmap/lookup_test.go index 4f60e7ee81..8bd24162b4 100644 --- a/kmap/accessor_test.go +++ b/kmap/lookup_test.go @@ -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 @@ -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)