table

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2019 License: MIT Imports: 4 Imported by: 0

README

The Table in go

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

Put 'interface{}' on the Table, and to unfold, then, manipulates interface{} value, convenient and simple.

Usage

Pick up some value from interface{}.

package main

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/helloyi/gotable"
)

func main() {
	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 := table.New(res)

	fngs := map[string]string{}
	_ = rest.MustGet("data").EachDo(func(_, fng *table.Table) 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))
}

For a map:

package main

import (
	"github.com/helloyi/gotable"
	"log"
)

func main() {
	var m interface {} = map[int]int{
		1: 1,
		2: 2,
	}
	t := table.New(m)

	sum := 0
	if err := t.EachDo(func(k, v *table.Table) error {
		sum += v.MustInt()
		return nil
	}); err != nil {
		log.Fatalln(err)
	}

	log.Println(sum) // print 3
}

Convert to struct

package main

import (
	"fmt"
	"log"

	"github.com/helloyi/gotable"
)


func main() {
	x := map[string]interface{}{
		"a": 1,
		"A": 11,

		"B": "b",
		"b": "bb",

		"C": "c",
		"c": "cc",
	}

	var y struct {
		A int    `table:"a"` // set with name "a"
		B string `table:"_"` // passed
		C string // set with name "C"
	}

	t := table.New(x)
	if err := t.Conv(&y); err != nil {
		log.Fatalln(err)
	}
	fmt.Printf("%+v\n", y)
}

Set map key:

package main

import (
	"github.com/helloyi/gotable"
	"log"
)

func main() {
  var m interface {} = map[int]int{
		1: 1,
		2: 2,
	}
	t := table.New(m)

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

	log.Println(t.MustGet(1).MustInt()) // print 100
}

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

This section is empty.

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 added in v0.1.3

type ErrOutOfRange struct {
	Method string
}

ErrOutOfRange ...

func (*ErrOutOfRange) Error added in v0.1.3

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 Table

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

Table ...

func New

func New(v interface{}) *Table

New new a Table from v

func (*Table) AList

func (t *Table) AList() ([][2]*Table, 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 (*Table) Bool

func (t *Table) Bool() (bool, error)

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

func (*Table) Bytes

func (t *Table) 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 (*Table) Complex128

func (t *Table) 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 (*Table) Complex64

func (t *Table) 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 (*Table) ConvTo added in v0.1.3

func (t *Table) ConvTo(value interface{}) error

ConvTo convToert t to value

func (*Table) EachDo

func (t *Table) EachDo(f eachDoFunc) error

EachDo ...

func (*Table) Float32

func (t *Table) 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 (*Table) Float64

func (t *Table) 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 (*Table) Get

func (t *Table) Get(k interface{}) (*Table, 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 (*Table) Int

func (t *Table) 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 (*Table) Int16

func (t *Table) 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 (*Table) Int32

func (t *Table) 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 (*Table) Int64

func (t *Table) 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 (*Table) Int8

func (t *Table) Int8() (int8, error)

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

func (*Table) Interface

func (t *Table) Interface() interface{}

func (*Table) Map

func (t *Table) Map() (map[*Table]*Table, 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 (*Table) MustAList added in v0.1.3

func (t *Table) MustAList() [][2]*Table

MustAList must api for AList

func (*Table) MustFloat32

func (t *Table) MustFloat32() float32

MustFloat32 must api for Float32()

func (*Table) MustFloat64

func (t *Table) MustFloat64() float64

MustFloat64 must api for Float64()

func (*Table) MustGet

func (t *Table) MustGet(k interface{}) *Table

MustGet must api for Get

func (*Table) MustInt

func (t *Table) MustInt() int

MustInt must api for Int()

func (*Table) MustInt16

func (t *Table) MustInt16() int16

MustInt16 must api for Int16()

func (*Table) MustInt32

func (t *Table) MustInt32() int32

MustInt32 must api for Int32()

func (*Table) MustInt64

func (t *Table) MustInt64() int64

MustInt64 must api for Int64()

func (*Table) MustInt8

func (t *Table) MustInt8() int8

MustInt8 must api for Int8()

func (*Table) MustMap

func (t *Table) MustMap() map[*Table]*Table

MustMap must api for Map

func (*Table) MustPList added in v0.1.3

func (t *Table) MustPList() []*Table

MustPList must api for PList

func (*Table) MustSlice added in v0.1.3

func (t *Table) MustSlice() []*Table

MustSlice must api for Slice

func (*Table) PList

func (t *Table) PList() ([]*Table, 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 (*Table) Ptr

func (t *Table) Ptr() uintptr

func (*Table) Put

func (t *Table) Put(k, v 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.

func (*Table) Set

func (t *Table) Set(v 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 Table.Put.

TODO: balala

func (*Table) Slice

func (t *Table) Slice() ([]*Table, error)

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

func (*Table) String

func (t *Table) String() (string, error)

func (*Table) Uint

func (t *Table) 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 (*Table) Uint16

func (t *Table) 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 (*Table) Uint32

func (t *Table) 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 (*Table) Uint64

func (t *Table) Uint64() (uint64, error)

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

func (*Table) Uint8

func (t *Table) 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