pinata

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

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

Go to latest
Published: Aug 4, 2015 License: MIT Imports: 3 Imported by: 0

README

Package pinata is a utility to beat data out of interface{}, []interface{} and map[string]interface{}.

Unlike other packages most methods do not return an error type. They become a no-op when the first error is found so the error can be checked after a series of operations instead of at each operation separately (inspired by https://blog.golang.org/errors-are-values). Special care is taken to return good errors so you can still find out where things went wrong.

See https://godoc.org/github.com/robbiev/pinata for more information.

(image by Raquel Gonzalez)

Documentation

Overview

Package pinata is a utility to beat data out of interface{}, []interface{} and map[string]interface{}. It was origally designed for use with the encoding/json package but can be generally useful.

Unlike other packages most methods do not return an error type. They become a no-op when the first error is found so the error can be checked after a series of operations instead of at each operation separately. Because of this "late" error handling design special care is taken to return good errors so you can still find out where things went wrong.

Here's an example: https://godoc.org/github.com/robbiev/pinata#example-Stick

This API is not thread safe.

Index

Examples

Constants

View Source
const (
	// ErrorReasonIncompatibleType indicates the contents of the Pinata is not compatible with the invoked method.
	ErrorReasonIncompatibleType ErrorReason = "incompatible type"
	// ErrorReasonNotFound indicates the input has not been found in the Pinata.
	ErrorReasonNotFound = "not found"
	// ErrorReasonInvalidInput indicates the input is not in the expected range or format.
	ErrorReasonInvalidInput = "invalid input"
)

Variables

This section is empty.

Functions

func New

func New(contents interface{}) (Stick, Pinata)

New is a starting point for a pinata celebration.

Types

type Error

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

Error is set on the Pinata when something goes wrong.

func (Error) Advice

func (p Error) Advice() string

Advice contains a human readable hint detailing how to remedy this error.

func (Error) Context

func (p Error) Context() (ErrorContext, bool)

Context returns more information about the circumstances of the error.

func (Error) Error

func (p Error) Error() string

Error returns a summary of the problem.

func (Error) Reason

func (p Error) Reason() ErrorReason

Reason indicates why the error occurred.

type ErrorContext

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

ErrorContext contains information about the circumstances of an error.

func (ErrorContext) MethodArgs

func (ec ErrorContext) MethodArgs() []interface{}

MethodArgs returns the input parameters of the method that caused the error.

func (ErrorContext) MethodName

func (ec ErrorContext) MethodName() string

MethodName returns the name of the method that caused the error.

func (ErrorContext) Next

func (ec ErrorContext) Next() (ErrorContext, bool)

Next gets additional context linked to this one.

type ErrorReason

type ErrorReason string

ErrorReason describes the reason for returning an Error.

type Pinata

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

Pinata holds the data.

func NewPinata

func NewPinata(contents interface{}) Pinata

NewPinata creates a new Pinata holding the specified value.

func (Pinata) Map

func (p Pinata) Map() (map[string]interface{}, bool)

Map returns the Pinata value as a map if it is one (the bool indicates success).

func (Pinata) Slice

func (p Pinata) Slice() ([]interface{}, bool)

Slice returns the Pinata value as a slice if it is one (the bool indicates success).

func (Pinata) Value

func (p Pinata) Value() interface{}

Value returns the raw Pinata value.

type Stick

type Stick interface {
	// Error returns the first error encountered or nil if all operations so far
	// were successful.
	Error() error

	// ClearError clears the error and returns it. If there is no error the
	// method has no effect and returns nil, otherwise it returns the error that
	// was cleared.
	ClearError() error

	// PathString gets the string value at the given path within the Pinata. The
	// last element in the path must be a string, the rest must be a
	// map[string]interface{}. The input Pinata must hold a
	// map[string]interface{} as well.
	PathString(Pinata, ...string) string

	// String returns the Pinata as a string if it is one.
	String(Pinata) string

	// IndexString gets the string value at the given index within the Pinata.
	// The input Pinata must hold a []interface{}.
	IndexString(Pinata, int) string

	// PathFloat64 gets the float64 value at the given path within the Pinata.
	// The last element in the path must be a float64, the rest must be a
	// map[string]interface{}. The input Pinata must hold a
	// map[string]interface{} as well.
	PathFloat64(Pinata, ...string) float64

	// Float64 returns the Pinata as a float64 if it is one.
	Float64(Pinata) float64

	// IndexFloat64 gets the string float64 at the given index within the Pinata.
	// The input Pinata must hold a []interface{}.
	IndexFloat64(Pinata, int) float64

	// PathBool gets the bool value at the given path within the Pinata.
	// The last element in the path must be a bool, the rest must be a
	// map[string]interface{}. The input Pinata must hold a
	// map[string]interface{} as well.
	PathBool(Pinata, ...string) bool

	// Bool returns the Pinata as a bool if it is one.
	Bool(Pinata) bool

	// IndexBool gets the string bool at the given index within the Pinata.
	// The input Pinata must hold a []interface{}.
	IndexBool(Pinata, int) bool

	// PathNil asserts nil value at the given path within the Pinata. The last
	// element in the path must be a nil, the rest must be a
	// map[string]interface{}. The input Pinata must hold a
	// map[string]interface{} as well.
	PathNil(Pinata, ...string)

	// Nil asserts the Pinata holds a nil value.
	Nil(Pinata)

	// IndexNil asserts a nil value at the given index within the Pinata. The
	// input Pinata must hold a []interface{}.
	IndexNil(Pinata, int)

	// Path gets the Pinata value at the given path within the Pinata. All
	// elements in the path must be of type map[string]interface{}. The input
	// Pinata must hold a map[string]interface{} as well.
	Path(Pinata, ...string) Pinata

	// Index gets the Pinata value at the given index within the Pinata.
	// The input Pinata must hold a []interface{}.
	Index(Pinata, int) Pinata
}

Stick offers methods of hitting the Pinata and extracting its goodness.

Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/robbiev/pinata"
)

func main() {
	const message = `
	{
		"Name": "Kevin",
		"Phone": ["+44 20 7123 4567", "+44 20 4567 7123"],
		"Address": {
			"Street": "1 Gopher Road",
			"City": "Gophertown"
		}
	}`

	var m map[string]interface{}

	err := json.Unmarshal([]byte(message), &m)
	if err != nil {
		fmt.Println(err)
		return
	}

	stick, p := pinata.New(m)

	type gopher struct {
		Name  string
		Phone string
		City  string
	}

	// no error handling here
	kevin := gopher{
		Name:  stick.PathString(p, "Name"),
		Phone: stick.IndexString(stick.Path(p, "Phone"), 0),
		City:  stick.PathString(p, "Address", "City"),
	}

	if err := stick.ClearError(); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("Name:", kevin.Name)
	fmt.Println("Phone:", kevin.Phone)
	fmt.Println("City:", kevin.City)

}
Output:

Name: Kevin
Phone: +44 20 7123 4567
City: Gophertown

func NewStick

func NewStick() Stick

NewStick returns a new Stick to hit a Pinata with.

Jump to

Keyboard shortcuts

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