orderedmap

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

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

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 6 Imported by: 1

README

OrderedMap

Chinese version: 中文

Summary

OrderedMap is a map which preserving the key order as they are added to the map.

Feature

  • Simple
  • Fast(Use json.Decoder for unmarshaling)
  • Powerful API
  • Well documented
  • Marshaling & UnMarshaling support.

Usage

package main

import (
    "fmt"
    "encoding/json"
    "github.com/haifenghuang/orderedmap"
)

func main() {
    om := orderedmap.New()
    om.Set("Name", "HuangHaiFeng")
    om.Set("Sex", "Male")
    om.Set("Hobby", "Programming")
    om.Set("Country", "China")

    hobby, _ := om.Get("Hobby")
    fmt.Printf("Hobby = %s\n", hobby)

    sex, _ := om.GetAt(1)
    fmt.Printf("sex = %v\n", sex)

    om.SetAt(2, "Married", true)
    married, _ := om.GetAt(2)
    fmt.Printf("married = %t\n", married)

    fmt.Printf("=============================\n")
    fmt.Printf("keys = %v\n", om.Keys())
    fmt.Printf("values = %v\n", om.Values())
    fmt.Printf("mapLen = %d\n", om.Len())

    fmt.Printf("=============================\n")
    om.DeleteAt(2)
    fmt.Printf("OrderedMap = %s\n", om)

    fmt.Printf("=============================\n")
    has := om.Exists("Married")
    fmt.Printf("Married? - %t\n", has)
    has = om.Exists("Country")
    fmt.Printf("Country? - %t\n", has)

    fmt.Printf("=============================\n")
    idx := om.Index("Hobby")
    fmt.Printf("Hobby key's index = %d\n", idx)

    fmt.Printf("=============================\n")
    b, _ := json.MarshalIndent(om, "", "    ")
    fmt.Printf("Marshal result = %s\n", string(b))

    fmt.Printf("=============================\n")
    jsonStream := `{
    "Name": "HuangHaiFeng",
    "Sex": "Male",
    "Hobby": "Programming",
    "Country": "China"
}`
    om2 := orderedmap.New()
    _ = json.Unmarshal([]byte(jsonStream), om2)
    fmt.Printf("om2 = %v\n", om2)

    om2.Sort()
    fmt.Printf("==============================\n")
    fmt.Printf("om2 = %v\n", om2)

    om3 := om2.Reverse()
    fmt.Printf("==============================\n")
    fmt.Printf("om3 = %v\n", om3)

    om4 := om3.Filter(func(key string,value interface{}) bool {
        return key == "China" || key == "Male"
    })
    fmt.Printf("==============================\n")
    fmt.Printf("om4 = %v\n", om4)
}

Limitation

  • OrderedMap only takes strings for the key.
  • OrderedMap is not thread-safe for concurrent use.(It is simple enough to add one using sync.RWMutex)

API

Function Description
New() New create a new OrderedMap.
Get(key string) Get returns the value of the map based on its key.
GetAt(pos int) GetAt returns the value based on the provided pos.
Set(key string, value interface{}) Set sets the key/value of the map based on key and value.
SetAt(index int, key string, val interface{}) SetAt sets the given key to the given value at the specified index.
Delete(key string) Delete remove an item from the map by the supplied key.
DeleteAt(offset int) DeleteAt delete the key/value pair from the map by the supplied offset.
Keys() Keys return the keys of the map in the order they were added.
Values() Values returns a slice of the values in the order they were added.
Exists(key string) Exists test whether the key exists or not.
Index(key string) Index returns the offset of the key in the ordered map.
Len() Len returns the length of the map.
String() String returns the JSON serialized string representation.
Reverse() Reverse reverse key & value of a map. The value must be a string.
Sort() Sort the given OrderedMap.
Filter(f func(key string, value interface{}) bool) Filter an OrderedMap if the provided function return true.
MarshalJSON() ([]byte, error) MarshalJSON implements the json.Marshaller interface.
UnmarshalJSON(b []byte) error UnmarshalJSON implements the json.Unmarshaller interface.

Alternatives

License

MIT

Remarks

If you like this repo,please star & fork. Thanks!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrderedMap

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

An OrderedMap is a map where the keys keep the order that they're added. It is similar to indexed arrays. You can get the strings by key or by position. The OrderedMap is not safe for concurrent use.

func New

func New() *OrderedMap

New create a new OrderedMap.

func (*OrderedMap) Delete

func (om *OrderedMap) Delete(key string)

Delete remove an item from the map by the supplied key. If the key does not exist, nothing happens.

func (*OrderedMap) DeleteAt

func (om *OrderedMap) DeleteAt(offset int)

DeleteAt delete the key/value pair from the map by the supplied offset. If the offset is outside the range of the ordered map, nothing happens.

func (*OrderedMap) Exists

func (om *OrderedMap) Exists(key string) (ok bool)

Exists test whether the key exists or not.

func (*OrderedMap) Filter

func (om *OrderedMap) Filter(f func(key string, value interface{}) bool) *OrderedMap

Filter filters an OrderedMap if the provided function return true. Returns a new OrderedMap.

func (*OrderedMap) Get

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

Get returns the value of the map based on its key. It will return nil if it doesn't exist.

func (*OrderedMap) GetAt

func (om *OrderedMap) GetAt(pos int) (val interface{}, ok bool)

GetAt returns the value based on the provided pos.

func (*OrderedMap) Index

func (om *OrderedMap) Index(key string) int

Index returns the offset of the key in the ordered map. If the key could not be found, -1 is returned.

func (*OrderedMap) Keys

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

Keys return the keys of the map in the order they were added.

func (*OrderedMap) Len

func (om *OrderedMap) Len() int

Blelow three functions implement sort.Interface. Len returns the length of the map.

func (*OrderedMap) Less

func (om *OrderedMap) Less(i, j int) bool

func (OrderedMap) MarshalJSON

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

MarshalJSON implements the json.Marshaller interface, so it could be serialized. When serializing, the keys of the map will keep the order they are added.

func (*OrderedMap) Reverse

func (om *OrderedMap) Reverse() *OrderedMap

Reverse reverse key & value of a map. The value must be a string. Returns a new OrderedMap.

func (*OrderedMap) Set

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

Set sets the key/value of the map based on key and value. If the value already exists, the value will be replaced.

func (*OrderedMap) SetAt

func (om *OrderedMap) SetAt(index int, key string, val interface{})

SetAt sets the given key to the given value at the specified index. 1. If (index == -1 || index >= len(orderedmap)), then put it at the end. 2. If index is negative, index < -len(orderedmap), then put it at the beginning. 3 If index == -1, -2, ..., put it from the last, last - 1, etc...

func (*OrderedMap) Sort

func (om *OrderedMap) Sort()

Sort the given orderedmap.

func (*OrderedMap) String

func (om *OrderedMap) String() string

String returns the JSON serialized string representation.

func (*OrderedMap) Swap

func (om *OrderedMap) Swap(i, j int)

func (*OrderedMap) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaller interface. so it could be use like below:

o := New()
err := json.Unmarshal([]byte(jsonString), &o)

func (*OrderedMap) Values

func (om *OrderedMap) Values() []interface{}

Values returns a slice of the values in the order they were added.

Jump to

Keyboard shortcuts

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