value

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: Apache-2.0 Imports: 21 Imported by: 0

README

value

Value in GO

  • All instances are immutable and good for multi-threading or go-routing.
  • Deterministic serialization that guarantee consistent results before hashing.
  • No cycle pack deps, because all objects are immutable
List
v := value.NewList()

v = v.Insert(value.Boolean(true))
v = v.Insert(value.Long(123))
v = v.Insert(value.Double(-12.34))
v = v.Insert(value.Utf8("text"))
v = v.Insert(value.Raw([]byte{0, 1, 2}, false))

mp, _ := value.Pack(v)

c, err := value.Unpack(mp, false)
if err != nil {
    t.Errorf("unpack fail %v", err)
}

require.True(t, v.Equal(c))
Map
b = value.NewMap()

c := value.NewMap()
c.Put("5", value.Long(5))

b.Put("name", value.Utf8("name"))
b.Put("123", value.Long(123))
b.Put("map", c)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllowFastAppends = true
View Source
var Base64Prefix = "base64,"
View Source
var DecimalExpDelim = byte('x')
View Source
var DecimalExpDelimStr = "x"
View Source
var ErrUnseal = errors.New("unseal error")
View Source
var False = boolValue(false)
View Source
var FirstIndexKey = 0
View Source
var PrecisionLevel = 0.00001
View Source
var True = boolValue(true)
View Source
var UnknownPrefix = "data:application/x-msgpack-ext;"
View Source
var ValueClass = reflect.TypeOf((*Value)(nil)).Elem()

Functions

func Equal

func Equal(left Value, right Value) bool

func Hash

func Hash(val Value, hash crypto.Hash) ([]byte, error)

func Hex

func Hex(val Value) string

func Jsonify

func Jsonify(val Value) string

func MessagePacker

func MessagePacker(w io.Writer) *messagePacker

func MessageParser

func MessageParser() *messageParser

func MessageReader

func MessageReader(r io.Reader) *messageIOUnpacker

func MessageUnpacker

func MessageUnpacker(buf []byte, copy bool) *messageBufUnpacker

func Pack

func Pack(val Value) ([]byte, error)

func PackStruct

func PackStruct(obj interface{}) ([]byte, error)

func ParseBoolean

func ParseBoolean(str string) boolValue

func ParseStruct

func ParseStruct(unpacker Unpacker, parser Parser, value reflect.Value, schema *Schema) error

func ReadStream

func ReadStream(r io.Reader, out chan<- Value) error

func Seal

func Seal(val Value, recipientPublicKey, senderPrivateKey *[32]byte) ([]byte, error)

use box.GenerateKey(rand.Reader) to get keys

func Unknown

func Unknown(tagAndData []byte) unknownValue

func UnpackBigInt

func UnpackBigInt(data []byte) (*big.Int, error)

func UnpackDecimal

func UnpackDecimal(data []byte) (decimal.Decimal, error)

func UnpackStruct

func UnpackStruct(buf []byte, obj interface{}, copy bool) error

func Write

func Write(w io.Writer, val Value) error

func WriteStream

func WriteStream(w io.Writer, valueC <-chan Value) error

Types

type Bool

type Bool interface {
	Value

	Boolean() bool
}

func Boolean

func Boolean(b bool) Bool

type Collection

type Collection interface {
	Len() int

	Entries() []MapEntry
}

type Ext

type Ext byte
const (
	UnknownExt Ext = iota

	BigIntExt
	DecimalExt

	MaxExt
)

type Extension

type Extension interface {
	Value

	Native() []byte
}

type Field

type Field struct {
	FieldNum    int
	FieldType   reflect.Type
	FieldName   string
	Array       bool
	Struct      bool
	Repeated    bool
	FieldSchema *Schema
	Tag         int
}

type Format

type Format int
const (
	EOF Format = iota
	UnexpectedEOF
	NilToken
	BoolToken
	LongToken
	DoubleToken
	FixExtToken
	BinHeader
	StrHeader
	ListHeader
	MapHeader
	ExtHeader
)

type Kind

type Kind int
const (
	INVALID Kind = iota
	BOOL
	NUMBER
	STRING
	LIST
	MAP
	UNKNOWN
)

func (Kind) String

func (k Kind) String() string

type List

type List interface {
	Value
	Collection

	Items() []ListItem

	Values() []Value

	GetAt(int) Value

	GetBoolAt(int) Bool

	GetNumberAt(int) Number

	GetStringAt(int) String

	GetListAt(int) List

	GetMapAt(int) Map

	PutAt(int, Value) List

	InsertAt(int, Value) List

	Append(Value) List

	RemoveAt(int) List

	Select(int) []Value

	InsertAll(int, []Value) List

	DeleteAll(int) List
}

