pax_global_header00006660000000000000000000000064140014110070014476gustar00rootroot0000000000000052 comment=82684f0a646914bd6959575046bf78cfdbb92536 orderedmap-0.2.0/000077500000000000000000000000001400141100700136175ustar00rootroot00000000000000orderedmap-0.2.0/LICENSE000066400000000000000000000020661400141100700146300ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2017 Ian Coleman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, Subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or Substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. orderedmap-0.2.0/orderedmap.go000066400000000000000000000135371400141100700163010ustar00rootroot00000000000000package orderedmap import ( "bytes" "encoding/json" "sort" ) type Pair struct { key string value interface{} } func (kv *Pair) Key() string { return kv.key } func (kv *Pair) Value() interface{} { return kv.value } type ByPair struct { Pairs []*Pair LessFunc func(a *Pair, j *Pair) bool } func (a ByPair) Len() int { return len(a.Pairs) } func (a ByPair) Swap(i, j int) { a.Pairs[i], a.Pairs[j] = a.Pairs[j], a.Pairs[i] } func (a ByPair) Less(i, j int) bool { return a.LessFunc(a.Pairs[i], a.Pairs[j]) } type OrderedMap struct { keys []string values map[string]interface{} escapeHTML bool } func New() *OrderedMap { o := OrderedMap{} o.keys = []string{} o.values = map[string]interface{}{} o.escapeHTML = true return &o } func (o *OrderedMap) SetEscapeHTML(on bool) { o.escapeHTML = on } func (o *OrderedMap) Get(key string) (interface{}, bool) { val, exists := o.values[key] return val, exists } func (o *OrderedMap) Set(key string, value interface{}) { _, exists := o.values[key] if !exists { o.keys = append(o.keys, key) } o.values[key] = value } func (o *OrderedMap) Delete(key string) { // check key is in use _, ok := o.values[key] if !ok { return } // remove from keys for i, k := range o.keys { if k == key { o.keys = append(o.keys[:i], o.keys[i+1:]...) break } } // remove from values delete(o.values, key) } func (o *OrderedMap) Keys() []string { return o.keys } // SortKeys Sort the map keys using your sort func func (o *OrderedMap) SortKeys(sortFunc func(keys []string)) { sortFunc(o.keys) } // Sort Sort the map using your sort func func (o *OrderedMap) Sort(lessFunc func(a *Pair, b *Pair) bool) { pairs := make([]*Pair, len(o.keys)) for i, key := range o.keys { pairs[i] = &Pair{key, o.values[key]} } sort.Sort(ByPair{pairs, lessFunc}) for i, pair := range pairs { o.keys[i] = pair.key } } func (o *OrderedMap) UnmarshalJSON(b []byte) error { if o.values == nil { o.values = map[string]interface{}{} } err := json.Unmarshal(b, &o.values) if err != nil { return err } dec := json.NewDecoder(bytes.NewReader(b)) if _, err = dec.Token(); err != nil { // skip '{' return err } o.keys = make([]string, 0, len(o.values)) return decodeOrderedMap(dec, o) } func decodeOrderedMap(dec *json.Decoder, o *OrderedMap) error { hasKey := make(map[string]bool, len(o.values)) for { token, err := dec.Token() if err != nil { return err } if delim, ok := token.(json.Delim); ok && delim == '}' { return nil } key := token.(string) if hasKey[key] { // duplicate key for j, k := range o.keys { if k == key { copy(o.keys[j:], o.keys[j+1:]) break } } o.keys[len(o.keys)-1] = key } else { hasKey[key] = true o.keys = append(o.keys, key) } token, err = dec.Token() if err != nil { return err } if delim, ok := token.(json.Delim); ok { switch delim { case '{': if values, ok := o.values[key].(map[string]interface{}); ok { newMap := OrderedMap{ keys: make([]string, 0, len(values)), values: values, escapeHTML: o.escapeHTML, } if err = decodeOrderedMap(dec, &newMap); err != nil { return err } o.values[key] = newMap } else if oldMap, ok := o.values[key].(OrderedMap); ok { newMap := OrderedMap{ keys: make([]string, 0, len(oldMap.values)), values: oldMap.values, escapeHTML: o.escapeHTML, } if err = decodeOrderedMap(dec, &newMap); err != nil { return err } o.values[key] = newMap } else if err = decodeOrderedMap(dec, &OrderedMap{}); err != nil { return err } case '[': if values, ok := o.values[key].([]interface{}); ok { if err = decodeSlice(dec, values, o.escapeHTML); err != nil { return err } } else if err = decodeSlice(dec, []interface{}{}, o.escapeHTML); err != nil { return err } } } } } func decodeSlice(dec *json.Decoder, s []interface{}, escapeHTML bool) error { for index := 0; ; index++ { token, err := dec.Token() if err != nil { return err } if delim, ok := token.(json.Delim); ok { switch delim { case '{': if index < len(s) { if values, ok := s[index].(map[string]interface{}); ok { newMap := OrderedMap{ keys: make([]string, 0, len(values)), values: values, escapeHTML: escapeHTML, } if err = decodeOrderedMap(dec, &newMap); err != nil { return err } s[index] = newMap } else if oldMap, ok := s[index].(OrderedMap); ok { newMap := OrderedMap{ keys: make([]string, 0, len(oldMap.values)), values: oldMap.values, escapeHTML: escapeHTML, } if err = decodeOrderedMap(dec, &newMap); err != nil { return err } s[index] = newMap } else if err = decodeOrderedMap(dec, &OrderedMap{}); err != nil { return err } } else if err = decodeOrderedMap(dec, &OrderedMap{}); err != nil { return err } case '[': if index < len(s) { if values, ok := s[index].([]interface{}); ok { if err = decodeSlice(dec, values, escapeHTML); err != nil { return err } } else if err = decodeSlice(dec, []interface{}{}, escapeHTML); err != nil { return err } } else if err = decodeSlice(dec, []interface{}{}, escapeHTML); err != nil { return err } case ']': return nil } } } } func (o OrderedMap) MarshalJSON() ([]byte, error) { var buf bytes.Buffer buf.WriteByte('{') encoder := json.NewEncoder(&buf) encoder.SetEscapeHTML(o.escapeHTML) for i, k := range o.keys { if i > 0 { buf.WriteByte(',') } // add key if err := encoder.Encode(k); err != nil { return nil, err } buf.WriteByte(':') // add value if err := encoder.Encode(o.values[k]); err != nil { return nil, err } } buf.WriteByte('}') return buf.Bytes(), nil } orderedmap-0.2.0/orderedmap_test.go000066400000000000000000000277761400141100700173520ustar00rootroot00000000000000package orderedmap import ( "encoding/json" "fmt" "sort" "strings" "testing" ) func TestOrderedMap(t *testing.T) { o := New() // number o.Set("number", 3) v, _ := o.Get("number") if v.(int) != 3 { t.Error("Set number") } // string o.Set("string", "x") v, _ = o.Get("string") if v.(string) != "x" { t.Error("Set string") } // string slice o.Set("strings", []string{ "t", "u", }) v, _ = o.Get("strings") if v.([]string)[0] != "t" { t.Error("Set strings first index") } if v.([]string)[1] != "u" { t.Error("Set strings second index") } // mixed slice o.Set("mixed", []interface{}{ 1, "1", }) v, _ = o.Get("mixed") if v.([]interface{})[0].(int) != 1 { t.Error("Set mixed int") } if v.([]interface{})[1].(string) != "1" { t.Error("Set mixed string") } // overriding existing key o.Set("number", 4) v, _ = o.Get("number") if v.(int) != 4 { t.Error("Override existing key") } // Keys method keys := o.Keys() expectedKeys := []string{ "number", "string", "strings", "mixed", } for i, key := range keys { if key != expectedKeys[i] { t.Error("Keys method", key, "!=", expectedKeys[i]) } } for i, key := range expectedKeys { if key != expectedKeys[i] { t.Error("Keys method", key, "!=", expectedKeys[i]) } } // delete o.Delete("strings") o.Delete("not a key being used") if len(o.Keys()) != 3 { t.Error("Delete method") } _, ok := o.Get("strings") if ok { t.Error("Delete did not remove 'strings' key") } } func TestBlankMarshalJSON(t *testing.T) { o := New() // blank map b, err := json.Marshal(o) if err != nil { t.Error("Marshalling blank map to json", err) } s := string(b) // check json is correctly ordered if s != `{}` { t.Error("JSON Marshaling blank map value is incorrect", s) } // convert to indented json bi, err := json.MarshalIndent(o, "", " ") if err != nil { t.Error("Marshalling indented json for blank map", err) } si := string(bi) ei := `{}` if si != ei { fmt.Println(ei) fmt.Println(si) t.Error("JSON MarshalIndent blank map value is incorrect", si) } } func TestMarshalJSON(t *testing.T) { o := New() // number o.Set("number", 3) // string o.Set("string", "x") // string o.Set("specialstring", "\\.<>[]{}_-") // new value keeps key in old position o.Set("number", 4) // keys not sorted alphabetically o.Set("z", 1) o.Set("a", 2) o.Set("b", 3) // slice o.Set("slice", []interface{}{ "1", 1, }) // orderedmap v := New() v.Set("e", 1) v.Set("a", 2) o.Set("orderedmap", v) // escape key o.Set("test\n\r\t\\\"ing", 9) // convert to json b, err := json.Marshal(o) if err != nil { t.Error("Marshalling json", err) } s := string(b) // check json is correctly ordered if s != `{"number":4,"string":"x","specialstring":"\\.\u003c\u003e[]{}_-","z":1,"a":2,"b":3,"slice":["1",1],"orderedmap":{"e":1,"a":2},"test\n\r\t\\\"ing":9}` { t.Error("JSON Marshal value is incorrect", s) } // convert to indented json bi, err := json.MarshalIndent(o, "", " ") if err != nil { t.Error("Marshalling indented json", err) } si := string(bi) ei := `{ "number": 4, "string": "x", "specialstring": "\\.\u003c\u003e[]{}_-", "z": 1, "a": 2, "b": 3, "slice": [ "1", 1 ], "orderedmap": { "e": 1, "a": 2 }, "test\n\r\t\\\"ing": 9 }` if si != ei { fmt.Println(ei) fmt.Println(si) t.Error("JSON MarshalIndent value is incorrect", si) } } func TestMarshalJSONNoEscapeHTML(t *testing.T) { o := New() o.SetEscapeHTML(false) // string special characters o.Set("specialstring", "\\.<>[]{}_-") // convert to json b, err := o.MarshalJSON() if err != nil { t.Error("Marshalling json", err) } s := strings.Replace(string(b), "\n", "", -1) // check json is correctly ordered if s != `{"specialstring":"\\.<>[]{}_-"}` { t.Error("JSON Marshal value is incorrect", s) } } func TestMarshalJSONNoEscapeHTMLRecursive(t *testing.T) { src := `{"x":"<>","y":[{"z":["<>"]}]}` o := New() o.SetEscapeHTML(false) err := json.Unmarshal([]byte(src), &o) if err != nil { t.Error("JSON Unmarshal error with special chars", err) } b, err := o.MarshalJSON() if err != nil { t.Error("Marshalling json", err) } s := strings.Replace(string(b), "\n", "", -1) if s != src { t.Error("JSON Marshal value is incorrect", s) } } func TestUnmarshalJSON(t *testing.T) { s := `{ "number": 4, "string": "x", "z": 1, "a": "should not break with unclosed { character in value", "b": 3, "slice": [ "1", 1 ], "orderedmap": { "e": 1, "a { nested key with brace": "with a }}}} }} {{{ brace value", "after": { "link": "test {{{ with even deeper nested braces }" } }, "test\"ing": 9, "after": 1, "multitype_array": [ "test", 1, { "map": "obj", "it" : 5, ":colon in key": "colon: in value" }, [{"inner": "map"}] ], "should not break with { character in key": 1 }` o := New() err := json.Unmarshal([]byte(s), &o) if err != nil { t.Error("JSON Unmarshal error", err) } // Check the root keys expectedKeys := []string{ "number", "string", "z", "a", "b", "slice", "orderedmap", "test\"ing", "after", "multitype_array", "should not break with { character in key", } k := o.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("Unmarshal root key order", i, k[i], "!=", expectedKeys[i]) } } // Check nested maps are converted to orderedmaps // nested 1 level deep expectedKeys = []string{ "e", "a { nested key with brace", "after", } vi, ok := o.Get("orderedmap") if !ok { t.Error("Missing key for nested map 1 deep") } v := vi.(OrderedMap) k = v.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("Key order for nested map 1 deep ", i, k[i], "!=", expectedKeys[i]) } } // nested 2 levels deep expectedKeys = []string{ "link", } vi, ok = v.Get("after") if !ok { t.Error("Missing key for nested map 2 deep") } v = vi.(OrderedMap) k = v.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("Key order for nested map 2 deep", i, k[i], "!=", expectedKeys[i]) } } // multitype array expectedKeys = []string{ "map", "it", ":colon in key", } vislice, ok := o.Get("multitype_array") if !ok { t.Error("Missing key for multitype array") } vslice := vislice.([]interface{}) vmap := vslice[2].(OrderedMap) k = vmap.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("Key order for nested map 2 deep", i, k[i], "!=", expectedKeys[i]) } } // nested map 3 deep vislice, _ = o.Get("multitype_array") vslice = vislice.([]interface{}) expectedKeys = []string{"inner"} vinnerslice := vslice[3].([]interface{}) vinnermap := vinnerslice[0].(OrderedMap) k = vinnermap.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("Key order for nested map 3 deep", i, k[i], "!=", expectedKeys[i]) } } } func TestUnmarshalJSONDuplicateKeys(t *testing.T) { s := `{ "a": [{}, []], "b": {"x":[1]}, "c": "x", "d": {"x":1}, "b": [{"x":[]}], "c": 1, "d": {"y": 2}, "e": [{"x":1}], "e": [[]], "e": [{"z":2}], "a": {}, "b": [[1]] }` o := New() err := json.Unmarshal([]byte(s), &o) if err != nil { t.Error("JSON Unmarshal error with special chars", err) } expectedKeys := []string{ "c", "d", "e", "a", "b", } keys := o.Keys() if len(keys) != len(expectedKeys) { t.Error("Unmarshal key count", len(keys), "!=", len(expectedKeys)) } for i, key := range keys { if key != expectedKeys[i] { t.Errorf("Unmarshal root key order: %d, %q != %q", i, key, expectedKeys[i]) } } vimap, _ := o.Get("a") _ = vimap.(OrderedMap) vislice, _ := o.Get("b") _ = vislice.([]interface{}) vival, _ := o.Get("c") _ = vival.(float64) vimap, _ = o.Get("d") m := vimap.(OrderedMap) expectedKeys = []string{"y"} keys = m.Keys() if len(keys) != len(expectedKeys) { t.Error("Unmarshal key count", len(keys), "!=", len(expectedKeys)) } for i, key := range keys { if key != expectedKeys[i] { t.Errorf("Unmarshal key order: %d, %q != %q", i, key, expectedKeys[i]) } } vislice, _ = o.Get("e") m = vislice.([]interface{})[0].(OrderedMap) expectedKeys = []string{"z"} keys = m.Keys() if len(keys) != len(expectedKeys) { t.Error("Unmarshal key count", len(keys), "!=", len(expectedKeys)) } for i, key := range keys { if key != expectedKeys[i] { t.Errorf("Unmarshal key order: %d, %q != %q", i, key, expectedKeys[i]) } } } func TestUnmarshalJSONSpecialChars(t *testing.T) { s := `{ " \u0041\n\r\t\\\\\\\\\\\\ " : { "\\\\\\" : "\\\\\"\\" }, "\\": " \\\\ test ", "\n": "\r" }` o := New() err := json.Unmarshal([]byte(s), &o) if err != nil { t.Error("JSON Unmarshal error with special chars", err) } expectedKeys := []string{ " \u0041\n\r\t\\\\\\\\\\\\ ", "\\", "\n", } keys := o.Keys() if len(keys) != len(expectedKeys) { t.Error("Unmarshal key count", len(keys), "!=", len(expectedKeys)) } for i, key := range keys { if key != expectedKeys[i] { t.Errorf("Unmarshal root key order: %d, %q != %q", i, key, expectedKeys[i]) } } } func TestUnmarshalJSONArrayOfMaps(t *testing.T) { s := ` { "name": "test", "percent": 6, "breakdown": [ { "name": "a", "percent": 0.9 }, { "name": "b", "percent": 0.9 }, { "name": "d", "percent": 0.4 }, { "name": "e", "percent": 2.7 } ] } ` o := New() err := json.Unmarshal([]byte(s), &o) if err != nil { t.Error("JSON Unmarshal error", err) } // Check the root keys expectedKeys := []string{ "name", "percent", "breakdown", } k := o.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("Unmarshal root key order", i, k[i], "!=", expectedKeys[i]) } } // Check nested maps are converted to orderedmaps // nested 1 level deep expectedKeys = []string{ "name", "percent", } vi, ok := o.Get("breakdown") if !ok { t.Error("Missing key for nested map 1 deep") } vs := vi.([]interface{}) for _, vInterface := range vs { v := vInterface.(OrderedMap) k = v.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("Key order for nested map 1 deep ", i, k[i], "!=", expectedKeys[i]) } } } } func TestUnmarshalJSONStruct(t *testing.T) { var v struct { Data *OrderedMap `json:"data"` } err := json.Unmarshal([]byte(`{ "data": { "x": 1 } }`), &v) if err != nil { t.Fatalf("JSON unmarshal error: %v", err) } x, ok := v.Data.Get("x") if !ok { t.Errorf("missing expected key") } else if x != float64(1) { t.Errorf("unexpected value: %#v", x) } } func TestOrderedMap_SortKeys(t *testing.T) { s := ` { "b": 2, "a": 1, "c": 3 } ` o := New() json.Unmarshal([]byte(s), &o) o.SortKeys(sort.Strings) // Check the root keys expectedKeys := []string{ "a", "b", "c", } k := o.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("SortKeys root key order", i, k[i], "!=", expectedKeys[i]) } } } func TestOrderedMap_Sort(t *testing.T) { s := ` { "b": 2, "a": 1, "c": 3 } ` o := New() json.Unmarshal([]byte(s), &o) o.Sort(func(a *Pair, b *Pair) bool { return a.value.(float64) > b.value.(float64) }) // Check the root keys expectedKeys := []string{ "c", "b", "a", } k := o.Keys() for i := range k { if k[i] != expectedKeys[i] { t.Error("Sort root key order", i, k[i], "!=", expectedKeys[i]) } } } // https://github.com/iancoleman/orderedmap/issues/11 func TestOrderedMap_empty_array(t *testing.T) { srcStr := `{"x":[]}` src := []byte(srcStr) om := New() json.Unmarshal(src, om) bs, _ := json.Marshal(om) marshalledStr := string(bs) if marshalledStr != srcStr { t.Error("Empty array does not serialise to json correctly") t.Error("Expect", srcStr) t.Error("Got", marshalledStr) } } // Inspired by // https://github.com/iancoleman/orderedmap/issues/11 // but using empty maps instead of empty slices func TestOrderedMap_empty_map(t *testing.T) { srcStr := `{"x":{}}` src := []byte(srcStr) om := New() json.Unmarshal(src, om) bs, _ := json.Marshal(om) marshalledStr := string(bs) if marshalledStr != srcStr { t.Error("Empty map does not serialise to json correctly") t.Error("Expect", srcStr) t.Error("Got", marshalledStr) } } orderedmap-0.2.0/readme.md000066400000000000000000000032711400141100700154010ustar00rootroot00000000000000# orderedmap A golang data type equivalent to python's collections.OrderedDict Retains order of keys in maps Can be JSON serialized / deserialized # Usage ```go package main import ( "encoding/json" "github.com/iancoleman/orderedmap" ) func main() { // use New() instead of o := map[string]interface{}{} o := orderedmap.New() // use SetEscapeHTML() to whether escape problematic HTML characters or not, defaults is true o.SetEscapeHTML(false) // use Set instead of o["a"] = 1 o.Set("a", 1) // add some value with special characters o.Set("b", "\\.<>[]{}_-") // use Get instead of i, ok := o["a"] val, ok := o.Get("a") // use Keys instead of for k, v := range o keys := o.Keys() for _, k := range keys { v, _ := o.Get(k) } // use o.Delete instead of delete(o, key) o.Delete("a") // serialize to a json string using encoding/json bytes, err := json.Marshal(o) prettyBytes, err := json.MarshalIndent(o, "", " ") // deserialize a json string using encoding/json // all maps (including nested maps) will be parsed as orderedmaps s := `{"a": 1}` err := json.Unmarshal([]byte(s), &o) // sort the keys o.SortKeys(sort.Strings) // sort by Pair o.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool { return a.Value().(float64) < b.Value().(float64) }) } ``` # Caveats * OrderedMap only takes strings for the key, as per [the JSON spec](http://json.org/). # Tests ``` go test ``` # Alternatives None of the alternatives offer JSON serialization. * [cevaris/ordered_map](https://github.com/cevaris/ordered_map) * [mantyr/iterator](https://github.com/mantyr/iterator)