orderedmap

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2020 License: MIT Imports: 5 Imported by: 2

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 Set instead of o["a"] = 1
    o.Set("a", 1)

    // use Get instead of i, ok := o["a"]
    val, ok := o.Get("a")

    // use Keys instead of for k, v := range o
    key := o.Keys()
    for _, k := range keys {
        v, _ := o.Get(k)
    }

    // use o.Delete instead of delete(o, key)
    err := 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 *Pair, b *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

View Source
var NoValueError = errors.New("No value for this key")

Functions

func DeepCopyJSONValue

func DeepCopyJSONValue(x interface{}) interface{}

DeepCopyJSONValue deep copies the passed value, assuming it is a valid JSON representation i.e. only contains types produced by json.Unmarshal() and also int64. bool, int64, float64, string, []interface{}, map[string]interface{}, json.Number and nil

func NestedBool

func NestedBool(obj *OrderedMap, fields ...string) (bool, bool, error)

NestedBool returns the bool value of a nested field. Returns false if value is not found and an error if not a bool.

func NestedFieldCopy

func NestedFieldCopy(obj *OrderedMap, fields ...string) (interface{}, bool, error)

NestedFieldCopy returns a deep copy of the value of a nested field. Returns false if the value is missing. No error is returned for a nil field.

Note: fields passed to this function are treated as keys within the passed object; no array/slice syntax is supported.

func NestedFieldNoCopy

func NestedFieldNoCopy(obj *OrderedMap, fields ...string) (interface{}, bool, error)

NestedFieldNoCopy returns a reference to a nested field. Returns false if value is not found and an error if unable to traverse obj.

Note: fields passed to this function are treated as keys within the passed object; no array/slice syntax is supported.

func NestedFloat64

func NestedFloat64(obj *OrderedMap, fields ...string) (float64, bool, error)

NestedFloat64 returns the float64 value of a nested field. Returns false if value is not found and an error if not a float64.

func NestedInt64

func NestedInt64(obj *OrderedMap, fields ...string) (int64, bool, error)

NestedInt64 returns the int64 value of a nested field. Returns false if value is not found and an error if not an int64.

func NestedSlice

func NestedSlice(obj *OrderedMap, fields ...string) ([]interface{}, bool, error)

NestedSlice returns a deep copy of []interface{} value of a nested field. Returns false if value is not found and an error if not a []interface{}.

func NestedString

func NestedString(obj *OrderedMap, fields ...string) (string, bool, error)

NestedString returns the string value of a nested field. Returns false if value is not found and an error if not a string.

func NestedStringMap

func NestedStringMap(obj *OrderedMap, fields ...string) (map[string]string, bool, error)

NestedStringMap returns a copy of map[string]string value of a nested field. Returns false if value is not found and an error if not a OrderedMap or contains non-string values in the map.

func NestedStringSlice

func NestedStringSlice(obj *OrderedMap, fields ...string) ([]string, bool, error)

NestedStringSlice returns a copy of []string value of a nested field. Returns false if value is not found and an error if not a []interface{} or contains non-string items in the slice.

func RemoveNestedField

func RemoveNestedField(obj *OrderedMap, fields ...string)

RemoveNestedField removes the nested field from the obj.

func SetNestedField

func SetNestedField(obj *OrderedMap, value interface{}, fields ...string) error

SetNestedField sets the value of a nested field to a deep copy of the value provided. Returns an error if value cannot be set because one of the nesting levels is not a OrderedMap.

func SetNestedMap

func SetNestedMap(obj *OrderedMap, value OrderedMap, fields ...string) error

SetNestedMap sets the OrderedMap value of a nested field. Returns an error if value cannot be set because one of the nesting levels is not a OrderedMap.

func SetNestedSlice

func SetNestedSlice(obj *OrderedMap, value []interface{}, fields ...string) error

SetNestedSlice sets the slice value of a nested field. Returns an error if value cannot be set because one of the nesting levels is not a OrderedMap.

func SetNestedStringMap

func SetNestedStringMap(obj *OrderedMap, value map[string]string, fields ...string) error

SetNestedStringMap sets the map[string]string value of a nested field. Returns an error if value cannot be set because one of the nesting levels is not a OrderedMap.

func SetNestedStringSlice

func SetNestedStringSlice(obj *OrderedMap, value []string, fields ...string) error

SetNestedStringSlice sets the string slice value of a nested field. Returns an error if value cannot be set because one of the nesting levels is not a OrderedMap.

Types

type ByIndex

type ByIndex []KeyIndex

func (ByIndex) Len

func (a ByIndex) Len() int

func (ByIndex) Less

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

func (ByIndex) Swap

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

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 KeyIndex

type KeyIndex struct {
	Key   string
	Index int
}

type OrderedMap

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

func DeepCopyJSON

func DeepCopyJSON(x *OrderedMap) *OrderedMap

DeepCopyJSON deep copies the passed value, assuming it is a valid JSON representation i.e. only contains types produced by json.Unmarshal() and also int64. bool, int64, float64, string, []interface{}, map[string]interface{}, json.Number and nil

func NestedMap

func NestedMap(obj *OrderedMap, fields ...string) (*OrderedMap, bool, error)

NestedMap returns a deep copy of OrderedMap value of a nested field. Returns false if value is not found and an error if not a OrderedMap.

func New

func New() *OrderedMap

func (*OrderedMap) DeepCopy

func (o *OrderedMap) DeepCopy() *OrderedMap

DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OrderedMap.

func (*OrderedMap) DeepCopyInto

func (o *OrderedMap) DeepCopyInto(out *OrderedMap)

DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.

func (*OrderedMap) Delete

func (o *OrderedMap) Delete(key string)

func (*OrderedMap) Entry

func (o *OrderedMap) Entry(key string) interface{}

func (*OrderedMap) Get

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

func (*OrderedMap) IsZero

func (o *OrderedMap) IsZero() bool

IsZero returns true if the value is nil or OrderedMap is zero.

func (*OrderedMap) Keys

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

func (*OrderedMap) Len

func (o *OrderedMap) Len() int

func (OrderedMap) MarshalJSON

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

func (*OrderedMap) Set

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

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{}

Jump to

Keyboard shortcuts

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