ei

package module
v0.0.0-...-4f519a4 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2017 License: BSD-3-Clause Imports: 3 Imported by: 22

README

#ei GoDoc Painless empty interface type conversion.

Download:

go get github.com/jaracil/ei

##Description

ei package allows easy type conversion between empty interfaces and basic go types.

Supported types are:

  • int8, int16, int32, int64, int
  • uin8, byte, uint16, uint32, uint64, uint
  • float32, float64
  • string, []byte
  • time.Time
  • map[string]interface{} or shorthand type ei.M
  • []interface{} or shorthand type ei.S
  • interface{} pointing to one of above types

###Examples

package main

import(
	"github.com/jaracil/ei"
	"strings"
)

// Middleware function, see examples below
func uppercase(i ei.Ei, p ...interface{}) ei.Ei { 
	s, err := i.String()
	if err != nil {
		return ei.N(err)
	}
	return  ei.N(strings.ToUpper(s))
}

func main(){
	var i interface{}
	
	// float to int (zero value on error)
	i = 5.2
	println(ei.N(i).IntZ()) // 5
	
	// string to int64 (zero value on error)
	i = "5"
	println(ei.N(i).Int64Z()) // 5
	
	// map[string]interface{} access. Using shorthand ei.M type
	i = ei.M{"sota":10, "caballo":"11"}
	println(ei.N(i).M("sota").IntZ(), ei.N(i).M("caballo").IntZ()) // 10 11
	l, _ := ei.N(i).Len()
	println(l) // 2 

	// []interface{} access. Using shorthand ei.S type
	i = ei.S{10, 11, 12}
	println(ei.N(i).S(0).IntZ(), ei.N(i).S(1).StringZ()) // 10 11
	l, _ = ei.N(i).Len()
	println(l) // 3
	
	// Nested access
	i = ei.M{"red":ei.S{255,0,0}, "green":ei.S{0,255,0}, "blue":ei.S{0,0,255}}
	println(ei.N(i).M("green").S(1).ByteZ()) // 255
	
	// Without shorthand types
	i = map[string]interface{}{"red":[]interface{}{255,0,0}, "green":[]interface{}{0,255,0}, "blue":[]interface{}{0,0,255}}
	println(ei.N(i).M("green").S(1).IntZ()) // 255
	
	// Errors are propagated 
	i = ei.M{"red":ei.S{255,0,0}, "green":ei.S{0,255,0}, "blue":ei.S{0,0,255}}
	v, err := ei.N(i).M("brown").S(1).Int() // Key error on M("brown") is propagated up to Int()
	println(v, err.Error()) // 0 key not found

	// Middleware transformation
	i = "hello"
	println(ei.N(i).F(uppercase).StringZ()) // HELLO
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEiErr

func IsEiErr(err error) bool

IsEiErr returns true if err type is EiErr.

func NewEiErr

func NewEiErr(err string) error

NewEiErr creates new EiErr.

Types

type Ei

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

Ei (Empty Interface struct). Used to wrap interface{} and provide methods.

func N

func N(i interface{}) Ei

N wraps a suppored type value (including interface{}) into Ei type.

Supported types are:

int8, int16, int32, int64, int
uin8, byte, uint16, uint32, uint64, uint
float32, float64
string, []byte
time.Time
map[string]interface{} or shorthand ei.M
[]interface{} or shorthand ei.S
interface{} pointing to one of above types

func (Ei) Bool

func (i Ei) Bool() (bool, error)

Bool converts Ei to bool.

func (Ei) BoolZ

func (i Ei) BoolZ() bool

BoolZ converts Ei to bool, returns false on error.

func (Ei) Byte

func (i Ei) Byte() (byte, error)

Byte converts Ei to byte.

func (Ei) ByteZ

func (i Ei) ByteZ() byte

ByteZ converts Ei to byte, returns zero value on error.

func (Ei) Bytes

func (i Ei) Bytes() ([]byte, error)

Bytes converts Ei to []byte.

func (Ei) BytesZ

func (i Ei) BytesZ() []byte

BytesZ converts Ei to []byte, returns nil on error.

func (Ei) Catch

func (i Ei) Catch(val interface{}) Ei

Catch returns val if input value is an error.

func (Ei) Clip

func (i Ei) Clip(min, max interface{}) Ei

Clip corrects input value to min if value < min or to max if value > max.

func (Ei) F

func (i Ei) F(f func(Ei, ...interface{}) Ei, p ...interface{}) Ei

F inserts a transformation function to Ei evaluation chain.

func (Ei) Float32

func (i Ei) Float32() (float32, error)

Float32 converts Ei to float32.

func (Ei) Float32Z

func (i Ei) Float32Z() float32

Float32Z converts Ei to float32, returns zero value on error.

func (Ei) Float64

func (i Ei) Float64() (float64, error)

Float64 converts Ei to float64.

func (Ei) Float64Z

func (i Ei) Float64Z() float64

Float64Z converts Ei to float64, returns zero value on error

func (Ei) HasKey

func (i Ei) HasKey(k string) (bool, error)

HasKey returns true if map[string]interface{} stored in Ei has the key k.

func (Ei) HasKeyZ

func (i Ei) HasKeyZ(k string) bool

HasKeyZ returns true if map[string]interface{} stored in Ei has the key k.

func (Ei) In

func (i Ei) In(sl []string) Ei

In searchs the input value in the list, returns an error if value is not found.

func (Ei) Int

func (i Ei) Int() (int, error)

Int converts Ei to int.

func (Ei) Int16

func (i Ei) Int16() (int16, error)

Int16 converts Ei to int16.

func (Ei) Int16Z

func (i Ei) Int16Z() int16

Int16Z converts Ei to int16, returns zero value on error.

func (Ei) Int32

func (i Ei) Int32() (int32, error)

Int32 converts Ei to int32.

func (Ei) Int32Z

func (i Ei) Int32Z() int32

Int32Z converts Ei to int32, returns zero value on error.

func (Ei) Int64

func (i Ei) Int64() (int64, error)

Int64 converts Ei to int64.

func (Ei) Int64Z

func (i Ei) Int64Z() int64

Int64Z converts Ei to int64, returns zero value on error.

func (Ei) Int8

func (i Ei) Int8() (int8, error)

Int8 converts Ei to int8.

func (Ei) Int8Z

func (i Ei) Int8Z() int8

Int8Z converts Ei to int8, returns zero value on error.

func (Ei) IntZ

func (i Ei) IntZ() int

IntZ converts Ei to int, returns zero value on error.

func (Ei) Len

func (i Ei) Len() (int, error)

Len returns the length of map[string]interface{} or []interface{} stored in Ei.

func (Ei) Limit

func (i Ei) Limit(min, max interface{}) Ei

Limit retruns an error if val < min or val > max

func (Ei) Lower

func (i Ei) Lower() Ei

Lower returns input value converted to lowercase.

func (Ei) M

func (i Ei) M(k string) Ei

M selects one key in map[string]interface{} and returns its value as Ei.

func (Ei) Map

func (i Ei) Map(m M) Ei

Map replaces the input value by the map[value] entry, returns an error if value is not found.

func (Ei) MapStr

func (i Ei) MapStr() (map[string]interface{}, error)

MapStr converts Ei to map[string]interface{}

func (Ei) MapStrZ

func (i Ei) MapStrZ() map[string]interface{}

MapStrZ converts Ei to map[string]interface{}, returns zero value on error.

func (Ei) Raw

func (i Ei) Raw() (interface{}, error)

Raw returns the content of Ei as interface{} or error if Ei points to EiErr

func (Ei) RawZ

func (i Ei) RawZ() interface{}

RawZ returns the content of Ei as interface{} or nil if Ei points to EiErr

func (Ei) S

func (i Ei) S(idx int) Ei

S selects one index in []interface{} and returns its value as Ei.

func (Ei) Slice

func (i Ei) Slice() ([]interface{}, error)

Slice converts Ei to []interface{}

func (Ei) SliceZ

func (i Ei) SliceZ() []interface{}

SliceZ converts Ei to []interface{}, returns zero value on error.

func (Ei) String

func (i Ei) String() (string, error)

String converts Ei to string.

func (Ei) StringZ

func (i Ei) StringZ() string

StringZ converts Ei to string, returns empty string on error.

func (Ei) Time

func (i Ei) Time() (time.Time, error)

Time converts Ei to time.Time.

func (Ei) TimeZ

func (i Ei) TimeZ() time.Time

TimeZ converts Ei to time.Time, returns time.Time{} value on error.

func (Ei) Uint

func (i Ei) Uint() (uint, error)

Uint converts Ei to uint.

func (Ei) Uint16

func (i Ei) Uint16() (uint16, error)

Uint16 converts Ei to uint16.

func (Ei) Uint16Z

func (i Ei) Uint16Z() uint16

Uint16Z converts Ei to uint16, returns zero value on error.

func (Ei) Uint32

func (i Ei) Uint32() (uint32, error)

Uint32 converts Ei to uint32.

func (Ei) Uint32Z

func (i Ei) Uint32Z() uint32

Uint32Z converts Ei to uint32, returns zero value on error.

func (Ei) Uint64

func (i Ei) Uint64() (uint64, error)

Uint64 converts Ei to uint64.

func (Ei) Uint64Z

func (i Ei) Uint64Z() uint64

Uint64Z converts Ei to uint64, returns zero value on error.

func (Ei) Uint8

func (i Ei) Uint8() (uint8, error)

Uint8 converts Ei to uint8.

func (Ei) Uint8Z

func (i Ei) Uint8Z() uint8

Uint8Z converts Ei to uint8, returns zero value on error.

func (Ei) UintZ

func (i Ei) UintZ() uint

UintZ converts Ei to uint, returns zero value on error.

func (Ei) Upper

func (i Ei) Upper() Ei

Upper returns input value converted to uppercase.

type EiErr

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

EiErr specific Ei conversion error

func (*EiErr) Error

func (e *EiErr) Error() string

Error returns EiErr error string.

type M

type M map[string]interface{}

M is a convenience shorthand for map[string]interface{} type.

type S

type S []interface{}

S is a convenience shorthand for []interface{} type.

Jump to

Keyboard shortcuts

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