ordered

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2021 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ordered provided a type OrderedMap for use in JSON handling

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KVPair

type KVPair struct {
	Key   string
	Value interface{}
}

the key-value pair type, for initializing from a list of key-value pairs, or for looping entries in the same order

type OrderedMap

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

the OrderedMap type, has similar operations as the default map, but maintained the keys order of inserted; similar to map, all single key operations (Get/Set/Delete) runs at O(1).

func NewOrderedMap

func NewOrderedMap() *OrderedMap

Create a new OrderedMap

func NewOrderedMapFromKVPairs

func NewOrderedMapFromKVPairs(pairs []*KVPair) *OrderedMap

Create a new OrderedMap and populate from a list of key-value pairs

func (*OrderedMap) Delete

func (om *OrderedMap) Delete(key string) (value interface{}, ok bool)

deletes the element with the specified key (m[key]) from the map. If there is no such element, this is a no-op.

func (*OrderedMap) EntriesIter

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

Iterate all key/value pairs in the same order of object constructed

func (*OrderedMap) EntriesReverseIter

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

Iterate all key/value pairs in the reverse order of object constructed

func (*OrderedMap) Get

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

Get value for particular key, or nil if not exist; but don't rely on nil for non-exist; should check by Has or GetValue

func (*OrderedMap) GetValue

func (om *OrderedMap) GetValue(key string) (value interface{}, ok bool)

Get value and exists together

func (*OrderedMap) Has

func (om *OrderedMap) Has(key string) bool

Check if value exists

func (*OrderedMap) Len

func (om *OrderedMap) Len() int

Get ordered map length

func (*OrderedMap) MarshalJSON

func (om *OrderedMap) MarshalJSON() (res []byte, err error)

this implements type json.Marshaler interface, so can be called in json.Marshal(om)

func (*OrderedMap) Set

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

set value for particular key, this will remember the order of keys inserted but if the key already exists, the order is not updated.

func (*OrderedMap) UnmarshalJSON

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

this implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, om)

Jump to

Keyboard shortcuts

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