objects

package
v0.0.0-...-86643de Latest Latest
Warning

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

Go to latest
Published: May 10, 2019 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Overview

Objects package adds more functionality to maps and other data objects.

Index

Constants

This section is empty.

Variables

View Source
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 M

func M(keyAndValuePairs ...interface{}) Map

M is a shortcut method for NewMap.

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

func NewMapFromBase64String(data string) (Map, error)

NewMapFromBase64String creates a new map from a Base64 string representation

func NewMapFromJSON

func NewMapFromJSON(data string) (Map, error)

NewMapFromJSON creates a new map from a JSON string representation

func NewMapFromSignedBase64String

func NewMapFromSignedBase64String(data, key string) (Map, error)

NewMapFromSignedBase64String creates a new map from a signed Base64 string representation

func NewMapFromURLQuery

func NewMapFromURLQuery(query string) (Map, error)

NewMapFromURLQuery generates a new map by parsing the specified query.

For queries with multiple values, the first value is selected.

func NewMapFromURLValues

func NewMapFromURLValues(vals url.Values) (Map, error)

func (Map) Base64

func (d Map) Base64() (string, error)

Base64 converts the map to a base64 string

func (Map) Copy

func (d Map) Copy() Map

Copy creates a shallow copy of the Map.

func (Map) Exclude

func (d Map) Exclude(exclude []string) Map

Exclude returns a new Map with the keys in the specified []string excluded.

func (Map) Get

func (d Map) Get(keypath string) interface{}

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

func (d Map) GetMap(keypath string) Map

GetMap gets another Map from this one, or panics if the object is missing or not a Map.

func (Map) GetOrDefault

func (d Map) GetOrDefault(keypath string, defaultValue interface{}) interface{}

GetWithDefault gets the value at the specified keypath, or returns the defaultValue if none could be found.

func (Map) GetString

func (d Map) GetString(keypath string) string

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

func (d Map) GetStringOrDefault(keypath, defaultValue string) string

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

func (d Map) GetStringOrEmpty(keypath string) string

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

func (d Map) Has(path string) bool

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

func (d Map) Hash() (string, error)

Hash gets the hash of the map with no security key.

Will return an error if Base64ing the map fails.

func (Map) HashWithKey

func (d Map) HashWithKey(key string) (string, error)

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) JSON

func (d Map) JSON() (string, error)

JSON converts the map to a JSON string

func (Map) MSI

func (d Map) MSI() map[string]interface{}

MSI is a shortcut method to get the current map as a normal map[string]interface{}.

func (Map) Merge

func (d Map) Merge(merge Map) Map

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

func (d Map) MergeHere(merge Map) Map

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

func (d Map) Set(keypath string, value interface{}) Map

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

func (d Map) SignedBase64(key string) (string, error)

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

func (d Map) Transform(transformer func(key string, value interface{}) (string, interface{})) Map

Transform builds a new map giving the transformer a chance to change the keys and values as it goes.

func (Map) TransformKeys

func (d Map) TransformKeys(mapping map[string]string) Map

TransformKeys builds a new map using the specified key mapping.

Unspecified keys will be unaltered.

func (Map) URLQuery

func (d Map) URLQuery() (string, error)

URLQuery gets an encoded URL query representing the given map.

func (Map) URLValues

func (d Map) URLValues() url.Values

Jump to

Keyboard shortcuts

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