dproxy

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2015 License: MIT Imports: 2 Imported by: 31

README

dProxy - document proxy

dProxy is a proxy to access interface{} (document) by simple query. It is intetedd 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

  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)
    

    You can verify queries easily.

LICENSE

MIT license. See LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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
)

type Proxy

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

	// 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
	// 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 object.

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