Git Product home page Git Product logo

dig's Introduction

gosexy/dig

This package provides sugar methods for traversing Go maps.

gosexy/dig basically turns this long statement:

foo := myMap["a"].(map[string]interface{})["b"].(map[string]interface{})["c"].(uint64)

Into this short and easier-to-understand one:

foo := dig.Uint64(myMap, "a", "b", "c")

Useful when you depend on external services, such as web-APIs, and you don't really want to handle all possible error cases or possible panic points caused by sudden changes on the expected JSON.

Installation

go get -u menteslibres.net/gosexy/dig

Usage

Usage is really simple, start with a map[string]interface{} and define which route to follow, take this testMap example:

testMap := map[string]interface{}{
	"first": map[string]interface{}{
		"first.1": map[string]string{
			"col.1": "a",
			"col.2": "b",
			"col.3": "c",
		},
	},
	"second": map[string]map[string]string{
		"second.2": map[string]string{
			"col.4": "d",
			"col.5": "e",
			"col.6": "f",
		},
	},
}

Suppose you want to get the value of the element with key "col.4". You'll need a route, the route of a node is defined as the array of map keys needed to be traversed in order to get to the node, in the above example the route would be: second > second.2 > col.4, in that specific order.

Now suppose you expect "col.4" to be a string, then you'd use dig.String() to get the value.

// s = "d"
s := dig.String(&testMap, "second", "second.2", "col.4")

Note that you're passing a pointer to map (&testMap) instead of a map.

There are more sugar methods with similar functionality, such as dig.Uint64(), dig.Float64(), dig.Interface(), etc. all these methods will attempt to convert the node data type into the expected Go type.

The dig.String() function and friends are just wrappers around dig.Get(), and you can also use dig.Get() to get a node value, it will also produce an error value if something goes wrong.

var s string
err := dig.Get(&testMap, &s, "second", "second.2", "col.4")

Now suppose you'd like to set the value of "col.5", you'd use the dig.Set() function:

dig.Set(&testMap, "modified", "second", "second.2", "col.4")

And finally, if you're ever in the need of creating a route on a map, you could use dig.Dig() like this:

var i int

m := map[string]interface{}{}

// Create a path.
dig.Dig(&m, "foo", "bar", "baz")

// Set the value 42.
dig.Set(&m, 42, "foo", "bar", "baz")

// Get the value, expecting 42.
dig.Get(&m, &i, "foo", "bar", "baz")

if i != 42 {
	t.Errorf("Test failed.")
}

Please consult the online documentation to get a list of all available methods.

Documentation

See the online docs and the project page.

License

Copyright (c) 2013 José Carlos Nieto, https://menteslibres.net/xiam

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

dig's People

Contributors

esilva-everbridge avatar xiam avatar

Watchers

James Cloos avatar

Forkers

edlitmus

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.