orderedmap

package module
Version: v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2021 License: MIT Imports: 3 Imported by: 118

README

orderedmap

A golang data type equivalent to python's collections.OrderedDict

Retains order of keys in maps

Can be JSON serialized / deserialized

Usage

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.

Tests

go test

Alternatives

None of the alternatives offer JSON serialization.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByPair

type ByPair struct {
	Pairs    []*Pair
	LessFunc func(a *Pair, j *Pair) bool
}

func (ByPair) Len

func (a ByPair) Len() int

func (ByPair) Less

func (a ByPair) Less(i, j int) bool

func (ByPair) Swap

func (a ByPair) Swap(i, j int)

type OrderedMap

type OrderedMap struct {
	// contains filtered or unexported fields
}

func New

func New() *OrderedMap

func (*OrderedMap) Delete

func (o *OrderedMap) Delete(key string)

func (*OrderedMap) Get

func (o *OrderedMap) Get(key string) (interface{}, bool)

func (*OrderedMap) Keys

func (o *OrderedMap) Keys() []string

func (OrderedMap) MarshalJSON

func (o OrderedMap) MarshalJSON() ([]byte, error)

func (*OrderedMap) Set

func (o *OrderedMap) Set(key string, value interface{})

func (*OrderedMap) SetEscapeHTML

func (o *OrderedMap) SetEscapeHTML(on bool)

func (*OrderedMap) Sort

func (o *OrderedMap) Sort(lessFunc func(a *Pair, b *Pair) bool)

Sort Sort the map using your sort func

func (*OrderedMap) SortKeys

func (o *OrderedMap) SortKeys(sortFunc func(keys []string))

SortKeys Sort the map keys using your sort func

func (*OrderedMap) UnmarshalJSON

func (o *OrderedMap) UnmarshalJSON(b []byte) error

type Pair

type Pair struct {
	// contains filtered or unexported fields
}

func (*Pair) Key

func (kv *Pair) Key() string

func (*Pair) Value

func (kv *Pair) Value() interface{}

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL