mapof

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 9 Imported by: 37

README

mapof

Generic, map-backed types (mapof.Any, mapof.Bool, mapof.Int, mapof.Int64, mapof.Float, mapof.String) that add type-safe getters/setters and schema integration on top of a plain Go map. Part of rosetta.

These exist so a map[string]any can be passed through the schema engine (and other rosetta tools) with typed accessors instead of repeated type assertions at every call site.

What matters here

  • Setters use pointer receivers and lazily allocate via makeNotNil(). A zero-value var m mapof.Bool is a nil map; calling m.SetBool(...) works because the setter takes *Bool and allocates the backing map on first write. This is the standard "guard the nil-map write" pattern — do not "simplify" setters to value receivers, and do not add a constructor requirement. Getters use value receivers (reading a nil map is safe).
  • The nilaway CI step runs with -exclude-test-files. .github/workflows/nilaway.yml invokes nilaway -exclude-test-files=true ./... directly, because the qbaware/nilaway-action hardcodes nilaway $PACKAGE_TO_SCAN and cannot pass the flag. The flag scopes analysis to production code; nil flows that exist only in test fixtures (e.g. a setter called on a zero-value map variable) are out of scope. The gate must exit with zero output — if it reports anything, fix the code, do not relax the gate.
  • mapof.Any implements the schema getter/setter interfaces (GetPointer, SetObject, SetValue, the typed GetXxxOK family). This is the seam that lets schema walk and mutate a generic map; changing those method signatures breaks schema path access.
  • GetXxx returns the zero value; GetXxxOK returns (value, ok). Use the OK form when "absent" and "present-but-zero" must be distinguished — the plain getters cannot tell them apart.

Documentation

Overview

Package mapof provides string-keyed map types with typed values and helper methods.

Any, String, Int, Int64, Float, Bool, and the generic Object[T] are all plain map types under their methods, so they can be ranged over, indexed, and serialized exactly like the maps they wrap. What they add is typed accessors — GetString, GetInt, GetBool, and their OK-returning variants — that convert a loosely-typed value on the way out, plus the Getter and Setter implementations that let the schema package address them by path.

That schema integration is the reason these types exist: they are the carriers a JSON-Schema-shaped document is read into and written back out of when there is no Go struct to bind to.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any

type Any map[string]any

Any is a map of string keys to arbitrary values with typed, coercing accessors.

func MapOfAny added in v0.22.0

func MapOfAny(value any) (Any, bool)

MapOfAny converts a supported map type (any of the mapof types, or a plain/pointer map[string]X) into a mapof.Any, returning the result and TRUE on success, or (nil, false) for unsupported types.

func NewAny added in v0.11.0

func NewAny() Any

NewAny returns a new, initialized Any map.

func (*Any) Append added in v0.16.0

func (x *Any) Append(key string, value any)

Append adds a new value to the provided key. If a value already exists for this key then it will be forced into a slice of values.

func (Any) Equal added in v0.24.8

func (x Any) Equal(value map[string]any) bool

Equal returns TRUE if this map deeply equals the provided map.

func (Any) GetAny

func (x Any) GetAny(key string) any

GetAny returns the raw value for the key, or nil if the key is not present.

func (Any) GetAnyOK added in v0.11.0

func (x Any) GetAnyOK(key string) (any, bool)

GetAnyOK returns the raw value for the key and TRUE if it is present, or (nil, false) if not.

func (Any) GetBool

func (x Any) GetBool(key string) bool

GetBool returns the value for the key coerced to bool, or FALSE if absent or not coercible.

func (Any) GetBoolOK added in v0.11.0

func (x Any) GetBoolOK(key string) (value bool, ok bool)

GetBoolOK returns the value for the key coerced to bool, with TRUE if it was present and coercible.

func (Any) GetFloat

func (x Any) GetFloat(key string) float64

GetFloat returns the value for the key coerced to float64, or 0 if absent or not coercible.

func (Any) GetFloatOK added in v0.11.0

func (x Any) GetFloatOK(key string) (value float64, ok bool)

GetFloatOK returns the value for the key coerced to float64, with TRUE if it was present and coercible.

func (Any) GetInt

func (x Any) GetInt(key string) int

GetInt returns the value for the key coerced to int, or 0 if absent or not coercible.

func (Any) GetInt64

func (x Any) GetInt64(key string) int64

GetInt64 returns the value for the key coerced to int64, or 0 if absent or not coercible.

func (Any) GetInt64OK added in v0.11.0

func (x Any) GetInt64OK(key string) (value int64, ok bool)

GetInt64OK returns the value for the key coerced to int64, with TRUE if it was present and coercible.

func (Any) GetIntOK added in v0.11.0

func (x Any) GetIntOK(key string) (value int, ok bool)

GetIntOK returns the value for the key coerced to int, with TRUE if it was present and coercible.

func (Any) GetMap added in v0.11.0

func (m Any) GetMap(name string) Any

GetMap returns a named property as a mapof.Any

func (Any) GetMapOfAny added in v0.25.26

func (m Any) GetMapOfAny(name string) Any

GetMapOfAny returns a named property as a mapof.Any

func (Any) GetMapOfString added in v0.25.26

func (m Any) GetMapOfString(name string) String

GetMapOfString returns a named property as a mapof.String

func (Any) GetPointer added in v0.14.0

func (x Any) GetPointer(key string) (any, bool)

GetPointer returns the raw value for the key (implements the schema PointerGetter interface).

func (Any) GetSliceOfAny added in v0.23.1

func (m Any) GetSliceOfAny(name string) []any

GetSliceOfAny returns a named property as a slice of any values

func (Any) GetSliceOfFloat added in v0.11.0

func (m Any) GetSliceOfFloat(name string) []float64

GetSliceOfFloat returns a named property as a slice of float64 values

func (Any) GetSliceOfInt added in v0.11.0

func (m Any) GetSliceOfInt(name string) []int

GetSliceOfInt returns a named property as a slice of int values

func (Any) GetSliceOfMap added in v0.11.0

func (m Any) GetSliceOfMap(name string) []Any

GetSliceOfMap returns a named property as a slice of mapof.Any objects.

func (Any) GetSliceOfPlainMap added in v0.23.2

func (m Any) GetSliceOfPlainMap(name string) []map[string]any

GetSliceOfPlainMap returns a named property as a slice of plain map[string]any objects.

func (Any) GetSliceOfString added in v0.11.0

func (m Any) GetSliceOfString(name string) []string

GetSliceOfString returns a named property as a slice of strings

func (Any) GetString

func (x Any) GetString(key string) string

GetString returns the value for the key coerced to string, or "" if absent or not coercible.

func (Any) GetStringOK added in v0.11.0

func (x Any) GetStringOK(key string) (value string, ok bool)

GetStringOK returns the value for the key coerced to string, with TRUE if it was present and coercible.

func (Any) GetTime added in v0.24.6

func (x Any) GetTime(key string) time.Time

GetTime returns the value for the key coerced to time.Time, or the zero time if absent or not coercible.

func (Any) GetTimeOK added in v0.24.6

func (x Any) GetTimeOK(key string) (value time.Time, ok bool)

GetTimeOK returns the value for the key coerced to time.Time, with TRUE if it was present and coercible.

func (Any) IsEmpty added in v0.16.3

func (x Any) IsEmpty() bool

IsEmpty returns TRUE if the map contains no elements.

func (Any) IsMap added in v0.29.0

func (x Any) IsMap() bool

IsMap returns TRUE, declaring this type a map for schema traversal. Implements schema.MapTyper.

func (Any) IsZeroValue added in v0.12.2

func (m Any) IsZeroValue(name string) bool

IsZeroValue returns TRUE if the named property is absent or holds a zero value.

func (Any) Keys added in v0.11.0

func (x Any) Keys() []string

Keys returns the map's keys in sorted order.

func (Any) Length added in v0.25.35

func (x Any) Length() int

Length returns the number of elements in the map

func (Any) MapOfAny added in v0.21.3

func (m Any) MapOfAny() map[string]any

MapOfAny implements the MapOfAny interface. It returns this value as a map[string]any

func (Any) MapOfString added in v0.21.3

func (m Any) MapOfString() map[string]string

MapOfString implements the MapOfString interface. It returns this value as a map[string]string

func (Any) NotEmpty added in v0.16.3

func (x Any) NotEmpty() bool

NotEmpty returns TRUE if the map contains one or more elements.

func (Any) NotEqual added in v0.24.8

func (x Any) NotEqual(value map[string]any) bool

NotEqual returns TRUE if this map does not deeply equal the provided map.

func (*Any) Remove added in v0.11.0

func (x *Any) Remove(key string) bool

Remove deletes the key from the map.

func (*Any) SetAny

func (x *Any) SetAny(key string, value any) bool

SetAny stores a non-zero value at the key (deleting the key when the value is zero to keep the map sparse).

func (*Any) SetBool

func (x *Any) SetBool(key string, value bool) bool

SetBool stores a bool value at the key.

func (*Any) SetFloat

func (x *Any) SetFloat(key string, value float64) bool

SetFloat stores a non-zero float64 at the key (deleting the key when the value is zero).

func (*Any) SetInt

func (x *Any) SetInt(key string, value int) bool

SetInt stores a non-zero int at the key (deleting the key when the value is zero).

func (*Any) SetInt64

func (x *Any) SetInt64(key string, value int64) bool

SetInt64 stores a non-zero int64 at the key (deleting the key when the value is zero).

func (*Any) SetObject added in v0.11.0

func (x *Any) SetObject(element schema.Element, path list.List, value any) error

SetObject descends the path (creating child maps as needed) and sets the value (implements the schema ObjectSetter interface).

func (*Any) SetString

func (x *Any) SetString(key string, value string) bool

SetString stores a non-empty string at the key (deleting the key when the value is empty).

func (*Any) SetValue added in v0.22.0

func (x *Any) SetValue(value any) error

SetValue replaces the entire map with the provided value, if it can be converted to a mapof.Any.

type Bool

type Bool map[string]bool

Bool is a map of string keys to bool values with typed accessors.

func NewBool added in v0.11.0

func NewBool() Bool

NewBool returns a new, initialized Bool map.

func (Bool) Equal added in v0.13.0

func (x Bool) Equal(value Bool) bool

Equal returns TRUE if this map has the same keys and values as the provided map.

func (Bool) GetBool

func (x Bool) GetBool(key string) bool

GetBool returns the value for the key, or FALSE if the key is not present.

func (Bool) GetBoolOK added in v0.11.0

func (x Bool) GetBoolOK(key string) (bool, bool)

GetBoolOK returns the value for the key and TRUE if it is present, or (false, false) if not.

func (Bool) IsEmpty added in v0.16.3

func (x Bool) IsEmpty() bool

IsEmpty returns TRUE if the map contains no elements.

func (Bool) IsMap added in v0.29.0

func (x Bool) IsMap() bool

IsMap returns TRUE, declaring this type a map for schema traversal. Implements schema.MapTyper.

func (Bool) Keys added in v0.11.2

func (x Bool) Keys() []string

Keys returns the map's keys in sorted order.

func (Bool) Length added in v0.25.35

func (x Bool) Length() int

Length returns the number of elements in the map

func (Bool) NotEmpty added in v0.16.3

func (x Bool) NotEmpty() bool

NotEmpty returns TRUE if the map contains one or more elements.

func (Bool) NotEqual added in v0.24.8

func (x Bool) NotEqual(value Bool) bool

NotEqual returns TRUE if this map differs from the provided map.

func (*Bool) Remove added in v0.10.0

func (x *Bool) Remove(key string) bool

Remove deletes the key from the map.

func (*Bool) SetBool

func (x *Bool) SetBool(key string, value bool) bool

SetBool stores the value at the key, allocating the map first if it is nil.

type Float

type Float map[string]float64

Float is a map of string keys to float64 values with typed accessors.

func NewFloat added in v0.11.0

func NewFloat() Float

NewFloat returns a new, initialized Float map.

func (Float) Equal added in v0.13.0

func (x Float) Equal(value map[string]float64) bool

Equal returns TRUE if this map has the same keys and values as the provided map.

func (Float) GetFloat

func (x Float) GetFloat(key string) float64

GetFloat returns the value for the key, or 0 if the key is not present.

func (Float) GetFloatOK added in v0.10.0

func (x Float) GetFloatOK(key string) (float64, bool)

GetFloatOK returns the value for the key and TRUE if it is present, or (0, false) if not.

func (Float) IsEmpty added in v0.16.3

func (x Float) IsEmpty() bool

IsEmpty returns TRUE if the map contains no elements.

func (Float) IsMap added in v0.29.0

func (x Float) IsMap() bool

IsMap returns TRUE, declaring this type a map for schema traversal. Implements schema.MapTyper.

func (Float) Keys added in v0.11.2

func (x Float) Keys() []string

Keys returns the map's keys in sorted order.

func (Float) Length added in v0.25.35

func (x Float) Length() int

Length returns the number of elements in the map

func (Float) NotEmpty added in v0.16.3

func (x Float) NotEmpty() bool

NotEmpty returns TRUE if the map contains one or more elements.

func (Float) NotEqual added in v0.24.8

func (x Float) NotEqual(value map[string]float64) bool

NotEqual returns TRUE if this map differs from the provided map.

func (*Float) Remove added in v0.10.0

func (x *Float) Remove(key string) bool

Remove deletes the key from the map.

func (*Float) SetFloat

func (x *Float) SetFloat(key string, value float64) bool

SetFloat stores a non-zero value at the key (deleting the key when the value is zero to keep the map sparse), allocating the map first if it is nil.

type Int

type Int map[string]int

Int is a map of string keys to int values with typed accessors.

func NewInt added in v0.11.0

func NewInt() Int

NewInt returns a new, initialized Int map.

func (Int) Equal added in v0.13.0

func (x Int) Equal(value map[string]int) bool

Equal returns TRUE if this map has the same keys and values as the provided map.

func (Int) GetInt

func (x Int) GetInt(key string) int

GetInt returns the value for the key, or 0 if the key is not present.

func (Int) GetIntOK added in v0.11.0

func (x Int) GetIntOK(key string) (int, bool)

GetIntOK returns the value for the key and TRUE if it is present, or (0, false) if not.

func (Int) IsEmpty added in v0.16.3

func (x Int) IsEmpty() bool

IsEmpty returns TRUE if the map contains no elements.

func (Int) IsMap added in v0.29.0

func (x Int) IsMap() bool

IsMap returns TRUE, declaring this type a map for schema traversal. Implements schema.MapTyper.

func (Int) Keys added in v0.11.2

func (x Int) Keys() []string

Keys returns the map's keys in sorted order.

func (Int) Length added in v0.25.35

func (x Int) Length() int

Length returns the number of elements in the map

func (Int) NotEmpty added in v0.16.3

func (x Int) NotEmpty() bool

NotEmpty returns TRUE if the map contains one or more elements.

func (Int) NotEqual added in v0.24.8

func (x Int) NotEqual(value map[string]int) bool

NotEqual returns TRUE if this map differs from the provided map.

func (*Int) Remove added in v0.10.0

func (x *Int) Remove(key string) bool

Remove deletes the key from the map.

func (*Int) SetInt

func (x *Int) SetInt(key string, value int) bool

SetInt stores a non-zero value at the key (deleting the key when the value is zero to keep the map sparse), allocating the map first if it is nil.

type Int64

type Int64 map[string]int64

Int64 is a map of string keys to int64 values with typed accessors.

func NewInt64 added in v0.11.0

func NewInt64() Int64

NewInt64 returns a new, initialized Int64 map.

func (Int64) Equal added in v0.13.0

func (x Int64) Equal(value map[string]int64) bool

Equal returns TRUE if this map has the same keys and values as the provided map.

func (Int64) GetInt64

func (x Int64) GetInt64(key string) int64

GetInt64 returns the value for the key, or 0 if the key is not present.

func (Int64) GetInt64OK added in v0.11.0

func (x Int64) GetInt64OK(key string) (int64, bool)

GetInt64OK returns the value for the key and TRUE if it is present, or (0, false) if not.

func (Int64) IsEmpty added in v0.16.3

func (x Int64) IsEmpty() bool

IsEmpty returns TRUE if the map contains no elements.

func (Int64) IsMap added in v0.29.0

func (x Int64) IsMap() bool

IsMap returns TRUE, declaring this type a map for schema traversal. Implements schema.MapTyper.

func (Int64) Keys added in v0.11.2

func (x Int64) Keys() []string

