Package whatever is a Go package that exports type Params with some useful methods on top of map[string]interface{}
import (
"fmt"
"github.com/ndyakov/whatever"
)
var body = []byte(`
{
"int": -10,
"string": "test",
"time": "2015-02-20T21:22:23.24Z",
"nestedParams": {
"arrayStrings": ["one","two","three"],
"arrayInts": [1,2,3,4],
}
}
`)
func main() {
p := whatever.NewFromJSON(body)
fmt.Println(p.GetInt("int")) // -10
fmt.Println(p.GetP("nestedParams").GetSliceStrings("arrayStrings")[1]) // two
}
func sum(p whatever.Params) int {
x := p.GetInt("x")
// or x := p.GetI("x").(int)
y := p.GetInt("y")
// or y := p.GetI("y").(int)
return x + y
}
func main() {
p := whatever.Params{x: 10, y: 5}
fmt.Println(sum(p)) // 15
}
go get github.com/ndyakov/whatever
import "github.com/ndyakov/whatever"
The main idea behind whatever was for the type Params to be
used when unmarshaling JSON data from an application/json
request,
but after adding the Add
and Remove
methods I think that this Params
type can be useful in variety of other cases as well.
There is a method that can transform the Params structure to
url.Values structure with specified prefix and suffix, for the
result can be used with Gorillas schema or Goji
s params packages.
Although some of the getters are useful for unmarshaled JSON
date you can also Add your own values to the Params structure.
You can also access nested Params objects.
If you need you can validate the existence of a specific key by
using the Required method.
This may be outdated, please check the godoc for up-to-date documentation
- func NewFromJSON(jsonBody []byte) (Params, error)
- func (p Params) Add(key string, value interface{}) bool
- func (p Params) Empty() bool
- func (p Params) Get(key string) string
- func (p Params) GetFloat(key string) float32
- func (p Params) GetFloat32(key string) float32
- func (p Params) GetFloat64(key string) float64
- func (p Params) GetI(key string) interface{}
- func (p Params) GetInt(key string) int
- func (p Params) GetInt64(key string) int64
- func (p Params) GetInt8(key string) int8
- func (p Params) GetP(key string) Params
- func (p Params) GetSlice(key string) []interface{}
- func (p Params) GetSliceInts(key string) []int
- func (p Params) GetSliceStrings(key string) []string
- func (p Params) GetString(key string) string
- func (p Params) GetTime(key string) time.Time
- func (p Params) Remove(key string)
- func (p Params) Required(keys ...string) error
- func (p Params) URLValues(prefix, suffix string) url.Values
Before contributing please execute:
- gofmt
- golint
- govet
The MIT License (MIT)
Copyright (c) 2015 Nedyalko Dyakov
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.