value

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2021 License: MIT Imports: 9 Imported by: 0

README

The Value in go

GoDoc Build Status Coverage Status Go Report Card stability-unstable License

Manipulates(gets,sets,convert etc.) interface{} value, convenient and simple, and reflect-based.

Features

You can do the following things, after new Value from interface{} like this v := value.New(var).

  • Get value from map slice array or struct with key idx or fieldname
  • Put key value pair to map slice or array
  • Set value
  • For range value any type
  • Get value with Int*, Float*, PList etc.
  • Numerical type is adaptive when get; example, i kind is uint8 and get with Int16() is legal, the value is automatically converted to int16 instead of type mismatch.
  • Provide Must* API, for chaining call and some friendly writing.
  • Unmarshal to a value with Value.ConvTo
  • Supported covert to time.Duration or time.Time etc..
  • Supported convert to frequently used build-in type

Example

example_test.go

convert v to net.IP, time.Duration etc..

package main

import (
	"fmt"
	"log"
	"net"
	"time"

	"github.com/helloyi/go-value"
)

func main() {
	x := map[string]interface{}{
		"a": 1,
		"B": "b",
		"C": "c",
		"D": "13s",
		"E": "Fri Nov 1 19:13:55 +0800 CST 2019",
		"F": "8.8.8.8",
	}

	var y struct {
		A int    `value:"a"` // set with name "a"
		B int    `value:"_"` // passed
		C string // set with name "C"
		D time.Duration
		E *time.Time
		F *net.IP
	}

	v := value.New(x)
	if err := v.ConvTo(&y); err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("%v\n", y)
	// Output: {1 0 c 13s 2019-11-01 19:13:55 +0800 CST 8.8.8.8}
}

Documentation

Overview

Package value manipulates(gets,sets,convert etc.) interface{} value, convenient and simple, and reflect-based.

Example
resp, err := http.Get("https://api.alternative.me/fng/?limit=10")
if err != nil {
	log.Fatalln(err)
}
defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
	log.Fatalln(err)
}

var res interface{}
if err := json.Unmarshal(data, &res); err != nil {
	log.Fatalln(err)
}

rest := value.New(res)

fngs := map[string]string{}
_ = rest.MustGet("data").EachDo(func(_, fng *value.Value) error {
	ts, _ := fng.MustGet("timestamp").String()
	val, _ := fng.MustGet("value").String()
	fngs[ts] = val
	return nil
})

data, err = json.MarshalIndent(fngs, "", "  ")
if err != nil {
	log.Fatalln(err)
}
log.Println(string(data))
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// TimeLayout default time layout
	TimeLayout = "Mon Jan 2 15:04:05 -0700 MST 2006"
)

Functions

func ConvTo

func ConvTo(src, dst interface{}) error

ConvTo convToert src to dst

Types

type ErrCannotBeNil

type ErrCannotBeNil struct {
	Method string
}

ErrCannotBeNil ...

func (*ErrCannotBeNil) Error

func (e *ErrCannotBeNil) Error() string

type ErrCannotSet

type ErrCannotSet struct {
	Method string
}

ErrCannotSet ...

func (*ErrCannotSet) Error

func (e *ErrCannotSet) Error() string

type ErrNotExist

type ErrNotExist struct {
	Method string
	Thing  string
}

ErrNotExist ...

func (*ErrNotExist) Error

func (e *ErrNotExist) Error() string

type ErrNumOverflow

type ErrNumOverflow struct {
	Method string
	Kind   reflect.Kind
}

ErrNumOverflow ...

func (*ErrNumOverflow) Error

func (e *ErrNumOverflow) Error() string

type ErrOutOfRange

type ErrOutOfRange struct {
	Method string
}

ErrOutOfRange ...

func (*ErrOutOfRange) Error

func (e *ErrOutOfRange) Error() string

type ErrTypeUnequal

type ErrTypeUnequal struct {
	Method string
	Kind1  reflect.Kind
	Kind2  reflect.Kind
}

ErrTypeUnequal ...

func (*ErrTypeUnequal) Error

func (e *ErrTypeUnequal) Error() string

type ErrUnsupportedKind

type ErrUnsupportedKind struct {
	Method string
	Kind   interface{}
}

ErrUnsupportedKind ...

func (*ErrUnsupportedKind) Error

func (e *ErrUnsupportedKind) Error() string

type Value

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

Value ...

func New

func New(v interface{}) *Value

New new a Value from v

func (*Value) AList

func (v *Value) AList() ([][2]*Value, error)

AList returns t's underlying value as an association list. It returns error if t's kind is not Map, Array, Slice or Struct.

func (*Value) Bool

