dproxy

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2019 License: MIT Imports: 5 Imported by: 25

README

dProxy - document proxy

GoDoc CircleCI Go Report Card

dProxy is a proxy to access interface{} (document) by simple query. It is intented to be used with json.Unmarshal() or json.NewDecorder().

See codes for overview.

import (
  "encoding/json"

  "github.com/koron/go-dproxy"
)

var v interface{}
json.Unmarshal([]byte(`{
  "cities": [ "tokyo", 100, "osaka", 200, "hakata", 300 ],
  "data": {
    "custom": [ "male", 23, "female", 24 ]
  }
}`), &v)

// s == "tokyo", got a string.
s, _ := dproxy.New(v).M("cities").A(0).String()

// err != nil, type not matched.
_, err := dproxy.New(v).M("cities").A(0).Float64()

// n == 200, got a float64
n, _ := dproxy.New(v).M("cities").A(3).Float64()

// can be chained.
dproxy.New(v).M("data").M("custom").A(0).String()

// err.Error() == "not found: data.kustom", wrong query can be verified.
_, err = dproxy.New(v).M("data").M("kustom").String()

Getting started

Proxy
  1. Wrap a value (interface{}) with dproxy.New() get dproxy.Proxy.

    p := dproxy.New(v) // v should be a value of interface{}
    
  2. Query as a map (map[string]interface{})by M(), returns dproxy.Proxy.

    p.M("cities")
    
  3. Query as an array ([]interface{}) with A(), returns dproxy.Proxy.

    p.A(3)
    
  4. Therefore, can be chained queries.

    p.M("cities").A(3)
    
  5. Get a value finally.

    n, _ := p.M("cities").A(3).Int64()
    
  6. You'll get an error when getting a value, if there were some mistakes.

    // OOPS! "kustom" is typo, must be "custom"
    _, err := p.M("data").M("kustom").A(3).Int64()
    
    // "not found: data.kustom"
    fmt.Println(err)
    
  7. If you tried to get a value as different type, get an error.

    // OOPS! "cities[3]" (=200) should be float64 or int64.
    _, err := p.M("cities").A(3).String()
    
    // "not matched types: expected=string actual=float64: cities[3]"
    fmt.Println(err)
    
  8. You can verify queries easily.

Drain

Getting value and error from Proxy/ProxySet multiple times, is very awful. It must check error when every getting values.

p := dproxy.New(v)
v1, err := p.M("cities").A(3).Int64()
if err != nil {
    return err
}
v2, err := p.M("data").M("kustom").A(3).Int64()
if err != nil {
    return err
}
v3, err := p.M("cities").A(3).String()
if err != nil {
    return err
}

It can be rewrite as simple like below with dproxy.Drain

var d Drain
p := dproxy.New(v)
v1 := d.Int64(p.M("cities").A(3))
v2 := d.Int64(p.M("data").M("kustom").A(3))
v3 := d.String(p.M("cities").A(3))
if err := d.CombineErrors(); err != nil {
    return err
}
JSON Pointer

JSON Pointer can be used to query interface{}

v1, err := dproxy.New(v).P("/cities/0").Int64()

or

v1, err := dproxy.Pointer(v, "/cities/0").Int64()

See RFC6901 for details of JSON Pointer.

LICENSE

MIT license. See LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Drain added in v1.2.0

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

Drain stores errors from Proxy or ProxySet.

func (*Drain) All added in v1.2.0

func (d *Drain) All() []error

All returns all errors which stored. Return nil if no errors stored.

func (*Drain) Array added in v1.2.0

func (d *Drain) Array(p Proxy) []interface{}

Array returns []interface{} value and stores an error.

func (*Drain) ArrayArray added in v1.2.0

func (d *Drain) ArrayArray(ps ProxySet) [][]interface{}

ArrayArray returns [][]interface{} value and stores an error.

func (*Drain) Bool added in v1.2.0

func (d *Drain) Bool(p Proxy) bool

Bool returns bool value and stores an error.

func (*Drain) BoolArray added in v1.2.0

func (d *Drain) BoolArray(ps ProxySet) []bool

BoolArray returns []bool value and stores an error.

func (*Drain) CombineErrors added in v1.2.0

func (d *Drain) CombineErrors() error

CombineErrors returns an error which combined all stored errors. Return nil if not erros stored.

func (*Drain) First added in v1.2.0

func (d *Drain) First() error

First returns a stored error. Returns nil if there are no errors.

func (*Drain) Float64 added in v1.2.0

func (d *Drain) Float64(p Proxy) float64

Float64 returns float64 value and stores an error.

func (*Drain) Float64Array added in v1.2.0

func (d *Drain) Float64Array(ps ProxySet) []float64

Float64Array returns []float64 value and stores an error.

func (*Drain) Has added in v1.2.0

func (d *Drain) Has() bool

Has returns true if the drain stored some of errors.

func (*Drain) Int64 added in v1.2.0

func (d *Drain) Int64(p Proxy) int64

Int64 returns int64 value and stores an error.

func (*Drain) Int64Array added in v1.2.0

func (d *Drain) Int64Array(ps ProxySet) []int64

