Help us shape the future of Go! Take the 2026 Go Developer Survey

gobag

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2020 License: MIT Imports: 2 Imported by: 0

README

gobag

Built with ❤️

Build Status Code Coverage Go Report Card GoDoc

A simple utility to read json response without any custom struct. Works for nested json using dot notation

Description

Tired of making custom structs for every API call you made? Especially when the response is nested? Well, this package simplify everything. For whatever structure your json response is you can simplify struct construction

type Person struct {
    Name string
    Birthdate string
    Height int
}

// somehow it nested somewhere deep in the json response
person := Person{
    Name: bag.GetString("profiles.0.name"),
    Birthdate: bag.GetString("profiles.0.birthdate"),
    Height: bag.GetInt("profiles.0.height")
}

Usage

Supposed you make an HTTP call and received a response like this

{
    "name": "Against The Current",
    "members": [
        "Chrissy Costanza",
        "Daniel Gow",
        "William Ferri"
    ],
    "formed_in": 2011,
    "profiles": [
        {
            "name": "Chrissy Costanza ❤️",
            "birthdate": "23 August 1995",
            "height": 155
        }
    ],
    "informations": {
        "labels": "Fueled by Ramen",
        "website": "atcofficial.com"
    },
    "years_of_active": [
        2011,
        2012,
        2013,
        2014,
        2015,
        2016,
        2017,
        2018,
        2019,
    ],
}

And in your code might look like this

resp, _ := http.Get("https://example.com/against-the-current")
body, _ := ioutil.ReadAll(resp.Body)

var data map[string]interface{}
_ = json.Unmarshal(body, &data)

bag := gobag.Bagify(data)

That will construct the Bag struct, now we can access everything

// simple get
name := bag.GetString("name") // Against The Current
formed_in := bag.GetInt("formed_in") // 2011

// accessing inside an array directly
firstElement := bag.GetString("members.0") // Chrissy Costanza

// array of string
var members []string
members = bag.GetArrayString("members") // ["Chrissy Costanza", "Daniel Gow", "William Ferri"]

// array of integer
var yearsOfActive []int
yearsOfActive = bag.GetArrayInt("years_of_active") // [2011, 2012, 2013, 2014, ...]

// wanna get a string inside a nested json?
website := bag.GetString("informations.website")

// or a deep nested object
height := bag.GetInt("profiles.0.height")

Error Handling
In the case that the json isn't in the type expected, it will returns its zero-value

// the key is missing
foo := bag.GetString("missing_key") // ""
bar := bag.GetInt("missing_key") // 0
baz := bag.GetArrayInt("missing_key") // []int{}

// or it's an integer not a string
qux := bag.GetString("formed_in") // ""

Why?

This package is particularly useful when you don't want to create a custom struct for every API call response.
Instead of creating a complex embedded struct of a response when you only need a few of the data here's an example, you wanna create an array of people struct

type Person struct {
    Name string
    Birthdate string
    Height int
}

func main() {
    bag := gobag.Bagify(jsonResponse)
    data, _ := bag.Get("data").([]interface{})

	var persons []Person
	for _, v := range data {
        // actually, you can bagify almost everything, including a json request on your handler
        b := gobag.Bagify(v)

		persons = append(persons, Person{
			Name:      b.GetString("name"),
			Birthdate: b.GetString("birthdate"),
			Height:    b.GetInt("height"),
		})
    }
    
    fmt.Println(persons)
}

Documentation

Overview

Package gobag provides a simple utility to read json response without any custom struct. Works for nested json using dot notation

A quick usage

supposed body is a json like this

{"name": "gobag", "amount": 1}

then

var data map[string]interface{}
_ = json.Unmarshal(body, &data)
bag := gobag.Bagify(data)
bag.GetString("name") // gobag
bag.GetInt("amount") // 1

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bag

type Bag struct {
	// contains filtered or unexported fields
}

Bag only contains contains filtered or unexported fields

func Bagify

func Bagify(data interface{}) Bag

Bagify constructs Bag by providing anything

func (Bag) Get

func (b Bag) Get(key string) interface{}

Get a value of any key including nested key by using dot notation

func (Bag) GetArray added in v1.2.0

func (b Bag) GetArray(key string) []interface{}

Get a value of any key including nested key by using dot notation

func (Bag) GetArrayFloat

func (b Bag) GetArrayFloat(key string) []float64

GetArrayFloat returns value in array of float64 defaults to 0 if any of the member is not float64

func (Bag) GetArrayInt

func (b Bag) GetArrayInt(key string) []int

GetArrayInt returns value in array of integer defaults to 0 if any of the member is not integer

func (Bag) GetArrayString

func (b Bag) GetArrayString(key string) []string

GetArrayString returns value in array of string defaults to "" if any of the member is not integer

func (Bag) GetBool

func (b Bag) GetBool(key string) bool

GetBool returns value in boolean

func (Bag) GetFloat

func (b Bag) GetFloat(key string) float64

GetFloat returns value in integer. Works for json numbers

func (Bag) GetInt

func (b Bag) GetInt(key string) int

GetInt returns value in integer. Works for json numbers

func (Bag) GetMapString added in v1.1.0

func (b Bag) GetMapString(key string) map[string]interface{}

GetMapString returns map[string]interface{}

func (Bag) GetString

func (b Bag) GetString(key string) string

GetString returns value in string

func (Bag) IsEmpty

func (b Bag) IsEmpty() bool

IsEmpty returns whether the data constructed is empty or not

Jump to

Keyboard shortcuts

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