func EmptyList

func EmptyList() List

func EmptySparseList

func EmptySparseList() List

func Single

func Single(value Value) List

func SolidList

func SolidList(list []Value) List

func SortedSparseList

func SortedSparseList(items []ListItem) List

func SparseList

func SparseList(items []ListItem, sortedItems bool) List

func SparseListOf

func SparseListOf(list []Value) List

func Tuple

func Tuple(values ...Value) List

type ListItem

type ListItem interface {

	/*
		Index in the array where item is located
	*/
	Key() int

	Value() Value

	Equal(ListItem) bool
}

func Item

func Item(key int, value Value) ListItem

type Map

type Map interface {
	Value
	Collection

	HashMap() map[string]Value

	Keys() []string

	Values() []Value

	Get(string) (Value, bool)

	GetBool(string) Bool

	GetNumber(string) Number

	GetString(string) String

	GetList(string) List

	GetMap(string) Map

	Insert(key string, value Value) Map

	Put(key string, value Value) Map

	Remove(string) Map

	Select(string) []Value

	InsertAll(string, []Value) Map

	DeleteAll(string) Map
}

func EmptyMap

func EmptyMap() Map

func SortedMap

func SortedMap(entries []MapEntry, sortedEntries bool) Map

type MapEntry

type MapEntry interface {
	Key() string

	Value() Value

	Equal(MapEntry) bool
}

func Entry

func Entry(key string, value Value) MapEntry

type Number

type Number interface {
	Value

	Type() NumberType

	IsNaN() bool

	Long() int64

	Double() float64

	BigInt() *big.Int

	Decimal() decimal.Decimal

	Add(Number) Number

	Subtract(Number) Number
}

func BigInt

func BigInt(val *big.Int) Number

func Decimal

func Decimal(dec decimal.Decimal) Number

func Double

func Double(val float64) Number

func Long

func Long(val int64) Number

func Nan

func Nan() Number

func ParseNumber

func ParseNumber(str string) Number

type NumberType

type NumberType int
const (
	InvalidNumber NumberType = iota
	LONG
	DOUBLE
	BIGINT
	DECIMAL
)

func (NumberType) String

func (t NumberType) String() string

type Packer

type Packer interface {
	PackNil()

	PackBool(bool)

	PackLong(int64)

	PackDouble(float64)

	PackStr(string)

	PackBin([]byte)

	PackExt(xtag Ext, data []byte)

	PackList(int)

	PackMap(int)

	PackRaw([]byte)

	Error() error
}

type Parser

type Parser interface {
	ParseBool([]byte) bool

	ParseLong([]byte) int64

	ParseDouble([]byte) float64

	ParseBin([]byte) int

	ParseStr([]byte) int

	ParseList([]byte) int

	ParseMap([]byte) int

	ParseExt([]byte) (int, []byte)

	Error() error
}

type Schema

type Schema struct {
	Fields       map[int]*Field // tag is the key
	SortedFields []*Field
}

type String

type String interface {
	Value

	Type() StringType

	Len() int

	Utf8() string

	Raw() []byte
}

func ParseString

func ParseString(str string) String

func Raw

func Raw(val []byte, copyFlag bool) String

func Stringf

func Stringf(format string, args ...interface{}) String

func Utf8

func Utf8(val string) String

type StringType

type StringType int
const (
	InvalidString StringType = iota
	UTF8
	RAW
)

func (StringType) String

func (t StringType) String() string

type Unpacker

type Unpacker interface {
	Next() (Format, []byte)

	Read(int) ([]byte, error)
}

type Value

type Value interface {
	fmt.Stringer
	json.Marshaler
	encoding.BinaryMarshaler

	Kind() Kind

	Class() reflect.Type

	Object() interface{}

	Pack(Packer)

	PrintJSON(out *strings.Builder)

	Equal(Value) bool
}

func Parse

func Parse(unpacker Unpacker, parser Parser) (Value, error)

func Read

func Read(r io.Reader) (Value, error)

func Unpack

func Unpack(buf []byte, copy bool) (Value, error)

func Unseal

func Unseal(encrypted []byte, senderPublicKey, recipientPrivateKey *[32]byte) (Value, error)

type Writer

type Writer interface {
	WriteNil() []byte

	WriteBool(val bool) []byte

	WriteLong(val int64) []byte

	WriteDouble(val float64) []byte

	WriteBinHeader(len int) []byte

	WriteStrHeader(len int) []byte

	WriteExtHeader(len int, xtag byte) []byte

	WriteArrayHeader(len int) []byte

	WriteMapHeader(len int) []byte
}

Jump to

Keyboard shortcuts

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