func (v *Value) Bool() (bool, error)

Bool returns t's underlying value. It returns error if t's kind is not Bool.

func (*Value) Bytes

func (v *Value) Bytes() ([]byte, error)

Bytes returns t's underlying value as a []bytes. It returns error if t's underlying value is not a slice of bytes.

func (*Value) Complex128

func (v *Value) Complex128() (complex128, error)

Complex128 returns t's underlying value as an complex128. It returns error if t's kind is not Uint*, Int*, Float* or Complex*.

func (*Value) Complex64

func (v *Value) Complex64() (complex64, error)

Complex64 returns t's underlying value as an complex64. It returns error if t's kind is not Uint*, Int*, Float32 or Complex64.

func (*Value) ConvTo

func (v *Value) ConvTo(dst interface{}) error

ConvTo convToert t to dst

Example
x := map[string]interface{}{
	"a": 1,
	"B": "b",
	"C": "c",
	"D": "13s",
	"E": "Fri Nov 1 19:13:55 +0800 CST 2019",
	"F": "8.8.8.8",
	"G": "http://host/path?param=x",
	"H": "[0-9]+",
	"I": "name <uer@mail.com>",
}

var y struct {
	A int    `value:"a"` // set with name "a"
	B int    `value:"_"` // passed
	C string // set with name "C"
	D time.Duration
	E *time.Time
	F *net.IP
	G *url.URL
	H *regexp.Regexp
	I *mail.Address
}

v := value.New(x)
if err := v.ConvTo(&y); err != nil {
	log.Fatalln(err)
}
fmt.Printf("%v\n", y)
Output:

{1 0 c 13s 2019-11-01 19:13:55 +0800 CST 8.8.8.8 http://host/path?param=x [0-9]+ "name" <uer@mail.com>}

func (*Value) EachDo

func (v *Value) EachDo(f eachDoFunc) error

EachDo ...

Example (Map)
var m interface{} = map[int]int{
	1: 1,
	2: 2,
}
mv := value.New(m)

sum := 0
if err := mv.EachDo(func(key, val *value.Value) error {
	sum += val.MustInt()
	return nil
}); err != nil {
	log.Fatalln(err)
}

fmt.Println(sum)
Output:

3

func (*Value) Float32

func (v *Value) Float32() (float32, error)

Float32 returns t's underlying value as an float32. It returns error if t's kind is not Uint*, Int* or Float32.

func (*Value) Float64

func (v *Value) Float64() (float64, error)

Float64 returns t's underlying value as an float64. It returns error if t's kind is not Uint*, Int* or Float*.

func (*Value) Get

func (v *Value) Get(k interface{}) (*Value, error)

Get returns the value with the given key.

If t's kind is Map, Get returns the value associated with key in the map. If t's kind is Array or Slice, Get returns t's k'th element, the k must be int. If t's kind is Struct, Get returns the struct field with the given field name, the k must be string. if t's kind is Interface or Ptr, indirect it. It returns the nil if k is not found in the t. It returns error if t's kind is not Map, Array, Slice or Struct.

func (*Value) Int

func (v *Value) Int() (i int, err error)

Int returns t's underlying value as an int. It returns error if t's kind is not Int, Int8, Int16, Int32, Uint8 or Uint16, and if t's kind is Int64 or Uint32 also Int is 32 bits.

func (*Value) Int16

func (v *Value) Int16() (int16, error)

Int16 returns t's underlying value as an int16. It returns error if t's kind is not Int, Int8, Int16, or Uint8.

func (*Value) Int32

func (v *Value) Int32() (int32, error)

Int32 returns t's underlying value as an int32. It returns error if t's kind is not Int, Int8, Int16, Int32, Uint8 or Uint16, and if t's kind is Int also Int is 64 bits.

func (*Value) Int64

func (v *Value) Int64() (int64, error)

Int64 returns t's underlying value as an int64. It returns error if t's kind is not Int, Int8, Int16, Int32, Uint8, Uint16, Uint32 and if t's kind is Uint also Uint is 64 bits.

func (*Value) Int8

func (v *Value) Int8() (int8, error)

Int8 returns t's underlying value as an int8. It returns error if t's kind is not Int8.

func (*Value) Interface

func (v *Value) Interface() interface{}

func (*Value) Map

func (v *Value) Map() (map[*Value]*Value, error)

Map returns t's underlying value as a map. It returns error if t's kind is not Map, Array, Slice or Struct.

func (*Value) MustAList

func (v *Value) MustAList() [][2]*Value

MustAList must api for AList

func (*Value) MustBool

func (v *Value) MustBool() bool

MustBool must api for Bool()

func (*Value) MustBytes

func (v *Value) MustBytes() []byte

MustBytes must api for Bytes()

func (*Value) MustComplex128

func (v *Value) MustComplex128() complex128

MustComplex128 must api for Complex128()

func (*Value) MustComplex64

func (v *Value) MustComplex64() complex64

MustComplex64 must api for Complex64()

func (*Value) MustFloat32

func (v *Value) MustFloat32() float32

MustFloat32 must api for Float32()

func (*Value) MustFloat64

func (v *Value) MustFloat64() float64

MustFloat64 must api for Float64()

func (*Value) MustGet

func (v *Value) MustGet(k interface{}) *Value

MustGet must api for Get

func (*Value) MustInt

func (v *Value) MustInt() int

MustInt must api for Int()

func (*Value) MustInt16

func (v *Value) MustInt16() int16

MustInt16 must api for Int16()

func (*Value) MustInt32

func (v *Value) MustInt32() int32

MustInt32 must api for Int32()

func (*Value) MustInt64

func (v *Value) MustInt64() int64

MustInt64 must api for Int64()

func (*Value) MustInt8

func (v *Value) MustInt8() int8

MustInt8 must api for Int8()

func (*Value) MustMap

func (v *Value) MustMap() map[*Value]*Value

MustMap must api for Map

func (*Value) MustPList

func (v *Value) MustPList() []*Value

MustPList must api for PList

func (*Value) MustSlice

func (v *Value) MustSlice() []*Value

MustSlice must api for Slice

func (*Value) MustString

func (v *Value) MustString() string

MustString must api for String()

func (*Value) MustUint

func (v *Value) MustUint() uint

MustUint must api for Uint()

func (*Value) MustUint16

func (v *Value) MustUint16() uint16

MustUint16 must api for Uint16()

func (*Value) MustUint32

func (v *Value) MustUint32() uint32

MustUint32 must api for Uint32()

func (*Value) MustUint64

func (v *Value) MustUint64() uint64

MustUint64 must api for Uint64()

func (*Value) MustUint8

func (v *Value) MustUint8() uint8

MustUint8 must api for Uint8()

func (*Value) PList

func (v *Value) PList() ([]*Value, error)

PList returns t's underlying value as an property list. It returns error if t's kind is not Map, Array, Slice or Struct.

func (*Value) Ptr

func (v *Value) Ptr() uintptr

func (*Value) Put

func (v *Value) Put(key, val interface{}) (err error)

Put put k, v to map, array, slice or struct(structed type).

If t's kind is map, the k indicates key of map. If t's kind is array/slice, the k indecates index of array/slice. If t's kind is struct, the k indecates fieldname of struct.

If k in t, and set k's value to v.

If t's kind is not map, array, slice or struct, returns ErrUnsupportedKind.

Example
var m interface{} = map[int]int{
	1: 1,
	2: 2,
}
mv := value.New(m)

if err := mv.Put(1, 100); err != nil {
	log.Fatalln(err)
}

fmt.Println(mv.MustGet(1).MustInt())
Output:

100

func (*Value) Set

func (v *Value) Set(iv interface{}) error

Set set t's value to v.

If t's value can't setable, returns ErrCannotSet. If t's kind and v's kind is not equivalence, returns ErrTypeUnequal. It returns nil, that set successful.

If set map key, struct field or array/slice index, using Value.Put.

func (*Value) Slice

func (v *Value) Slice() ([]*Value, error)

Slice returns t's underlying value as a slice. It returns error if t's kind is not Array, Slice or Struct.

func (*Value) String

func (v *Value) String() (string, error)

func (*Value) Uint

func (v *Value) Uint() (i uint, err error)

Uint returns t's underlying value as an uint. It returns error if t's kind is not Uint, Uint8, Uint16 or Uint32, and if t's kind is Uint64 also Uint is 32 bits.

func (*Value) Uint16

func (v *Value) Uint16() (uint16, error)

Uint16 returns t's underlying value as an uint16. It returns error if t's kind is not Uint8 or Uint16.

func (*Value) Uint32

func (v *Value) Uint32() (uint32, error)

Uint32 returns t's underlying value as an uint32. It returns error if t's kind is not Uint8, Uint16 or Uint32, and if t's kind is Uint also Uint is 64 bits.

func (*Value) Uint64

func (v *Value) Uint64() (uint64, error)

Uint64 returns t's underlying value as an uint64. It returns error if t's kind is not Uint*.

func (*Value) Uint8

func (v *Value) Uint8() (uint8, error)

Uint8 returns t's underlying value as an uint8. It returns error if t's kind is not Uint8.

Jump to

Keyboard shortcuts

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