-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add really basic "OrderedMap" implementation for round-tripping JSON …
…maps in a defined order
- Loading branch information
Showing
5 changed files
with
173 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package om | ||
|
||
// https://github.com/golang/go/issues/27179 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// only supports string keys because JSON is the intended use case (and the JSON spec says only string keys are allowed) | ||
type OrderedMap[T any] struct { | ||
m map[string]T | ||
keys []string | ||
} | ||
|
||
func (m OrderedMap[T]) Keys() []string { | ||
return append([]string{}, m.keys...) | ||
} | ||
|
||
func (m OrderedMap[T]) Get(key string) T { | ||
return m.m[key] | ||
} | ||
|
||
// TODO Has()? two-return form of Get? (we don't need either right now) | ||
|
||
func (m *OrderedMap[T]) Set(key string, val T) { // TODO make this variadic so it can take an arbitrary number of pairs? (would be useful for tests, but we don't need something like that right now) | ||
if m.m == nil || m.keys == nil { | ||
m.m = map[string]T{} | ||
m.keys = []string{} | ||
} | ||
if _, ok := m.m[key]; !ok { | ||
m.keys = append(m.keys, key) | ||
} | ||
m.m[key] = val | ||
} | ||
|
||
func (m *OrderedMap[T]) UnmarshalJSON(b []byte) error { | ||
dec := json.NewDecoder(bytes.NewReader(b)) | ||
|
||
// read opening { | ||
if tok, err := dec.Token(); err != nil { | ||
return err | ||
} else if tok != json.Delim('{') { | ||
return fmt.Errorf("expected '{', got %T: %#v", tok, tok) | ||
} | ||
|
||
for { | ||
tok, err := dec.Token() | ||
if err != nil { | ||
return err | ||
} | ||
if tok == json.Delim('}') { | ||
break | ||
} | ||
key, ok := tok.(string) | ||
if !ok { | ||
return fmt.Errorf("expected string key, got %T: %#v", tok, tok) | ||
} | ||
var val T | ||
err = dec.Decode(&val) | ||
if err != nil { | ||
return err | ||
} | ||
m.Set(key, val) | ||
} | ||
|
||
if dec.More() { | ||
return fmt.Errorf("unexpected extra content after closing '}'") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m OrderedMap[T]) MarshalJSON() ([]byte, error) { | ||
var buf bytes.Buffer | ||
enc := json.NewEncoder(&buf) | ||
if err := buf.WriteByte('{'); err != nil { | ||
return nil, err | ||
} | ||
for i, key := range m.keys { | ||
if i > 0 { | ||
buf.WriteByte(',') | ||
} | ||
if err := enc.Encode(key); err != nil { | ||
return nil, err | ||
} | ||
buf.WriteByte(':') | ||
if err := enc.Encode(m.m[key]); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if err := buf.WriteByte('}'); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package om_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/docker-library/meta-scripts/om" | ||
) | ||
|
||
func assert[V comparable](t *testing.T, v V, expected V) { | ||
t.Helper() | ||
if v != expected { | ||
t.Fatalf("expected %v, got %v", expected, v) | ||
} | ||
} | ||
|
||
func assertJSON[V any](t *testing.T, v V, expected string) { | ||
t.Helper() | ||
b, err := json.Marshal(v) | ||
assert(t, err, nil) | ||
assert(t, string(b), expected) | ||
} | ||
|
||
func TestOrderedMapSet(t *testing.T) { | ||
var m om.OrderedMap[string] | ||
assertJSON(t, m, `{}`) | ||
m.Set("c", "a") | ||
assert(t, m.Get("c"), "a") | ||
assert(t, m.Get("b"), "") | ||
assertJSON(t, m, `{"c":"a"}`) | ||
m.Set("b", "b") | ||
assertJSON(t, m, `{"c":"a","b":"b"}`) | ||
m.Set("a", "c") | ||
assertJSON(t, m, `{"c":"a","b":"b","a":"c"}`) | ||
m.Set("c", "d") | ||
assert(t, m.Get("c"), "d") | ||
assertJSON(t, m, `{"c":"d","b":"b","a":"c"}`) | ||
keys := m.Keys() | ||
assert(t, len(keys), 3) | ||
assert(t, keys[0], "c") | ||
assert(t, keys[1], "b") | ||
assert(t, keys[2], "a") | ||
keys[0] = "d" // make sure the result of .Keys cannot modify the original | ||
keys = m.Keys() | ||
assert(t, keys[0], "c") | ||
} | ||
|
||
func TestOrderedMapUnmarshal(t *testing.T) { | ||
var m om.OrderedMap[string] | ||
assert(t, json.Unmarshal([]byte(`{}`), &m), nil) | ||
assertJSON(t, m, `{}`) | ||
assert(t, json.Unmarshal([]byte(`{ "foo" : "bar" }`), &m), nil) | ||
assertJSON(t, m, `{"foo":"bar"}`) | ||
assert(t, json.Unmarshal([]byte(`{ "baz" : "buzz" }`), &m), nil) | ||
assertJSON(t, m, `{"foo":"bar","baz":"buzz"}`) | ||
assert(t, json.Unmarshal([]byte(`{ "foo" : "foo" }`), &m), nil) | ||
assertJSON(t, m, `{"foo":"foo","baz":"buzz"}`) | ||
} | ||
|
||
func TestOrderedMapUnmarshalDupes(t *testing.T) { | ||
var m om.OrderedMap[string] | ||
assert(t, json.Unmarshal([]byte(`{ "foo":"foo", "bar":"bar", "foo":"baz" }`), &m), nil) | ||
assertJSON(t, m, `{"foo":"baz","bar":"bar"}`) | ||
} |