Skip to content

Commit

Permalink
Round 1 code review | Added .Values method on OrderedMap | Testing va…
Browse files Browse the repository at this point in the history
…lues in TestOrderedMap
  • Loading branch information
horvski committed Jul 7, 2023
1 parent e19e935 commit cec467a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
7 changes: 1 addition & 6 deletions canon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 14 additions & 6 deletions orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
19 changes: 16 additions & 3 deletions orderedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package coze
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"
"testing"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit cec467a

Please sign in to comment.