Documentation ¶
Overview ¶
Objects package adds more functionality to maps and other data objects.
Index ¶
- Variables
- type Map
- func M(keyAndValuePairs ...interface{}) Map
- func NewMap(keyAndValuePairs ...interface{}) Map
- func NewMapFromBase64String(data string) (Map, error)
- func NewMapFromJSON(data string) (Map, error)
- func NewMapFromSignedBase64String(data, key string) (Map, error)
- func NewMapFromURLQuery(query string) (Map, error)
- func NewMapFromURLValues(vals url.Values) (Map, error)
- func (d Map) Base64() (string, error)
- func (d Map) Copy() Map
- func (d Map) Exclude(exclude []string) Map
- func (d Map) Get(keypath string) interface{}
- func (d Map) GetMap(keypath string) Map
- func (d Map) GetOrDefault(keypath string, defaultValue interface{}) interface{}
- func (d Map) GetString(keypath string) string
- func (d Map) GetStringOrDefault(keypath, defaultValue string) string
- func (d Map) GetStringOrEmpty(keypath string) string
- func (d Map) Has(path string) bool
- func (d Map) Hash() (string, error)
- func (d Map) HashWithKey(key string) (string, error)
- func (d Map) JSON() (string, error)
- func (d Map) MSI() map[string]interface{}
- func (d Map) Merge(merge Map) Map
- func (d Map) MergeHere(merge Map) Map
- func (d Map) Set(keypath string, value interface{}) Map
- func (d Map) SignedBase64(key string) (string, error)
- func (d Map) Transform(transformer func(key string, value interface{}) (string, interface{})) Map
- func (d Map) TransformKeys(mapping map[string]string) Map
- func (d Map) URLQuery() (string, error)
- func (d Map) URLValues() url.Values
Constants ¶
This section is empty.
Variables ¶
var ( // PathSeparator is the character used to separate the elements // of the keypath. // // For example, `location.address.city` PathSeparator string = "." // SignatureSeparator is the character that is used to // separate the Base64 string from the security signature. SignatureSeparator = "_" )
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map map[string]interface{}
Map is a map[string]interface{} with additional helpful functionality.
You can use Map functionality on any map[string]interface{} using the following format:
data := map[string]interface{}{"name": "Stew"} objects.Map(data).Get("name") // returns "Stew"
func NewMap ¶
func NewMap(keyAndValuePairs ...interface{}) Map
NewMap creates a new map. You may also use the M shortcut method.
The arguments follow a key, value pattern.
Panics ¶
Panics if any key arugment is non-string or if there are an odd number of arguments.
Example ¶
To easily create Maps:
m := objects.M("name", "Mat", "age", 29, "subobj", objects.M("active", true)) // creates a Map equivalent to m := map[string]interface{}{"name": "Mat", "age": 29, "subobj": map[string]interface{}{"active": true}}
func NewMapFromBase64String ¶
NewMapFromBase64String creates a new map from a Base64 string representation
func NewMapFromJSON ¶
NewMapFromJSON creates a new map from a JSON string representation
func NewMapFromSignedBase64String ¶
NewMapFromSignedBase64String creates a new map from a signed Base64 string representation
func NewMapFromURLQuery ¶
NewMapFromURLQuery generates a new map by parsing the specified query.
For queries with multiple values, the first value is selected.
func (Map) Get ¶
Get gets the value from the map. Supports deep nesting of other maps, For example:
m = Map{"name":Map{"First": "Mat", "Last": "Ryer"}} m.Get("name.Last") // returns "Ryer"
func (Map) GetMap ¶
GetMap gets another Map from this one, or panics if the object is missing or not a Map.
func (Map) GetOrDefault ¶
GetWithDefault gets the value at the specified keypath, or returns the defaultValue if none could be found.
func (Map) GetString ¶
GetString gets a string value from the map at the given keypath, or panics if one is not available, or is of the wrong type.
func (Map) GetStringOrDefault ¶
GetStringOrDefault gets the string value at the specified keypath, or returns the defaultValue if none could be found. Will panic if the object is there but of the wrong type.
func (Map) GetStringOrEmpty ¶
GetStringOrEmpty gets the string value at the specified keypath or returns an empty string if none could be fo und. Will panic if the object is there but of the wrong type.
func (Map) Has ¶
Has gets whether the Map has the specified field or not. Supports deep nesting of other maps.
For example:
m := map[string]interface{}{"parent": map[string]interface{}{"childname": "Luke"}} m.Has("parent.childname") // return true
func (Map) Hash ¶
Hash gets the hash of the map with no security key.
Will return an error if Base64ing the map fails.
func (Map) HashWithKey ¶
HashWithKey gets the a hash of the map, signed by the specified security key.
Will return an error if Base64ing the map fails.
func (Map) MSI ¶
MSI is a shortcut method to get the current map as a normal map[string]interface{}.
func (Map) Merge ¶
Merge blends the specified map with a copy of this map and returns the result.
Keys that appear in both will be selected from the specified map.
func (Map) MergeHere ¶
Merge blends the specified map with this map and returns the current map.
Keys that appear in both will be selected from the specified map. The original map will be modified.
func (Map) Set ¶
Set sets a value in the map. Supports dot syntax to set deep values.
For example,
m.Set("name.first", "Mat")
The above code sets the 'first' field on the 'name' object in the m Map.
If objects are nil along the way, Set creates new Map objects as needed.
func (Map) SignedBase64 ¶
SignedBase64 converts the map to a base64 string and signs it using the provided key. The returned data is the base64 string plus an appended signature.
Will return an error if Base64ing the map fails.
func (Map) Transform ¶
Transform builds a new map giving the transformer a chance to change the keys and values as it goes.
func (Map) TransformKeys ¶
TransformKeys builds a new map using the specified key mapping.
Unspecified keys will be unaltered.