orderedmap

package module
v0.0.0-...-b0dc1b7 Latest Latest
Warning

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

Go to latest
Published: May 30, 2018 License: MIT Imports: 6 Imported by: 1

README

Ordered Map for golang

Build Status

OrderedMap is a Python port of OrderedDict implemented in golang. Golang's builtin map purposefully randomizes the iteration of stored key/values. OrderedMap struct preserves inserted key/value pairs; such that on iteration, key/value pairs are received in inserted (first in, first out) order.

Features

  • Full support Key/Value for all data types
  • Exposes an Iterator that iterates in order of insertion
  • Full Get/Set/Delete map interface
  • Supports Golang v1.3 through v1.10
  • Supports JSON Marshal/Unmarshal
  • Supports YAML Marshal/Unmarshal

Download and Install

go get github.com/fredwangwang/orderedmap

Examples

Create, Get, Set, Delete
package main

import (
    "fmt"
    "github.com/fredwangwang/orderedmap"
)

func main() {

    // Init new OrderedMap
    om := orderedmap.New()

    om.Set("a", 1)
    om.Set("b", 2)

    if val, ok := om.Get("b"); ok == true {
        fmt.Println(val)
    }

    om.Delete("a")

    if _, ok := om.Get("a"); ok == false {
        fmt.Println("c not found")
    }
    
    fmt.Println(om)
}
Iterator
n := 100
om := orderedmap.New()

for i := 0; i < n; i++ {
    om.Set(i, fmt.Sprintf("%d", i * i))
}

// Iterate though values
// - Values iteration are in insert order
// - Returned in a key/value pair struct
iter := om.IterFunc()
for kv, ok := iter(); ok; kv, ok = iter() {
    fmt.Println(kv, kv.Key, kv.Value)
}
Custom Structs
om := orderedmap.New()
om.Set("one", &MyStruct{1, 1.1})
om.Set("two", &MyStruct{2, 2.2})
om.Set("three", &MyStruct{3, 3.3})

fmt.Println(om)
// Ouput: OrderedMap[one:&{1 1.1},  two:&{2 2.2},  three:&{3 3.3}, ]
JSON marshal & unmarshal
rawPayload := `{
"number": 4,
"string": "x",
"z": 1,
"a": 2,
"b": 3,
"slice": [
  "1",
  1
],
"orderedmap": {
  "e": 1,
  "a": 2
},
"test\"ing": 9
}`

om := orderedmap.New()
json.Unmarshal([]byte(rawPayload), om)
constructedPayload, _ := json.MarshalIndent(om, "", "  ")
println(string(constructedPayload)) // will get you the same thing as rawPayload
YAML marshal & unmarshal
rawPayload := `number: 4
string: x
z: 1
a: 2
b: 3
slice:
- '1'
- 1
orderedmap:
  e: 1
  a: 2
testing: 9`

om := orderedmap.New()
yaml.Unmarshal([]byte(rawPayload), om)
constructedPayload, _ := yaml.Marshal(om)
println(string(constructedPayload)) // will get you the same thing as rawPayload

For Development

Git clone project

git clone https://github.com/fredwangwang/orderedmap.git

Build and install project

make

Run tests

make test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KVPair

type KVPair struct {
	Key   interface{}
	Value interface{}
}

func (*KVPair) Compare

func (kv1 *KVPair) Compare(kv2 *KVPair) bool

func (*KVPair) String

func (k *KVPair) String() string

type KeyElement

type KeyElement struct {
	Key   string
	Index int
}

type KeyIndices

type KeyIndices []KeyElement

func (KeyIndices) Len

func (a KeyIndices) Len() int

func (KeyIndices) Less

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

func (KeyIndices) Swap

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

type OrderedMap

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

func New

func New() *OrderedMap

func NewWithArgs

func NewWithArgs(args []*KVPair) *OrderedMap

func (*OrderedMap) Delete

func (om *OrderedMap) Delete(key interface{})

func (*OrderedMap) Get

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

func (*OrderedMap) Iter

func (om *OrderedMap) Iter() <-chan *KVPair

func (*OrderedMap) IterFunc

func (om *OrderedMap) IterFunc() func() (*KVPair, bool)

func (*OrderedMap) Len

func (om *OrderedMap) Len() int

func (OrderedMap) MarshalJSON

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

func (OrderedMap) MarshalYAML

func (om OrderedMap) MarshalYAML() (interface{}, error)

func (*OrderedMap) Set

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

func (*OrderedMap) String

func (om *OrderedMap) String() string

func (*OrderedMap) UnmarshalJSON

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

func (*OrderedMap) UnmarshalYAML

func (om *OrderedMap) UnmarshalYAML(unmarshal func(interface{}) error) error

func (*OrderedMap) UnsafeIter

func (om *OrderedMap) UnsafeIter() <-chan *KVPair

Beware, Iterator leaks goroutines if we do not fully traverse the map. For most cases, `IterFunc()` should work as an iterator.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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