jsutil

package
v0.0.0-...-e560ebb Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2021 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(data interface{}, pathElems ...interface{}) interface{}

Get is like GetError, except that if GetError would return an error, this function panics instead.

func GetError

func GetError(data interface{}, pathElems ...interface{}) (interface{}, error)

GetError retrieves a value from a 'jsonish' data object, given a path to follow.

data is assumed to be either a map[string]interface{}, or a []interface{}.

pathElems should either be strings or int's. Any other type will cause a panic. So don't do it.

A pathElem of a string implies that GetError should expect a map at that location in the jsonish data. An int implies that it should expect a list. If this expectation is false, an error is returned.

If you attempt to index into a list such that the index is out of bounds, you'll get a panic just like if you passed an index to a slice that was out of bounds.

Accessing a map key which doesn't exist will return nil.

Example:

data = {
  "some": [
    {"nested": {"value": 10}}
  ]
}

GetError(data, "some") #=> [{"nested":...}]
GetError(data, "some", 0) #=> {"nested":...}
GetError(data, "some", 0, "nested") #=> {"value": 10}
GetError(data, "some", 0, "nested", "value") #=> 10
GetError(data, "wat") #=> nil
GetError(data, "wat", "something") #=> panic(nil deref)
GetError(data, "some", 1) #=> panic(out of bounds)

Types

This section is empty.

Jump to

Keyboard shortcuts

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