Documentation
¶
Overview ¶
Package ordered implements an ordered map type.
Index ¶
- Variables
- func Equal[K comparable, V any](a, b *Map[K, V]) bool
- type Map
- func (m *Map[K, V]) Contains(k K) bool
- func (m *Map[K, V]) Delete(k K)
- func (m *Map[K, V]) Get(k K) (V, bool)
- func (m *Map[K, V]) IsZero() bool
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) MarshalJSON() ([]byte, error)
- func (m *Map[K, V]) MarshalYAML() (any, error)
- func (m *Map[K, V]) Range(f func(k K, v V) error) error
- func (m *Map[K, V]) Replace(old, new K, v V)
- func (m *Map[K, V]) Set(k K, v V)
- func (m *Map[K, V]) ToMap() map[K]V
- func (m *Map[K, V]) UnmarshalJSON(b []byte) error
- func (m *Map[K, V]) UnmarshalYAML(n *yaml.Node) error
- type MapSA
- type MapSS
- type Slice
- type Strings
- type Tuple
- type TupleSA
- type TupleSS
Constants ¶
This section is empty.
Variables ¶
var EqualSA = Equal[string, any]
EqualSA is a convenience alias to reduce keyboard wear.
var EqualSS = Equal[string, string]
EqualSS is a convenience alias to reduce keyboard wear.
Functions ¶
Types ¶
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is an order-preserving map with string keys. It is intended for working with YAML in an order-preserving way (off-spec, strictly speaking) and JSON (more of the same).
func MapFromItems ¶
func MapFromItems[K comparable, V any](ps ...Tuple[K, V]) *Map[K, V]
MapFromItems creates an Map with some items.
func NewMap ¶
func NewMap[K comparable, V any](cap int) *Map[K, V]
NewMap returns a new empty map with a given initial capacity.
func (*Map[K, V]) Delete ¶
func (m *Map[K, V]) Delete(k K)
Delete deletes a key from the map. It does nothing if the key is not in the map.
func (*Map[K, V]) IsZero ¶
IsZero reports if m is nil or empty. It is used by yaml.v3 to check emptiness.
func (*Map[K, V]) MarshalJSON ¶
MarshalJSON marshals the ordered map to JSON. It preserves the map order in the output.
func (*Map[K, V]) MarshalYAML ¶
MarshalYAML returns a *yaml.Node encoding this map (in order), or an error if any of the items could not be encoded into a *yaml.Node.
func (*Map[K, V]) Range ¶
Range ranges over the map (in order). If f returns an error, it stops ranging and returns that error.
func (*Map[K, V]) Replace ¶
func (m *Map[K, V]) Replace(old, new K, v V)
Replace replaces an old key in the same spot with a new key and value. If the old key doesn't exist in the map, the item is inserted at the end. If the new key already exists in the map (and isn't equal to the old key), then it is deleted. This provides a way to change a single key in-place (easier than deleting the old key and all later keys, adding the new key, then restoring the rest).
func (*Map[K, V]) Set ¶
func (m *Map[K, V]) Set(k K, v V)
Set sets the value for the given key. If the key exists, it remains in its existing spot, otherwise it is added to the end of the map.
func (*Map[K, V]) ToMap ¶
func (m *Map[K, V]) ToMap() map[K]V
ToMap creates a regular (un-ordered) map containing the same data.
func (*Map[K, V]) UnmarshalJSON ¶
UnmarshalJSON unmarshals to JSON. It only supports K = string. This is yaml.Unmarshal in a trenchcoat (YAML is a superset of JSON).
func (*Map[K, V]) UnmarshalYAML ¶
UnmarshalYAML unmarshals a YAML mapping node into this map. It only supports K = string. Where yaml.v3 typically infers map[string]any for unmarshaling mappings into any, this method chooses *Map[string, any] instead. If V = *yaml.Node, then the value nodes are not decoded. This is useful for a shallow unmarshaling step.
type Slice ¶
type Slice []any
Slice is []any, but unmarshaling into it prefers *Map[string,any] over map[string]any.
type Strings ¶
type Strings []string
Strings is []string, but unmarshaling handles both sequences and single scalars.
func (*Strings) UnmarshalYAML ¶
UnmarshalYAML unmarshals n depending on its Kind as either - a sequence of strings (into a slice), or - a single string (into a one-element slice). For example, unmarshaling either `["foo"]` or `"foo"` should result in a one-element slice (`Strings{"foo"}`).
type Tuple ¶
type Tuple[K comparable, V any] struct { Key K Value V // contains filtered or unexported fields }
Tuple is used for storing values in Map.