ggja

package module
v0.11.3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 4 Imported by: 9

README

ggja

Test Coverage Go Report Card GoDoc

import "git.fractalqb.de/fractalqb/ggja"


Intro

Go Generic JSON Api is a small helper library to make working with Go's generic JSON types more comfortable.

Instead of writing this code fragment

if tmp, ok := genJson["key"]; !ok {
	log.Panic("no member key in JSON object")
} else {
	i := tmp.(int)
}

with ggja it can be somewhat shorter:

i := ggjaObj.MInt("key")

OK, one first has to wrap the generic JSON genJson map[string]interface{} into a ggja object wrapper:

ggjaObj := ggja.Obj{Bare: genJson}
i := ggjaObj.MInt("key")          // Must Int or…
j := ggjaObj.Int("key", 4711)     // with default value

Perhaps you don't like to simply panic and customize the error handling:

ggjaObj.OnError = func(err error) { log.Fatal(err) }

But this is only done once and—also useful—you can pass the error handling strategy around with the ggjaObj.

For more details read on or consult godoc.

JSON Array Element by Index

Simply by index (that's it for now):

strVal := ggjaArr.Str(3, strVal)

Updates strVal in case ggjaArr has at least four elements.

Access Methods for Basic Types

Access methods for basic types come in two flavours:

  1. A conditional one, that returns a default value nvl if the requested value is not in the JSON object or array. Those methods are simply called after the type they return, e.g. ggjaObj.Str("name", "-") returns a string.

  2. A mandatory flavour that calls your provided OnError function if the requested value is not preset. If you provide no error function it will panic instead. Mandatory access methods start with 'M', e.g. MInt(…)

Access Methods for Array and Objects

Besides the access methods for basic types there are also access methods for objects and arrays. Both come in the two flavours conditional and mandatory. The conditional access for arrays and objects does not have a default value but returns nil in case the is nothing that can be accessed.

However array element and object member access have a third generating flavour that creates an empty array resp. object on access if needed. Generating access methods start with 'C' for create, e.g. CObj(…).

ggjaArr := Arr{OnError: fail}
objMbr3 := ggjaArr.CObj(3)
objMbr3.Put("name", "John Doe")
jStr, _ := json.Marshal(ggjaArr.Bare)
fmt.Println(string(jStr))
jStr, _ = json.Marshal(objMbr3.Bare)
fmt.Println(string(jStr))

writes output

[null,null,null,{"name":"John Doe"}]
{"name":"John Doe"}

Documentation

Overview

Package ggja—the Go Generic JSON Api—is a small helper library to make working with Go's generic JSON types more comfortable.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get added in v0.10.1

func Get(v interface{}, path ...interface{}) (interface{}, error)
Example
package main

import (
	"fmt"
	"math"
	"testing"
)

func printError(err error) {
	fmt.Printf("ERROR: %s\n", err)
}

type onErr struct {
	t *testing.T
}

func (oe onErr) error(err error) {
	oe.t.Error(err)
}

func (oe onErr) fatal(err error) {
	oe.t.Fatal(err)
}

func main() {
	data := BareObj{
		"foo": "bar",
		"baz": BareArr{
			true, false,
			BareObj{"OK": math.Pi},
			"end",
		},
	}
	fmt.Println(Get(data, "baz", -2))
}
Output:

map[OK:3.141592653589793] <nil>

func GetString added in v0.10.1

func GetString(v interface{}, path ...interface{}) (string, error)

func Recover added in v0.11.0

func Recover(err *error)

Types

type Arr

type Arr struct {
	Bare BareArr
	// When OnError is nil all methods will panic on error, otherwise
	// OnError(err) will be called. 'M*' methods returing values will return
	// zero value on error if OnError has no non-local exists. Non-'M*' methods
	// retrun the nvl value.
	OnError func(error)
}

func (*Arr) Arr

func (a *Arr) Arr(idx int) *Arr

func (*Arr) Bool

func (a *Arr) Bool(idx int, nvl bool) bool

func (*Arr) CArr

func (a *Arr) CArr(idx int) (elm *Arr)

func (*Arr) CObj

func (a *Arr) CObj(idx int) (elm *Obj)
Example
ggjaArr := Arr{OnError: printError}
objMbr3 := ggjaArr.CObj(3)
objMbr3.Put("name", "John Doe")
jStr, _ := json.Marshal(ggjaArr.Bare)
fmt.Println(string(jStr))
jStr, _ = json.Marshal(objMbr3.Bare)
fmt.Println(string(jStr))
Output:

[null,null,null,{"name":"John Doe"}]
{"name":"John Doe"}

func (*Arr) F32 added in v0.11.0

func (a *Arr) F32(idx int, nvl float32) float32

func (*Arr) F64

func (a *Arr) F64(idx int, nvl float64) float64

func (*Arr) Ignoring added in v0.10.0

func (a *Arr) Ignoring(errs ...error) *Arr

func (*Arr) Int

func (a *Arr) Int(idx int, nvl int) int

func (*Arr) Len

func (a *Arr) Len() int

func (*Arr) MArr

func (a *Arr) MArr(idx int) *Arr

func (*Arr) MBool

func (a *Arr) MBool(idx int) bool

func (*Arr) MF32 added in v0.8.1

func (a *Arr) MF32(idx int) float32

TODO missleading error messages

func (*Arr) MF64

func (a *Arr) MF64(idx int) float64

func (*Arr) MInt

func (a *Arr) MInt(idx int) int

func (*Arr) MObj

func (a *Arr) MObj(idx int) *Obj

func (*Arr) MStr

func (a *Arr) MStr(idx int) string

func (*Arr) MUint32

func (a *Arr) MUint32(idx int) uint32

func (*Arr) Obj

func (a *Arr) Obj(idx int) *Obj

func (*Arr) Put

func (a *Arr) Put(idx int, v interface{}) *Arr

func (*Arr) Set

func (a *Arr) Set(idx int, v interface{}) *Arr

func (*Arr) Str

func (a *Arr) Str(idx int, nvl string) string

func (*Arr) Uint32

func (a *Arr) Uint32(idx int, nvl uint32) uint32

type BareArr added in v0.10.0

type BareArr = []interface{}

type BareObj added in v0.10.0

type BareObj = map[string]interface{}

type IdxRangeError added in v0.10.0

type IdxRangeError struct {
	Begin, End int
}

func (IdxRangeError) Error added in v0.10.0

func (eir IdxRangeError) Error() string

type IgnoreErrors added in v0.10.0

type IgnoreErrors struct {
	Ignore []error
	Else   func(error)
}

func (IgnoreErrors) Error added in v0.10.0

func (ign IgnoreErrors) Error(err error)

type IndexError added in v0.10.0

type IndexError struct {
	Idx, EIdx int
	// contains filtered or unexported fields
}

func (IndexError) Error added in v0.10.0

func (ee IndexError) Error() string

func (IndexError) Unwrap added in v0.10.0

func (ee IndexError) Unwrap() error

type KeyError added in v0.10.0

type KeyError struct {
	Key string
	// contains filtered or unexported fields
}

func (KeyError) Error added in v0.10.0

func (me KeyError) Error() string

func (KeyError) Unwrap added in v0.10.0

func (me KeyError) Unwrap() error

type NoConversionError added in v0.10.1

type NoConversionError struct {
	Value  interface{}
	Target string
}

func (NoConversionError) Error added in v0.10.1

func (nce NoConversionError) Error() string

type Obj

type Obj struct {
	Bare BareObj
	// When OnError is nil all methods will panic on error, otherwise
	// OnError(err) will be called. 'M*' methods returing values will return
	// zero value on error if OnError has no non-local exists. Non-'M*' methods
	// retrun the nvl value.
	OnError func(error)
}

func GetObj added in v0.10.1

func GetObj(v interface{}, path ...interface{}) (*Obj, error)

func (*Obj) Arr

func (o *Obj) Arr(key string) *Arr

func (*Obj) Bool

func (o *Obj) Bool(key string, nvl bool) bool

func (*Obj) CArr

func (o *Obj) CArr(key string) (sub *Arr)

func (*Obj) CObj

func (o *Obj) CObj(key string) (sub *Obj)

func (*Obj) F32

func (o *Obj) F32(key string, nvl float32) float32

func (*Obj) F64

func (o *Obj) F64(key string, nvl float64) float64

func (*Obj) Ignoring added in v0.10.0

func (o *Obj) Ignoring(errs ...error) *Obj

func (*Obj) Int

func (o *Obj) Int(key string, nvl int) int

func (*Obj) Int16 added in v0.11.2

func (o *Obj) Int16(key string, nvl int16) int16

func (*Obj) Int32 added in v0.10.0

func (o *Obj) Int32(key string, nvl int32) int32

func (*Obj) Int64

func (o *Obj) Int64(key string, nvl int64) int64

func (*Obj) Int8 added in v0.11.2

func (o *Obj) Int8(key string, nvl int8) int8

func (*Obj) MArr

func (o *Obj) MArr(key string) *Arr

func (*Obj) MBool

func (o *Obj) MBool(key string) bool

func (*Obj) MF32

func (o *Obj) MF32(key string) float32

func (*Obj) MF64

func (o *Obj) MF64(key string) float64

func (*Obj) MInt

func (o *Obj) MInt(key string) int

func (*Obj) MInt16 added in v0.10.1

func (o *Obj) MInt16(key string) int16

func (*Obj) MInt32 added in v0.8.0

func (o *Obj) MInt32(key string) int32

func (*Obj) MInt64

func (o *Obj) MInt64(key string) int64

func (*Obj) MInt8 added in v0.11.2

func (o *Obj) MInt8(key string) int8

func (*Obj) MObj

func (o *Obj) MObj(key string) (sub *Obj)

func (*Obj) MStr

func (o *Obj) MStr(key string) string

func (*Obj) MTime

func (o *Obj) MTime(key string) time.Time

func (*Obj) MUint16 added in v0.11.2

func (o *Obj) MUint16(key string) uint16

func (*Obj) MUint32

func (o *Obj) MUint32(key string) uint32

func (*Obj) MUint64 added in v0.8.0

func (o *Obj) MUint64(key string) uint64

func (*Obj) MUint8 added in v0.11.2

func (o *Obj) MUint8(key string) uint8

func (*Obj) Obj

func (o *Obj) Obj(key string) (sub *Obj)

func (*Obj) Put

func (o *Obj) Put(key string, v interface{}) *Obj
Example
jBar := Obj{OnError: printError}
jBar.Put("foo", 4711)
jBar.CObj("baz").Put("quux", true)
err := json.NewEncoder(os.Stdout).Encode(jBar.Bare)
if err != nil {
	fmt.Println(err)
}
Output:

{"baz":{"quux":true},"foo":4711}

func (*Obj) Set

func (o *Obj) Set(key string, v interface{}) *Obj

func (*Obj) Str

func (o *Obj) Str(key, nvl string) string

func (*Obj) Time

func (o *Obj) Time(key string, nvl time.Time) time.Time

func (*Obj) Uint16 added in v0.11.2

func (o *Obj) Uint16(key string, nvl uint16) uint16

func (*Obj) Uint32

func (o *Obj) Uint32(key string, nvl uint32) uint32

func (*Obj) Uint64 added in v0.9.0

func (o *Obj) Uint64(key string, nvl uint64) uint64

func (*Obj) Uint8 added in v0.11.2

func (o *Obj) Uint8(key string, nvl uint8) uint8

type SetError added in v0.10.2

type SetError struct {
	Target *error
}

func (SetError) OnError added in v0.10.2

func (es SetError) OnError(err error)

type TypeMismatch added in v0.10.0

type TypeMismatch struct {
	Expect string
	Value  interface{}
}

func (TypeMismatch) Error added in v0.10.0

func (tmm TypeMismatch) Error() string

Jump to

Keyboard shortcuts

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