Int64Array returns []int64 value and stores an error.

func (*Drain) Map added in v1.2.0

func (d *Drain) Map(p Proxy) map[string]interface{}

Map returns map[string]interface{} value and stores an error.

func (*Drain) MapArray added in v1.2.0

func (d *Drain) MapArray(ps ProxySet) []map[string]interface{}

MapArray returns []map[string]interface{} value and stores an error.

func (*Drain) ProxyArray added in v1.2.0

func (d *Drain) ProxyArray(ps ProxySet) []Proxy

ProxyArray returns []Proxy value and stores an error.

func (*Drain) String added in v1.2.0

func (d *Drain) String(p Proxy) string

String returns string value and stores an error.

func (*Drain) StringArray added in v1.2.0

func (d *Drain) StringArray(ps ProxySet) []string

StringArray returns []string value and stores an error.

type Error

type Error interface {
	// ErrorType returns type of error.
	ErrorType() ErrorType

	// FullAddress returns query string where cause first error.
	FullAddress() string
}

Error get detail information of the errror.

type ErrorType

type ErrorType int

ErrorType is type of errors

const (
	// Etype means expected type is not matched with actual.
	Etype ErrorType = iota + 1

	// Enotfound means key or index doesn't exist.
	Enotfound

	// EmapNorArray means target is not a map nor an array (for JSON Pointer)
	EmapNorArray

	// EconvertFailure means value conversion is failed.
	EconvertFailure

	// EinvalidIndex means token is invalid as index (for JSON Pointer)
	EinvalidIndex

	// EinvalidQuery means query is invalid as JSON Pointer.
	EinvalidQuery

	// ErequiredType means the type mismatch against user required one.
	// For example M() requires map, A() requires array.
	ErequiredType
)

func (ErrorType) String added in v1.2.0

func (et ErrorType) String() string

type Proxy

type Proxy interface {
	// Nil returns true, if target value is nil.
	Nil() bool

	// Value returns a proxied value.  If there are no values, it returns
	// error.
	Value() (interface{}, error)

	// Bool returns its value.  If value isn't the type, it returns error.
	Bool() (bool, error)

	// Int64 returns its value.  If value isn't the type, it returns error.
	Int64() (int64, error)

	// Float64 returns its value.  If value isn't the type, it returns error.
	Float64() (float64, error)

	// String returns its value.  If value isn't the type, it returns error.
	String() (string, error)

	// Array returns its value.  If value isn't the type, it returns error.
	Array() ([]interface{}, error)

	// Map returns its value.  If value isn't the type, it returns error.
	Map() (map[string]interface{}, error)

	// A returns an item from value treated as the array.
	A(n int) Proxy

	// M returns an item from value treated as the map.
	M(k string) Proxy

	// P returns which pointed by JSON Pointer's query q.
	P(q string) Proxy

	// ProxySet returns a set which converted from its array value.
	ProxySet() ProxySet

	// Q returns set of all items which property matchs with k.
	Q(k string) ProxySet
	// contains filtered or unexported methods
}

Proxy is a proxy to access a document (interface{}).

func New

func New(v interface{}) Proxy

New creates a new Proxy instance for v.

func Pointer added in v1.2.0

func Pointer(v interface{}, q string) Proxy

Pointer returns a Proxy which pointed by JSON Pointer's query q

type ProxySet added in v1.2.0

type ProxySet interface {
	// Empty returns true when the set is empty.
	Empty() bool

	// Len returns count of items in the set.
	Len() int

	// BoolArray returns []bool which converterd from the set.
	BoolArray() ([]bool, error)

	// Int64Array returns []int64 which converterd from the set.
	Int64Array() ([]int64, error)

	// Float64Array returns []float64 which converterd from the set.
	Float64Array() ([]float64, error)

	// StringArray returns []string which converterd from the set.
	StringArray() ([]string, error)

	// ArrayArray returns [][]interface{} which converterd from the set.
	ArrayArray() ([][]interface{}, error)

	// MapArray returns []map[string]interface{} which converterd from the set.
	MapArray() ([]map[string]interface{}, error)

	// ProxyArray returns []Proxy which wrap each items.
	ProxyArray() ([]Proxy, error)

	// A returns an proxy for index in the set.
	A(n int) Proxy

	// Q returns set of all items which property matchs with k.
	Q(k string) ProxySet

	// Qc returns set of property of all items.
	Qc(k string) ProxySet
	// contains filtered or unexported methods
}

ProxySet proxies to access to set.

func NewSet added in v1.2.0

func NewSet(v []interface{}) ProxySet

NewSet create a new ProxySet instance for v.

type Type

type Type int

Type is type of value.

const (
	// Tunknown shows value is not supported.
	Tunknown Type = iota

	// Tnil shows value is nil.
	Tnil

	// Tbool shows value is bool.
	Tbool

	// Tint64 shows value is int64.
	Tint64

	// Tfloat64 shows value is float64.
	Tfloat64

	// Tstring shows value is string.
	Tstring

	// Tarray shows value is an array ([]interface{})
	Tarray

	// Tmap shows value is a map (map[string]interface{})
	Tmap
)

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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