From cec467aaa1c8eb62462d6b31a416cd8777152ff8 Mon Sep 17 00:00:00 2001 From: horvski Date: Fri, 7 Jul 2023 16:33:55 -0600 Subject: [PATCH] Round 1 code review | Added .Values method on OrderedMap | Testing values in TestOrderedMap --- canon.go | 7 +------ orderedmap.go | 20 ++++++++++++++------ orderedmap_test.go | 19 ++++++++++++++++--- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/canon.go b/canon.go index 26b56f8..3c8299b 100644 --- a/canon.go +++ b/canon.go @@ -15,12 +15,7 @@ func Canon(raw json.RawMessage) (can []string, err error) { if err != nil { return nil, err } - keys := o.Keys() - can = make([]string, len(keys)) - for i, k := range keys { - can[i] = k - } - return can, nil + return o.Keys(), nil } // Canonical returns the canonical form. Input canon is optional and may be nil. diff --git a/orderedmap.go b/orderedmap.go index 677f6c2..ba7e7e0 100644 --- a/orderedmap.go +++ b/orderedmap.go @@ -73,13 +73,13 @@ func newOrderedMap() *orderedMap { } func (o *orderedMap) Get(key string) (interface{}, bool) { - val, exists := o.values[key] - return val, exists + val, ok := o.values[key] + return val, ok } func (o *orderedMap) Set(key string, value interface{}) { - _, exists := o.values[key] - if !exists { + _, ok := o.values[key] + if !ok { o.keys = append(o.keys, key) } o.values[key] = value @@ -106,12 +106,20 @@ func (o *orderedMap) Keys() []string { return o.keys } -// SortKeys Sort the map keys using your sort func +func (o *orderedMap) Values() []any { + v := []any{} + for _, k := range o.values { + v = append(v, k) + } + return v +} + +// SortKeys sorts the map keys using the provided sort func. func (o *orderedMap) SortKeys(sortFunc func(keys []string)) { sortFunc(o.keys) } -// Sort Sort the map using your sort func +// Sort sorts the map using the provided less func. func (o *orderedMap) Sort(lessFunc func(a *pair, b *pair) bool) { pairs := make([]*pair, len(o.keys)) for i, key := range o.keys { diff --git a/orderedmap_test.go b/orderedmap_test.go index 09f87c2..8921566 100644 --- a/orderedmap_test.go +++ b/orderedmap_test.go @@ -26,6 +26,7 @@ package coze import ( "encoding/json" "fmt" + "reflect" "sort" "strings" "testing" @@ -88,11 +89,23 @@ func TestOrderedMap(t *testing.T) { t.Error("Keys method", key, "!=", expectedKeys[i]) } } - for i, key := range expectedKeys { - if key != expectedKeys[i] { - t.Error("Keys method", key, "!=", expectedKeys[i]) + + values := o.Values() + expectedValues := []any{ + 4, // 3 is overwritten + "x", + []string{"t", "u"}, + []any{1, "1"}, + } + for i, val := range values { + if !reflect.DeepEqual(expectedValues[i], val) { + t.Error("Values method", expectedValues[i], "!=", val) } + // if expectedValues[i] != val { // TODO in the future when GO has comparable working + // t.Error("Values method", expectedValues[i], "!=", val) + // } } + // delete o.Delete("strings") o.Delete("not a key being used")