Keys returns the map's keys in sorted order.

func (Int64) Length added in v0.25.35

func (x Int64) Length() int

Length returns the number of elements in the map

func (Int64) NotEmpty added in v0.16.3

func (x Int64) NotEmpty() bool

NotEmpty returns TRUE if the map contains one or more elements.

func (Int64) NotEqual added in v0.24.8

func (x Int64) NotEqual(value map[string]int64) bool

NotEqual returns TRUE if this map differs from the provided map.

func (*Int64) Remove added in v0.10.0

func (x *Int64) Remove(key string) bool

Remove deletes the key from the map.

func (*Int64) SetInt64

func (x *Int64) SetInt64(key string, value int64) bool

SetInt64 stores a non-zero value at the key (deleting the key when the value is zero to keep the map sparse), allocating the map first if it is nil.

type Matchable added in v0.25.35

type Matchable[T Matcher] map[string]T

Matchable is a map of objects that implement the Matcher interface and can be queried using the Match and MatchOne methods.

func NewMatchable added in v0.25.35

func NewMatchable[T Matcher]() Matchable[T]

NewMatchable returns a fully initialized Matchable object

func (Matchable[T]) Equal added in v0.25.35

func (m Matchable[T]) Equal(value map[string]any) bool

Equal returns TRUE if this map deeply equals the provided map.

func (Matchable[T]) GetPointer added in v0.25.35

func (m Matchable[T]) GetPointer(key string) (any, bool)

GetPointer returns the value for the key (implements the schema PointerGetter interface).

func (Matchable[T]) IsEmpty added in v0.25.35

func (m Matchable[T]) IsEmpty() bool

IsEmpty returns TRUE if the map contains no elements.

func (Matchable[T]) IsMap added in v0.29.0

func (m Matchable[T]) IsMap() bool

IsMap returns TRUE, declaring this type a map for schema traversal. Implements schema.MapTyper.

func (Matchable[T]) IsZeroValue added in v0.25.35

func (m Matchable[T]) IsZeroValue(name string) bool

IsZeroValue returns TRUE if the named property is absent or holds a zero value.

func (Matchable[T]) Keys added in v0.25.35

func (m Matchable[T]) Keys() []string

Keys returns the map's keys in sorted order.

func (Matchable[T]) Length added in v0.25.35

func (m Matchable[T]) Length() int

Length returns the number of elements in the map

func (Matchable[T]) MapOfAny added in v0.25.35

func (m Matchable[T]) MapOfAny() map[string]any

MapOfAny implements the MapOfAny interface. It returns this value as a map[string]any

func (Matchable[T]) MapOfString added in v0.25.35

func (m Matchable[T]) MapOfString() map[string]string

MapOfString implements the MapOfString interface. It returns this value as a map[string]string

func (Matchable[T]) Match added in v0.25.35

func (m Matchable[T]) Match(expression exp.Expression) Matchable[T]

Match returns a new map containing all elements that match the given expression.

func (Matchable[T]) MatchOne added in v0.25.35

func (m Matchable[T]) MatchOne(expression exp.Expression) (T, bool)

MatchOne returns the first element that matches the given expression, along with a boolean indicating if a match was found.

func (Matchable[T]) NotEmpty added in v0.25.35

func (m Matchable[T]) NotEmpty() bool

NotEmpty returns TRUE if the map contains one or more elements.

func (Matchable[T]) NotEqual added in v0.25.35

func (m Matchable[T]) NotEqual(value map[string]any) bool

NotEqual returns TRUE if this map does not deeply equal the provided map.

func (*Matchable[T]) Remove added in v0.25.35

func (m *Matchable[T]) Remove(key string) bool

Remove deletes the key from the map.

func (Matchable[T]) Values added in v0.25.35

func (m Matchable[T]) Values() []T

Values returns all of the map's values (in unspecified order).

type Matcher added in v0.25.35

type Matcher interface {

	// Match returns TRUE if this object matches the provided predicate
	Match(predicate exp.Predicate) bool
}

Matcher interface wraps the Match method, which allows objects to declare if they match a given predicate

type Object added in v0.10.0

type Object[T any] map[string]T

Object is a map of string keys to values of a single type T, with schema-traversal support.

