Categorygithub.com/stretchr/objx
modulepackage
0.5.2
Repository: https://github.com/stretchr/objx.git
Documentation: pkg.go.dev

# README

Objx

Build Status Go Report Card Maintainability Test Coverage Sourcegraph GoDoc

Objx - Go package for dealing with maps, slices, JSON and other data.

Get started:

Overview

Objx provides the objx.Map type, which is a map[string]interface{} that exposes a powerful Get method (among others) that allows you to easily and quickly get access to data within the map, without having to worry too much about type assertions, missing data, default values etc.

Pattern

Objx uses a predictable pattern to make access data from within map[string]interface{} easy. Call one of the objx. functions to create your objx.Map to get going:

m, err := objx.FromJSON(json)

NOTE: Any methods or functions with the Must prefix will panic if something goes wrong, the rest will be optimistic and try to figure things out without panicking.

Use Get to access the value you're interested in. You can use dot and array notation too:

 m.Get("places[0].latlng")

Once you have sought the Value you're interested in, you can use the Is* methods to determine its type.

 if m.Get("code").IsStr() { // Your code... }

Or you can just assume the type, and use one of the strong type methods to extract the real value:

m.Get("code").Int()

If there's no value there (or if it's the wrong type) then a default value will be returned, or you can be explicit about the default value.

 Get("code").Int(-1)

If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating, manipulating and selecting that data. You can find out more by exploring the index below.

Reading data

A simple example of how to use Objx:

// Use MustFromJSON to make an objx.Map from some JSON
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)

// Get the details
name := m.Get("name").Str()
age := m.Get("age").Int()

// Get their nickname (or use their name if they don't have one)
nickname := m.Get("nickname").Str(name)

Ranging

Since objx.Map is a map[string]interface{} you can treat it as such. For example, to range the data, do what you would expect:

m := objx.MustFromJSON(json)
for key, value := range m {
  // Your code...
}

Installation

To install Objx, use go get:

go get github.com/stretchr/objx

Staying up to date

To update Objx to the latest version, run:

go get -u github.com/stretchr/objx

Supported go versions

We currently support the three recent major Go versions.

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

# Functions

FromBase64 creates a new Obj containing the data specified in the Base64 string.
FromJSON creates a new Map containing the data specified in the jsonString.
FromJSONSlice creates a new slice of Map containing the data specified in the jsonString.
FromSignedBase64 creates a new Obj containing the data specified in the Base64 string.
FromURLQuery generates a new Obj by parsing the specified query.
HashWithKey hashes the specified string using the security key.
MSI creates a map[string]interface{} and puts it inside a new Map.
MustFromBase64 creates a new Obj containing the data specified in the Base64 string and panics if there is an error.
MustFromJSON creates a new Map containing the data specified in the jsonString.
MustFromJSONSlice creates a new slice of Map containing the data specified in the jsonString.
MustFromSignedBase64 creates a new Obj containing the data specified in the Base64 string and panics if there is an error.
MustFromURLQuery generates a new Obj by parsing the specified query.
New creates a new Map containing the map[string]interface{} in the data argument.
SetURLValuesSliceKeySuffix sets the character that is used to specify a suffix for slices parsed by URLValues.

# Constants

PathSeparator is the character used to separate the elements of the keypath.
SignatureSeparator is the character that is used to separate the Base64 string from the security signature.
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

Nil represents a nil Map.

# Structs

Value provides methods for extracting interface{} data in various types.

# Interfaces

MSIConvertable is an interface that defines methods for converting your custom types to a map[string]interface{} representation.

# Type aliases

Map provides extended functionality for working with untyped data, in particular map[string]interface (msi).