Documentation ¶
Overview ¶
Utilities for converting, manipulating, and iterating over maps
Index ¶
- Variables
- func Append(maps ...map[string]interface{}) map[string]interface{}
- func Apply(input interface{}, fn ApplyFunc) map[string]interface{}
- func ApplyStruct(input interface{}, fn ApplyFunc) map[string]interface{}
- func Autotype(input interface{}) map[string]interface{}
- func CoalesceMap(data map[string]interface{}, fieldJoiner string) (map[string]interface{}, error)
- func CoalesceMapTyped(data map[string]interface{}, fieldJoiner string, typePrefixSeparator string) (map[string]interface{}, []error)
- func Compact(input map[string]interface{}) (map[string]interface{}, error)
- func DeepCopy(input interface{}) map[string]interface{}
- func DeepCopyStruct(input interface{}) map[string]interface{}
- func DeepGet(data interface{}, path []string, fallbacks ...interface{}) interface{}
- func DeepGetBool(data interface{}, path []string) bool
- func DeepGetString(data interface{}, path []string) string
- func DeepJoin(input interface{}, innerJoiner string, outerJoiner string, ...) string
- func DeepSet(data interface{}, path []string, value interface{}) interface{}
- func Delete(data interface{}, key interface{}) error
- func DiffuseMap(data map[string]interface{}, fieldJoiner string) (map[string]interface{}, error)
- func DiffuseMapTyped(data map[string]interface{}, fieldJoiner string, typePrefixSeparator string) (map[string]interface{}, []error)
- func Fprintf(w io.Writer, format string, data ...interface{})
- func Get(data interface{}, key string, fallback ...interface{}) interface{}
- func Join(input interface{}, innerJoiner string, outerJoiner string) string
- func Keys(input interface{}) []interface{}
- func MapValues(input interface{}) []interface{}
- func Merge(first interface{}, second interface{}, options ...MergeOption) (map[string]interface{}, error)
- func Pluck(sliceOfMaps interface{}, key []string) []interface{}
- func Printf(format string, data ...interface{})
- func Set(data interface{}, key interface{}, value interface{}) error
- func Split(input string, innerJoiner string, outerJoiner string) map[string]interface{}
- func Sprintf(format string, data ...interface{}) string
- func StringKeys(input interface{}) []string
- func Stringify(input map[string]interface{}) map[string]string
- func StructFromMap(input map[string]interface{}, populate interface{}) error
- func TaggedStructFromMap(input interface{}, populate interface{}, tagname string) error
- func TaggedStructFromMapFunc(input interface{}, populate interface{}, tagname string, ...) error
- func Walk(input interface{}, walkFn WalkFunc) error
- func WalkStruct(input interface{}, walkFn WalkFunc) error
- type ApplyFunc
- type ConversionFunc
- type Item
- type ItemFunc
- type IterOptions
- type KeyTransformFunc
- type Map
- func (self *Map) Auto(key string, fallbacks ...interface{}) interface{}
- func (self *Map) Bool(key string) bool
- func (self *Map) Bytes(key string) []byte
- func (self *Map) Duration(key string, fallbacks ...interface{}) time.Duration
- func (self *Map) Each(fn ItemFunc, tagName ...string) error
- func (self *Map) Err(key string) error
- func (self *Map) Float(key string, fallbacks ...interface{}) float64
- func (self *Map) Fprintf(w io.Writer, format string)
- func (self *Map) Get(key string, fallbacks ...interface{}) typeutil.Variant
- func (self *Map) Int(key string, fallbacks ...interface{}) int64
- func (self *Map) IsZero(key string) bool
- func (self *Map) Iter(opts ...IterOptions) <-chan Item
- func (self *Map) Keys(tagName ...string) []interface{}
- func (self *Map) Len() int
- func (self *Map) Map(key string, tagName ...string) map[typeutil.Variant]typeutil.Variant
- func (self *Map) MapNative(tagName ...string) map[string]interface{}
- func (self *Map) MarshalJSON() ([]byte, error)
- func (self *Map) MarshalXML(e *xml.Encoder, start xml.StartElement) error
- func (self *Map) Set(key string, value interface{}) typeutil.Variant
- func (self *Map) SetIfZero(key string, value interface{}) (typeutil.Variant, bool)
- func (self *Map) SetMarshalXmlGeneric(yes bool)
- func (self *Map) SetMarshalXmlKeyFunc(fn KeyTransformFunc)
- func (self *Map) SetRootTagName(root string)
- func (self *Map) SetValueIfNonZero(key string, value interface{}) (typeutil.Variant, bool)
- func (self *Map) Slice(key string) []typeutil.Variant
- func (self *Map) Sprintf(format string) string
- func (self *Map) String(key string, fallbacks ...interface{}) string
- func (self *Map) StringKeys(tagName ...string) []string
- func (self *Map) Strings(key string) []string
- func (self *Map) Tag(key string) *Map
- func (self *Map) Time(key string, fallbacks ...interface{}) time.Time
- func (self *Map) UnmarshalJSON(data []byte) error
- func (self *Map) Value() interface{}
- func (self *Map) Walk(fn WalkFunc) error
- type MergeOption
- type MergeOptions
- type WalkFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var MapXmlRootTagName = `data`
var MapXmlStructTagName = `xml`
var SkipDescendants = errors.New("skip descendants")
var UnmarshalStructTag string = `maputil`
Functions ¶
func Apply ¶
Recursively walk the given map, calling the ApplyFunc for each leaf value. If the second return value from the function is true, that value in the struct will be replaced with the first return value. If false, the value will be left as-is.
func ApplyStruct ¶ added in v1.6.4
The same as Apply(), but will descend into structs.
func Autotype ¶
func Autotype(input interface{}) map[string]interface{}
Recursively walk the given map, performing automatic type conversion on all leaf nodes.
func CoalesceMap ¶
Take a deeply-nested map and return a flat (non-nested) map with keys whose intermediate tiers are joined with fieldJoiner
func CoalesceMapTyped ¶
func CoalesceMapTyped(data map[string]interface{}, fieldJoiner string, typePrefixSeparator string) (map[string]interface{}, []error)
Take a deeply-nested map and return a flat (non-nested) map with keys whose intermediate tiers are joined with fieldJoiner Additionally, values will be converted to strings and keys will be prefixed with the datatype of the value
func DeepCopy ¶
func DeepCopy(input interface{}) map[string]interface{}
Perform a deep copy of the given map.
func DeepCopyStruct ¶ added in v1.6.3
func DeepCopyStruct(input interface{}) map[string]interface{}
Perform a deep copy of the given map or struct, returning a map.
func DeepGetBool ¶
func DeepGetString ¶
func DeepJoin ¶
func DeepJoin(input interface{}, innerJoiner string, outerJoiner string, nestedSeparator string) string
Join the given map, using innerJoiner to join keys and values, and outerJoiner to join the resulting key-value lines.
func Delete ¶ added in v1.8.11
func Delete(data interface{}, key interface{}) error
Delete a key to a given value in the given map.
func DiffuseMap ¶
Take a flat (non-nested) map keyed with fields joined on fieldJoiner and return a deeply-nested map
func DiffuseMapTyped ¶
func DiffuseMapTyped(data map[string]interface{}, fieldJoiner string, typePrefixSeparator string) (map[string]interface{}, []error)
Take a flat (non-nested) map keyed with fields joined on fieldJoiner and return a deeply-nested map
func Join ¶
Join the given map, using innerJoiner to join keys and values, and outerJoiner to join the resulting key-value lines.
func Keys ¶
func Keys(input interface{}) []interface{}
Return an interface slice of the keys of the given map.
func MapValues ¶
func MapValues(input interface{}) []interface{}
Return the values from the given map.
func Merge ¶
func Merge(first interface{}, second interface{}, options ...MergeOption) (map[string]interface{}, error)
Recursively merge the contents of the second map into the first one and return the result.
func Printf ¶ added in v1.8.41
func Printf(format string, data ...interface{})
Same as Sprintf, but prints its output to standard output.
Example (DeeplyNestedKeys) ¶
Printf("Hello ${details.0.value|guest}! Your IP is: ${details.1.value|(unknown)}", map[string]interface{}{ `details`: []map[string]interface{}{ { `key`: `username`, `value`: `friend`, }, { `key`: `ipaddress`, `value`: `127.0.0.1`, }, }, })
Output: Hello friend! Your IP is: 127.0.0.1
Example (SuppliedWithData) ¶
Printf("Hello ${username|guest}! Your IP is: ${ipaddress|(unknown)}", map[string]interface{}{ `username`: `friend`, `ipaddress`: `127.0.0.1`, })
Output: Hello friend! Your IP is: 127.0.0.1
Example (UsingDefaultValues) ¶
Printf("Hello ${username|guest}! Your IP is: ${ipaddress|(unknown)}")
Output: Hello guest! Your IP is: (unknown)
func Set ¶ added in v1.5.53
func Set(data interface{}, key interface{}, value interface{}) error
Set a key to a given value in the given map, reflect.Map Value, or slice/array.
func Split ¶
Split the given string, first on outerJoiner to form key-value lines, then each line on innerJoiner. Populates a map and returns the result.
func Sprintf ¶ added in v1.8.41
Format the given string in the same manner as fmt.Sprintf, except data items that are maps or Map objects will be expanded using special patterns in the format string. Deeply-nested map values can be referenced using a format string "${path.to.value}". Missing keys will return an empty string, or a fallback value may be provided like so: "${path.to.value|fallback}". The value may also specify a standard fmt.Sprintf pattern with "${path.to.value:%02d}" (or "${path.to.value|fallback:%02d}" for fallback values.) Finally, a special case for time.Time values allows for the format string to be passed to time.Format: "${path.to.time:%January 2, 2006 (3:04pm)}".
func StringKeys ¶
func StringKeys(input interface{}) []string
Return a slice of strings representing the keys of the given map.
func StructFromMap ¶
Same as TaggedStructFromMapFunc, but no value conversion and uses the "maputil" struct tag.
func TaggedStructFromMap ¶
Same as TaggedStructFromMapFunc, but does not perform any value conversion.
func TaggedStructFromMapFunc ¶
func TaggedStructFromMapFunc(input interface{}, populate interface{}, tagname string, converter ConversionFunc) error
Take an input map, and populate the struct instance pointed to by "populate". Use the values of the tagname tag to inform which map keys should be used to fill struct fields, and if a Conversion function is given, that function will be used to allow values to be converted in preparation for becoming struct field values.
func Walk ¶
Recursively walk through the given map, calling walkFn for each intermediate and leaf value.
func WalkStruct ¶ added in v1.6.3
Recursively walk through the given map, calling walkFn for each intermediate and leaf value. This form behaves identically to Walk(), except that it will also recurse into structs, calling walkFn for all intermediate structs and fields.
Types ¶
type ConversionFunc ¶
type Item ¶ added in v1.8.33
type IterOptions ¶ added in v1.8.34
type KeyTransformFunc ¶ added in v1.8.26
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
A Map object (or "M" object) is a utility struct that makes it straightforward to work with interface data types that contain map-like data (has a reflect.Kind equal to reflect.Map).
func M ¶
func M(data interface{}) *Map
Create a new Variant map object from the given value (which should be a map of some kind).
func (*Map) Err ¶ added in v1.7.35
Return the value at key as an error, or nil if the value is not an error.
func (*Map) Fprintf ¶ added in v1.8.41
Uses the extended Fprintf in this package, passing this map as the data used in the given format string.
func (*Map) Get ¶
Retrieve a value from the Map by the given dot.separated key, or return a fallback value. Return values are a typeutil.Variant, which can be easily coerced into various types.
func (*Map) IsZero ¶ added in v1.7.14
Return whether the value at the given key is that type's zero value.
func (*Map) Iter ¶ added in v1.8.33
func (self *Map) Iter(opts ...IterOptions) <-chan Item
func (*Map) Keys ¶ added in v1.8.1
Return the keys in this Map object. You may specify the name of a struct tag on the underlying object to use for generating key names.
func (*Map) Map ¶
Return the value at key as a Map. If the resulting value is nil or not a map type, a null Map will be returned. All values retrieved from a null Map will return that type's zero value.
func (*Map) MarshalJSON ¶
func (*Map) MarshalXML ¶ added in v1.8.21
Marshals the current data into XML. Nested maps are output as nested elements. Map values that are scalars (strings, numbers, bools, dates/times) will appear as attributes on the parent element.
func (*Map) SetIfZero ¶ added in v1.7.14
Set a value in the Map at the given dot.separated key to a value, but only if the current value at that key is that type's zero value.
func (*Map) SetMarshalXmlGeneric ¶ added in v1.8.21
func (*Map) SetMarshalXmlKeyFunc ¶ added in v1.8.26
func (self *Map) SetMarshalXmlKeyFunc(fn KeyTransformFunc)
Set a function that will be used to generate XML tag names when calling MarshalXML. This works for all keys, including ones that appear inside of maps.
func (*Map) SetRootTagName ¶ added in v1.8.21
set the name of the root XML tag, used by MarshalXML.
func (*Map) SetValueIfNonZero ¶ added in v1.7.15
Set a value in the Map at the given dot.separated key to a value, but only if the new value is not a zero value.
func (*Map) Slice ¶
Return the value at key as a slice. Scalar values will be returned as a slice containing only that value.
func (*Map) Sprintf ¶ added in v1.8.41
Uses the extended Sprintf in this package, passing this map as the data used in the given format string.
func (*Map) StringKeys ¶ added in v1.8.1
A string slice version of Keys()
func (*Map) Tag ¶ added in v1.8.2
Specify which struct tag to honor for generating field names when then underlying data is a struct.
func (*Map) UnmarshalJSON ¶ added in v1.8.44
type MergeOptions ¶ added in v1.6.31
type MergeOptions []MergeOption
func (MergeOptions) Has ¶ added in v1.6.31
func (self MergeOptions) Has(option MergeOption) bool