• Stars
    star
    695
  • Rank 65,073 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 11 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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!

More Repositories

1

testify

A toolkit with common assertions and mocks that plays nicely with the standard library
Go
23,256
star
2

arg.js

Lightweight URL argument and parameter parser
JavaScript
1,060
star
3

gomniauth

Authentication framework for Go applications.
Go
637
star
4

goweb

A lightweight RESTful web framework for Go
Go
632
star
5

over.js

Elegant function overloading in JavaScript.
JavaScript
147
star
6

gorc

Recursive go testing, done better.
Go
141
star
7

powerwalk

Package for concurrently walking files
Go
104
star
8

stew

Stew is a very high performance package that extends common Go objects providing better alternatives or wrappers.
Go
67
star
9

hoard

A fast, smart caching package for Go
Go
59
star
10

github-stars

Returns the aggregate number of stars for a user/org or single repo
JavaScript
52
star
11

signature

URL signing package for Go
Go
38
star
12

codecs

Provides interfaces, functions and codecs that can be used to encode/decode data to/from various formats.
Go
31
star
13

filetypes.js

A complete list of file types, extensions and mime types in JavaScript.
JavaScript
18
star
14

pat

Go package for patterns and best practices
Go
15
star
15

readcaster

Go package for elegantly broadcasting one io.Reader source to many io.Readers
Go
14
star
16

commander

A Go package that makes it easy to create a command line interface.
Go
12
star
17

todo

A Go package that helps you remember the DO in TODO
Go
12
star
18

ottox

Plugins for the Otto Go JavaScript parser and interpreter
Go
12
star
19

thru

The worlds simplest web server
Go
11
star
20

slog

Logging package for Go
Go
10
star
21

jsonblend

A command line tool providing extensive capabilities for combining json objects.
Go
9
star
22

piglatin

English -> Pig Latin translator package for Go - TDD tutorial code
Go
9
star
23

respond

Go package for building data APIs
Go
7
star
24

sdk-go

Go SDK for Stretchr platform
Go
7
star
25

tracer

A quick and dirty tracing package for Go
Go
5
star
26

pools

pools is a go package for managing a suite of differently sized slices of objects backed by sync.Pool
5
star
27

bits

A simple bit manipulation package for Go.
Go
4
star
28

tdd-present

TDD Presentation code
Go
4
star
29

sdk-cocoa

Cocoa and iOS SDK for the Stretchr Platform
Objective-C
3
star
30

fanjs

JavaScript code for fanning elements in a circular pattern
JavaScript
3
star
31

sdk-ruby

Ruby SDK for the Stretchr Platform
Ruby
2
star
32

excerpt-search

Returns a highlighted excerpt from a block of text based on search terms
JavaScript
2
star
33

stretchr.github.io

The Stretchr.com homepage
JavaScript
2
star
34

jbaas

jsonblend as a service.
Go
2
star
35

sdk-js

HTML5 and JavaScript SDK for Stretchr
JavaScript
2
star
36

jsonblend-website

jsonblend website
JavaScript
2
star
37

config

Package that provides easy configuration management for Go programs
Go
2
star
38

sandiego

Repo for San Diego crime data workshop projects
2
star
39

cmds

Elegant command line and external tool management package for Go
Go
2
star
40

issues

Bug tracking for Stretchr platform
1
star
41

TagSpot

iOS application and HTML page showing example integrations into Stretchr
Objective-C
1
star
42

stretchr-backbone

Backbone.js adapter for Stretchr
JavaScript
1
star