func NewObject added in v0.10.0

func NewObject[T any]() Object[T]

NewObject returns a new, initialized Object map.

func (Object[T]) GetPointer added in v0.14.0

func (object Object[T]) GetPointer(name string) (any, bool)

GetPointer returns the value for the key (implements the schema PointerGetter interface).

func (Object[T]) IsEmpty added in v0.16.3

func (x Object[T]) IsEmpty() bool

IsEmpty returns TRUE if the map contains no elements.

func (Object[T]) IsMap added in v0.29.0

func (x Object[T]) IsMap() bool

IsMap returns TRUE, declaring this type a map for schema traversal. Implements schema.MapTyper.

func (Object[T]) IsZeroValue added in v0.12.2

func (object Object[T]) IsZeroValue(name string) bool

IsZeroValue returns TRUE if the named property is absent or holds a zero value.

func (Object[T]) Keys added in v0.11.2

func (x Object[T]) Keys() []string

Keys returns the map's keys in sorted order.

func (Object[T]) Length added in v0.25.35

func (x Object[T]) Length() int

Length returns the number of elements in the map

func (Object[T]) NotEmpty added in v0.16.3

func (x Object[T]) NotEmpty() bool

NotEmpty returns TRUE if the map contains one or more elements.

func (*Object[T]) Remove added in v0.10.0

func (object *Object[T]) Remove(key string) bool

Remove deletes the key from the map.

func (*Object[T]) SetObject added in v0.10.0

func (object *Object[T]) SetObject(element schema.Element, path list.List, value any) error

SetObject descends the path (creating child entries as needed) and sets the value (implements the schema ObjectSetter interface).

type Slices added in v0.25.10

type Slices[K comparable, V comparable] map[K][]V

Slices is a map from keys to slices of values, i.e. a one-to-many multimap.

func (*Slices[K, V]) Add added in v0.25.10

func (s *Slices[K, V]) Add(key K, value V)

Add appends a value to the slice stored at the key, creating the slice if necessary.

func (Slices[K, V]) Flatten added in v0.25.11

func (s Slices[K, V]) Flatten() []V

Flatten returns all values across every key as a single slice (in unspecified order).

type String

type String map[string]string

String is a map of string keys to string values with typed accessors.

func NewString added in v0.11.0

func NewString() String

NewString returns a new, initialized String map.

func (String) Equal added in v0.13.0

func (x String) Equal(value map[string]string) bool

Equal returns TRUE if this map has the same keys and values as the provided map.

func (String) GetString

func (x String) GetString(key string) string

GetString returns the value for the key, or "" if the key is not present.

func (String) GetStringOK added in v0.11.0

func (x String) GetStringOK(key string) (string, bool)

GetStringOK returns the value for the key and TRUE if it is present, or ("", false) if not.

func (String) IsEmpty added in v0.16.3

func (x String) IsEmpty() bool

IsEmpty returns TRUE if the map contains no elements.

func (String) IsMap added in v0.29.0

func (x String) IsMap() bool

IsMap returns TRUE, declaring this type a map for schema traversal. Implements schema.MapTyper.

func (String) Keys added in v0.11.2

func (x String) Keys() []string

Keys returns the map's keys in sorted order.

func (String) Length added in v0.25.35

func (x String) Length() int

Length returns the number of elements in the map

func (String) MapOfAny added in v0.21.3

func (x String) MapOfAny() map[string]any

MapOfAny returns the map's contents as a map[string]any.

func (String) MapOfString added in v0.21.3

func (x String) MapOfString() map[string]string

MapOfString returns the underlying map[string]string.

func (String) NotEmpty added in v0.16.3

func (x String) NotEmpty() bool

NotEmpty returns TRUE if the map contains one or more elements.

func (String) NotEqual added in v0.24.8

func (x String) NotEqual(value map[string]string) bool

NotEqual returns TRUE if this map differs from the provided map.

func (*String) Remove added in v0.10.0

func (x *String) Remove(key string) bool

Remove deletes the key from the map.

func (*String) SetString

func (x *String) SetString(key string, value string) bool

SetString stores a non-empty value at the key (deleting the key when the value is empty to keep the map sparse), allocating the map first if it is nil.

Jump to

Keyboard shortcuts

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