orderedmap

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Package orderedmap is extended version of: https://github.com/iancoleman/orderedmap

Differences:
- Additional methods (GetNested, SetNested, ToMap, ...).
- Enhanced JSON decoding: nested map is always pointer (*OrderedMap), this avoids problems with nested values modification.
- Added support for deepcopy, see HandleDeepCopy method.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByPair

type ByPair struct {
	Pairs    []*Pair
	LessFunc func(a *Pair, j *Pair) bool
}

ByPair ordered map converted to Pairs.

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 MapKeyStep

type MapKeyStep string

MapKeyStep represents a map key, used with deepcopy.

func (MapKeyStep) Key

func (v MapKeyStep) Key() string

Key returns key name.

func (MapKeyStep) String

func (v MapKeyStep) String() string

type MapStep

type MapStep string

MapStep represents a map key value.

func (MapStep) Key

func (v MapStep) Key() string

Key returns key name.

func (MapStep) String

func (v MapStep) String() string

type OrderedMap

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

OrderedMap a map that preserves the order of the keys.

func FromPairs

func FromPairs(pairs []Pair) *OrderedMap

FromPairs creates ordered map from Pairs.

func New

func New() *OrderedMap

New creates new OrderedMap.

func (*OrderedMap) Clone

func (o *OrderedMap) Clone() *OrderedMap

Clone clones ordered map using deepcopy.

func (*OrderedMap) Delete

func (o *OrderedMap) Delete(key string)

Delete key from map.

func (*OrderedMap) Get

func (o *OrderedMap) Get(key string) (any, bool)

Get key.

func (*OrderedMap) GetNested

func (o *OrderedMap) GetNested(path string) (value any, found bool, err error)

GetNested returns nested value by path as string.

func (*OrderedMap) GetNestedMap

func (o *OrderedMap) GetNestedMap(path string) (m *OrderedMap, found bool, err error)

GetNestedMap returns nested OrderedMap by path as string.

func (*OrderedMap) GetNestedOrNil

func (o *OrderedMap) GetNestedOrNil(path string) any

GetNestedOrNil returns nil if values is not found or an error occurred.

func (*OrderedMap) GetNestedPath

func (o *OrderedMap) GetNestedPath(path Path) (value any, found bool, err error)

GetNestedPath returns nested value by Path.

func (*OrderedMap) GetNestedPathMap

func (o *OrderedMap) GetNestedPathMap(path Path) (m *OrderedMap, found bool, err error)

GetNestedPathMap returns nested OrderedMap by Path.

func (*OrderedMap) GetNestedPathOrNil

func (o *OrderedMap) GetNestedPathOrNil(path Path) any

GetNestedPathOrNil returns nil if values is not found or an error occurred.

func (*OrderedMap) GetOrNil

func (o *OrderedMap) GetOrNil(key string) any

GetOrNil gets key or returns nil if it doesn't exists.

func (*OrderedMap) HandleDeepCopy

func (o *OrderedMap) HandleDeepCopy(callback deepcopy.TranslateFn, steps deepcopy.Path, visited deepcopy.VisitedPtrMap) (*OrderedMap, deepcopy.CloneFn)

HandleDeepCopy implements deepcopy operation.

func (*OrderedMap) Keys

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

Keys method returns all keys as slice.

func (*OrderedMap) Len

func (o *OrderedMap) Len() int

Len returns number of keys.

func (OrderedMap) MarshalJSON

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

MarshalJSON implements JSON encoding.

func (*OrderedMap) MarshalYAML added in v0.5.0

func (o *OrderedMap) MarshalYAML() (any, error)

func (*OrderedMap) Set

func (o *OrderedMap) Set(key string, value any)

Set key.

func (*OrderedMap) SetNested

func (o *OrderedMap) SetNested(path string, value any) error

SetNested value defined by path, eg. "parameters.foo[123]".

func (*OrderedMap) SetNestedPath

func (o *OrderedMap) SetNestedPath(path Path, value any) error

SetNestedPath value defined by key, eg. Key{MapStep("parameters), MapStep("foo"), SliceStep(123)}.

func (*OrderedMap) Sort

func (o *OrderedMap) Sort(lessFunc func(a *Pair, b *Pair) bool)

Sort sorts keys/values using sort func.

func (*OrderedMap) SortKeys

func (o *OrderedMap) SortKeys(sortFunc func(keys []string))

SortKeys sorts keys using sort func.

func (*OrderedMap) ToMap

func (o *OrderedMap) ToMap() map[string]any

ToMap converts OrderedMap to native Go map.

func (*OrderedMap) UnmarshalJSON

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

UnmarshalJSON implements JSON decoding.

func (*OrderedMap) UnmarshalYAML added in v0.5.0

func (o *OrderedMap) UnmarshalYAML(node *yaml.Node) error

func (*OrderedMap) VisitAllRecursive

func (o *OrderedMap) VisitAllRecursive(callback VisitCallback)

VisitAllRecursive calls callback for each nested key in OrderedMap or []any.

type Pair

type Pair struct {
	Key   string
	Value any
}

Pair a key/value pair.

type Path

type Path []Step

Path to a nested value in the OrderedMap.

func PathFromStr

func PathFromStr(str string) Path

PathFromStr converts string to Path.

func (Path) First

func (v Path) First() Step

First returns path first step or nil.

func (Path) Last

func (v Path) Last() Step

Last returns path last step or nil.

func (Path) String

func (v Path) String() string

func (Path) WithoutFirst

func (v Path) WithoutFirst() Path

WithoutFirst returns path without first step or nil.

func (Path) WithoutLast

func (v Path) WithoutLast() Path

WithoutLast returns path without last step or nil.

type SliceStep

type SliceStep int

SliceStep represents a slice index.

func (SliceStep) Index

func (v SliceStep) Index() int

Index returns slice index.

func (SliceStep) String

func (v SliceStep) String() string

type Step

type Step interface {
	String() string
}

Step of Path.

type VisitCallback

type VisitCallback func(path Path, value any, parent any)

VisitCallback callback to visit each nested value in OrderedMap.

Jump to

Keyboard shortcuts

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