Skip to content
Go package for dealing with maps, slices, JSON and other data.
Branch: master
Clone or download
Latest commit 35313a9 Apr 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Add PULL_REQUEST_TEMPLATE.md (#77) May 31, 2018
codegen Update types_list.txt May 28, 2018
vendor Move to go modules (#91) Apr 9, 2019
.codeclimate.yml
.gitignore Upload test coverage to codeclimate (#39) Jan 25, 2018
.travis.yml Move to go modules (#91) Apr 9, 2019
LICENSE Update LICENSE (#28) Jan 5, 2018
README.md Move to go modules (#91) Apr 9, 2019
Taskfile.yml Move to go modules (#91) Apr 9, 2019
accessors.go Fixes #88 (#95) Apr 15, 2019
accessors_test.go Fixes #88 (#95) Apr 15, 2019
conversions.go Fix for map[interface{}]interface{} to JSON conversion (#87) Feb 11, 2019
conversions_test.go Check more errors in tests (#94) Apr 15, 2019
doc.go Update readme (#31) Jan 5, 2018
fixture_test.go Refactor tests (#46) Jan 25, 2018
go.mod
go.sum Move to go modules (#91) Apr 9, 2019
map.go
map_test.go Fix indentation (#69) May 27, 2018
mutations.go Refactor tests (#46) Jan 25, 2018
mutations_test.go Refactor tests (#46) Jan 25, 2018
security.go Refactor tests (#46) Jan 25, 2018
security_test.go Refactor tests (#46) Jan 25, 2018
simple_example_test.go Refactor tests (#46) Jan 25, 2018
tests.go Make Map an MSI Sep 17, 2013
tests_test.go Refactor tests (#46) Jan 25, 2018
type_specific.go Fix for #7 (#85) Feb 4, 2019
type_specific_codegen.go Move type Map out of type_specific_codegen.go (#73) May 28, 2018
type_specific_codegen_test.go Move type Map out of type_specific_codegen.go (#73) May 28, 2018
type_specific_test.go Fix for #7 (#85) Feb 4, 2019
value.go Nil value returns empty string. (#76) May 31, 2018
value_test.go

README.md

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 preditable 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 support the lastest three major Go versions, which are 1.10, 1.11 and 1.12 at the moment.

Contributing

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

You can’t perform that action at this time.