• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H09-Apr-2019-5733

codegen/H03-May-2022-

vendor/github.com/H03-May-2022-8,3284,914

.codeclimate.ymlH A D09-Apr-2019291 2220

.gitignoreH A D09-Apr-2019185 129

.travis.ymlH A D09-Apr-2019656 3124

LICENSEH A D09-Apr-20191.1 KiB2318

README.mdH A D09-Apr-20193.5 KiB8152

Taskfile.ymlH A D09-Apr-2019463 3124

accessors.goH A D09-Apr-20193.1 KiB12067

accessors_test.goH A D09-Apr-20194 KiB192151

conversions.goH A D09-Apr-20196.9 KiB281205

conversions_test.goH A D09-Apr-20195.3 KiB186144

doc.goH A D09-Apr-20192.1 KiB671

fixture_test.goH A D09-Apr-20192.2 KiB9783

go.modH A D09-Apr-2019137 96

go.sumH A D09-Apr-2019697 98

map.goH A D09-Apr-20195.7 KiB229146

map_test.goH A D09-Apr-20194.8 KiB228180

mutations.goH A D09-Apr-20192 KiB7850

mutations_test.goH A D09-Apr-20192 KiB10788

security.goH A D09-Apr-2019243 139

security_test.goH A D09-Apr-2019241 139

simple_example_test.goH A D09-Apr-20191.1 KiB4322

tests.goH A D09-Apr-2019362 1810

tests_test.goH A D09-Apr-2019476 2618

type_specific.goH A D09-Apr-20199 KiB347253

type_specific_codegen.goH A D09-Apr-201962 KiB2,2521,477

type_specific_codegen_test.goH A D09-Apr-201964.7 KiB2,1881,699

type_specific_test.goH A D09-Apr-201919.5 KiB460364

value.goH A D09-Apr-20194 KiB160147

value_test.goH A D09-Apr-20193.5 KiB144117

README.md

1# Objx
2[![Build Status](https://travis-ci.org/stretchr/objx.svg?branch=master)](https://travis-ci.org/stretchr/objx)
3[![Go Report Card](https://goreportcard.com/badge/github.com/stretchr/objx)](https://goreportcard.com/report/github.com/stretchr/objx)
4[![Maintainability](https://api.codeclimate.com/v1/badges/1d64bc6c8474c2074f2b/maintainability)](https://codeclimate.com/github/stretchr/objx/maintainability)
5[![Test Coverage](https://api.codeclimate.com/v1/badges/1d64bc6c8474c2074f2b/test_coverage)](https://codeclimate.com/github/stretchr/objx/test_coverage)
6[![Sourcegraph](https://sourcegraph.com/github.com/stretchr/objx/-/badge.svg)](https://sourcegraph.com/github.com/stretchr/objx)
7[![GoDoc](https://godoc.org/github.com/stretchr/objx?status.svg)](https://godoc.org/github.com/stretchr/objx)
8
9Objx - Go package for dealing with maps, slices, JSON and other data.
10
11Get started:
12
13- Install Objx with [one line of code](#installation), or [update it with another](#staying-up-to-date)
14- Check out the API Documentation http://godoc.org/github.com/stretchr/objx
15
16## Overview
17Objx 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.
18
19### Pattern
20Objx 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:
21
22    m, err := objx.FromJSON(json)
23
24NOTE: 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.
25
26Use `Get` to access the value you're interested in.  You can use dot and array
27notation too:
28
29     m.Get("places[0].latlng")
30
31Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type.
32
33     if m.Get("code").IsStr() { // Your code... }
34
35Or you can just assume the type, and use one of the strong type methods to extract the real value:
36
37    m.Get("code").Int()
38
39If 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.
40
41     Get("code").Int(-1)
42
43If 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.
44
45### Reading data
46A simple example of how to use Objx:
47
48    // Use MustFromJSON to make an objx.Map from some JSON
49    m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
50
51    // Get the details
52    name := m.Get("name").Str()
53    age := m.Get("age").Int()
54
55    // Get their nickname (or use their name if they don't have one)
56    nickname := m.Get("nickname").Str(name)
57
58### Ranging
59Since `objx.Map` is a `map[string]interface{}` you can treat it as such.  For example, to `range` the data, do what you would expect:
60
61    m := objx.MustFromJSON(json)
62    for key, value := range m {
63      // Your code...
64    }
65
66## Installation
67To install Objx, use go get:
68
69    go get github.com/stretchr/objx
70
71### Staying up to date
72To update Objx to the latest version, run:
73
74    go get -u github.com/stretchr/objx
75
76### Supported go versions
77We support the lastest three major Go versions, which are 1.10, 1.11 and 1.12 at the moment.
78
79## Contributing
80Please feel free to submit issues, fork the repository and send pull requests!
81