gomap

package module
v0.0.0-...-b9d0d62 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

gomap

Go library for manipulating generic maps and cast elements into native Go types and native Go structures

gomap is super simple to use.

Start from a generic map, creating by hand or using some decoding golang tools

m := map[string]interface{}{
    "v1": 1,
    "elt": map[string]interface{}{
        "v2": 2,
    },
    "vs": "test",
}

Cast it into a GMap

gm := gomap.GMap(m)

And that's it, you can start to play

vi, err := gm.Get("elt", "v2").Int(42) //42 is the default value

When a default value is specified, errors are never raised, if you want to have path and types checks do this instead

vi, err := gm.Get("elt", "v2").Int() //no default value

gomap is also capable to set a custom struct using json annotations

type Elt struct{
    v2 int `json:"v2,omitempty"`
}
elt := Elt{}
err := gm.Get("elt").Object(&elt)

and empty path is ok

type Elt2 struct {
    V1  int    `json:"v1,omitempty"`
    Elt Elt    `json:"elt,omitempty"`
    VS  string `json:"vs,omitempty"`
}
elt2 := Elt2{}
err := gm.Get().Object(&elt2)

You can also play with slices

gm = GMap{//shortcut to define a GMap
    "v1": []interface{}{1, 2},
    "elt": GMap{
        "v2": []interface{}{int64(2), int64(3)},
    },
    "vs": []interface{}{"test", "test2"},
}
l, err := gm.Get("v2").Int64Slice()

See gomap_test.go for more examples

Documentation

Overview

Package gomap implements utilities to manipulate generic maps and cast elements into native Go types and native Go structures

The package is really simple to use

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {
	Path  []string
	Value interface{}
}

Element an element of the map

func (*Element) Bool

func (elt *Element) Bool(defaultValue ...bool) (bool, error)

Bool cast element value into bool

func (*Element) BoolSlice

func (elt *Element) BoolSlice(defaultValue ...[]bool) ([]bool, error)

BoolSlice cast element value into bool

func (*Element) Float32

func (elt *Element) Float32(defaultValue ...float32) (float32, error)

Float32 cast element value into float32

func (*Element) Float32Slice

func (elt *Element) Float32Slice(defaultValue ...[]float32) ([]float32, error)

Float32Slice cast element value into float32

func (*Element) Float64

func (elt *Element) Float64(defaultValue ...float64) (float64, error)

Float64 cast element value into float64

func (*Element) Float64Slice

func (elt *Element) Float64Slice(defaultValue ...[]float64) ([]float64, error)

Float64Slice cast element value into float64

func (*Element) Get

func (elt *Element) Get(path ...string) *Element

Get returns map elements along path

func (*Element) Int

func (elt *Element) Int(defaultValue ...int) (int, error)

Int cast element value into int

func (*Element) Int16

func (elt *Element) Int16(defaultValue ...int16) (int16, error)

Int16 cast element value into int16

func (*Element) Int16Slice

func (elt *Element) Int16Slice(defaultValue ...[]int16) ([]int16, error)

Int16Slice cast element value into int16

func (*Element) Int32

func (elt *Element) Int32(defaultValue ...int32) (int32, error)

Int32 cast element value into int32

func (*Element) Int32Slice

func (elt *Element) Int32Slice(defaultValue ...[]int32) ([]int32, error)

Int32Slice cast element value into int32

func (*Element) Int64

func (elt *Element) Int64(defaultValue ...int64) (int64, error)

Int64 cast element value into int64

func (*Element) Int64Slice

func (elt *Element) Int64Slice(defaultValue ...[]int64) ([]int64, error)

Int64Slice cast element value into int64

func (*Element) Int8

func (elt *Element) Int8(defaultValue ...int8) (int8, error)

Int8 cast element value into int8

func (*Element) Int8Slice

func (elt *Element) Int8Slice(defaultValue ...[]int8) ([]int8, error)

Int8Slice cast element value into int8

func (*Element) IntSlice

func (elt *Element) IntSlice(defaultValue ...[]int) ([]int, error)

IntSlice cast element value into int

func (*Element) Object

func (elt *Element) Object(obj interface{}) error

Object read element as struct

func (*Element) String

func (elt *Element) String(defaultValue ...string) (string, error)

String cast element value into string

func (*Element) StringSlice

func (elt *Element) StringSlice(defaultValue ...[]string) ([]string, error)

StringSlice cast element value into string

func (*Element) Uint16

func (elt *Element) Uint16(defaultValue ...uint16) (uint16, error)

Uint16 cast element value into uint16

func (*Element) Uint16Slice

func (elt *Element) Uint16Slice(defaultValue ...[]uint16) ([]uint16, error)

Uint16Slice cast element value into uint16

func (*Element) Uint32

func (elt *Element) Uint32(defaultValue ...uint32) (uint32, error)

Uint32 cast element value into uint32

func (*Element) Uint32Slice

func (elt *Element) Uint32Slice(defaultValue ...[]uint32) ([]uint32, error)

Uint32Slice cast element value into uint32

func (*Element) Uint64

func (elt *Element) Uint64(defaultValue ...uint64) (uint64, error)

Uint64 cast element value into uint64

func (*Element) Uint64Slice

func (elt *Element) Uint64Slice(defaultValue ...[]uint64) ([]uint64, error)

Uint64Slice cast element value into uint64

func (*Element) Uint8

func (elt *Element) Uint8(defaultValue ...uint8) (uint8, error)

Uint8 cast element value into uint8

func (*Element) Uint8Slice

func (elt *Element) Uint8Slice(defaultValue ...[]uint8) ([]uint8, error)

Uint8Slice cast element value into uint8

type GMap

type GMap map[string]interface{}

GMap overload map with utility functions

func (GMap) FromJSON

func (m GMap) FromJSON(content []byte) error

FromJSON laod json content into a GMap

func (GMap) Get

func (m GMap) Get(path ...string) *Element

Get returns map elements along path

func (GMap) ToJSON

func (m GMap) ToJSON() ([]byte, error)

ToJSON marshal a gmap into json content

type GSlice

type GSlice []interface{}

GSlice overload []interface{} to simplify GMap declarations

type WrongPathError

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

WrongPathError raised when the path is not correct

func NewWrongPathError

func NewWrongPathError(path []string) *WrongPathError

NewWrongPathError creates a new WrongPathError

func (*WrongPathError) Error

func (e *WrongPathError) Error() string

type WrongTypeError

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

WrongTypeError raised when the path is not correct

func NewWrongTypeError

func NewWrongTypeError(expected string, value interface{}) *WrongTypeError

NewWrongTypeError creates a new WrongPathError

func (*WrongTypeError) Error

func (e *WrongTypeError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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