Documentation ¶
Overview ¶
Package ordered is a vendored in version of `gitlab.com/c0b/go-ordered-json@febf46534d5a4f9f64ea4c1874f0e55791373a2b`
The package is vendored in because usages of `json.Marshal()` for **values** (as opposed to keys) in the ordered map are not configurable. Here in particular we have semver ranges e.g. `>= 2.3.1` that by default get HTML escaped as `\u003e= 2.3.1`.
Package ordered provided a type OrderedMap for use in JSON handling although JSON spec says the keys order of an object should not matter but sometimes when working with particular third-party proprietary code which has incorrect using the keys order, we have to maintain the object keys in the same order of incoming JSON object, this package is useful for these cases.
Disclaimer: same as Go's default [map](https://blog.golang.org/go-maps-in-action), this OrderedMap is not safe for concurrent use, if need atomic access, may use a sync.Mutex to synchronize.
Index ¶
- type KVPair
- type OrderedMap
- func (om *OrderedMap) Delete(key string) (value interface{}, ok bool)
- func (om *OrderedMap) EntriesIter() func() (*KVPair, bool)
- func (om *OrderedMap) EntriesReverseIter() func() (*KVPair, bool)
- func (om *OrderedMap) Get(key string) interface{}
- func (om *OrderedMap) GetValue(key string) (value interface{}, ok bool)
- func (om *OrderedMap) Has(key string) bool
- func (om *OrderedMap) MarshalJSON() (res []byte, err error)
- func (om *OrderedMap) Set(key string, value interface{})
- func (om *OrderedMap) UnmarshalJSON(data []byte) error
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 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) 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)