sb

package module
v0.0.0-...-20bc55f Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 22 Imported by: 9 modules, 24 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sb

Data streaming toolbox

Features

  • marshal and unmarshal
  • encode and decode
  • hash
  • compare
  • stream and sink combinators

Demo

package main

import (
	"bytes"
	"crypto/sha256"
	"fmt"
	"github.com/reusee/e5"
	"github.com/reusee/sb"
	"os"
)

func main() {

	// marshal stream
	marshaler := sb.Marshal(42)

	// unmarshal sink
	var num int
	unmarshaler := sb.Unmarshal(&num)

	// copy stream to sink
	check(sb.Copy(
		marshaler,
		unmarshaler,
	))
	must(num == 42)

	// encode
	buf := new(bytes.Buffer)
	check(sb.Copy(
		sb.Marshal(80),
		sb.Encode(buf),
	))

	// decode
	check(sb.Copy(
		sb.Decode(buf),
		sb.Unmarshal(&num),
	))
	must(num == 80)

	// hash
	var sum []byte
	check(sb.Copy(
		sb.Marshal(map[int]string{
			42: "42",
			80: "80",
		}),
		sb.Hash(sha256.New, &sum, nil),
	))
	must(fmt.Sprintf("%x", sum) ==
		"48fc94ae5e9f6961bbf6e85288deab361e9d1bac2d13be8fa20ee4103295d033")

	var tokens sb.Tokens
	check(sb.Copy(
		// stream combinator
		sb.Tee(
			sb.Marshal(42),
			sb.Encode(buf),
		),
		// multiple sinks
		sb.Unmarshal(&num),
		sb.Hash(sha256.New, &sum, nil),
		sb.CollectTokens(&tokens),
	))
	must(num == 42)
	must(fmt.Sprintf("%x", sum) ==
		"151a3a0b4c88483512fc484d0badfedf80013ebb18df498bbee89ac5b69d7222")

	// stream comparison
	res, err := sb.Compare(
		sb.Marshal(42),
		sb.Marshal(map[int]int{42: 42}),
	)
	check(err)
	must(res == -1)

}

var (
	check = e5.Check
	pt    = fmt.Printf
)

func must(b bool) {
	if !b {
		pt(e5.NewStacktrace()(fmt.Errorf("should be true")).Error())
		os.Exit(-1)
	}
}

More examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BadMapKey    = fmt.Errorf("bad map key")
	BadTokenKind = fmt.Errorf("bad token kind")

	UnexpectedEndToken = fmt.Errorf("unexpected end token")
)
View Source
var (
	BadFieldName        = fmt.Errorf("bad field name")
	BadTargetType       = fmt.Errorf("bad target type")
	BadTupleType        = fmt.Errorf("bad tuple type")
	DuplicatedFieldName = fmt.Errorf("duplicated field name")
	TooManyElement      = fmt.Errorf("too many element")
	TooFewElement       = fmt.Errorf("too few element")
	UnknownFieldName    = fmt.Errorf("unknown field name")
)
View Source
var (
	StringTooLong   = fmt.Errorf("string too long")
	BytesTooLong    = fmt.Errorf("bytes too long")
	BadStringLength = fmt.Errorf("bad string length")
)
View Source
var (
	CyclicPointer = fmt.Errorf("cyclic pointer")
)
View Source
var DecodeError = fmt.Errorf("decode error")
View Source
var DefaultCtx = Ctx{
	Marshal:   MarshalValue,
	Unmarshal: UnmarshalValue,
}
View Source
var MarshalError = fmt.Errorf("marshal error")
View Source
var Max = Token{
	Kind: KindMax,
}
View Source
var MaxDecodeStringLength uint64 = 4 * 1024 * 1024 * 1024
View Source
var Min = Token{
	Kind: KindMin,
}
View Source
var (
	MoreThanOneValue = fmt.Errorf("more than one value in stream")
)
View Source
var NaN = Token{
	Kind: KindNaN,
}
View Source
var Nil = Token{
	Kind: KindNil,
}
View Source
var (
	NotFound = fmt.Errorf("not found")
)
View Source
var UnmarshalError = fmt.Errorf("unmarshal error")

Functions

func Compare

func Compare(stream1, stream2 Stream) (int, error)

func CompareBytes

func CompareBytes(a, b []byte) (int, error)

func Copy

func Copy(stream Stream, sinks ...Sink) error

func Fuzz

func Fuzz(data []byte) int

func MustCompare

func MustCompare(stream1, stream2 Stream) int

func MustCompareBytes

func MustCompareBytes(a, b []byte) int

func Register

func Register(t reflect.Type)

func TupleTypes

func TupleTypes(typeSpec any) []reflect.Type

func TypeName

func TypeName(t reflect.Type) (name string)

func WithPath

func WithPath(ctx Ctx) e5.WrapFunc

Types

type Ctx

type Ctx struct {
	Marshal   func(Ctx, reflect.Value, Proc) Proc
	Unmarshal func(Ctx, reflect.Value, Sink) Sink

	// array index, slice index, struct field name, map key, tuple index
	Path Path

	SkipEmptyStructFields       bool
	DisallowUnknownStructFields bool

	IgnoreFuncs bool
	// contains filtered or unexported fields
}

func (Ctx) SkipEmpty

func (c Ctx) SkipEmpty() Ctx

func (Ctx) Strict

func (c Ctx) Strict() Ctx

func (Ctx) WithPath

func (c Ctx) WithPath(path any) Ctx

type ErrUnmarshalTypeMismatch

type ErrUnmarshalTypeMismatch struct {
	TokenKind Kind
	Target    reflect.Kind
}

func TypeMismatch

func TypeMismatch(kind Kind, target reflect.Kind) ErrUnmarshalTypeMismatch

func (ErrUnmarshalTypeMismatch) Error

func (e ErrUnmarshalTypeMismatch) Error() string

type HasDeprecatedFields

type HasDeprecatedFields interface {
	SBDeprecatedFields() []string
}

type Kind

type Kind uint8
const (
	KindInvalid Kind = 0
	KindMin     Kind = 1

	KindArrayEnd  Kind = 10
	KindObjectEnd Kind = 20
	KindMapEnd    Kind = 25
	KindTupleEnd  Kind = 27

	KindNil         Kind = 30
	KindBool        Kind = 40
	KindStringEnd   Kind = 49
	KindString      Kind = 50
	KindStringBegin Kind = 51
	KindBytesEnd    Kind = 54
	KindBytes       Kind = 55
	KindBytesBegin  Kind = 56

	KindInt   Kind = 60
	KindInt8  Kind = 70
	KindInt16 Kind = 80
	KindInt32 Kind = 90
	KindInt64 Kind = 100

	KindUint   Kind = 110
	KindUint8  Kind = 120
	KindUint16 Kind = 130
	KindUint32 Kind = 140
	KindUint64 Kind = 150

	KindFloat32 Kind = 160
	KindFloat64 Kind = 170
	KindNaN     Kind = 175

	KindArray  Kind = 180
	KindObject Kind = 190
	KindMap    Kind = 200
	KindTuple  Kind = 210

	KindTypeName Kind = 230
	KindLiteral  Kind = 240
	KindPointer  Kind = 245

	KindRef Kind = 251

	KindMax Kind = 0xFF
)

func (Kind) Error

func (k Kind) Error() string

func (Kind) String

func (i Kind) String() string

type MapTuple

type MapTuple struct {
	Key       reflect.Value
	Value     reflect.Value
	KeyTokens Tokens
}

type Offset

type Offset int64

func (Offset) Error

func (o Offset) Error() string

type Path

type Path []any

func (Path) Error

func (p Path) Error() string

func (Path) String

func (p Path) String() string

type Proc

type Proc func(token *Token) (Proc, error)

func Decode

func Decode(r io.Reader) *Proc

func DecodeBuffer

func DecodeBuffer(r io.Reader, byteReader io.ByteReader, buf []byte, cont Proc) Proc

func DecodeBufferForCompare

func DecodeBufferForCompare(r io.Reader, byteReader io.ByteReader, buf []byte, cont Proc) Proc

func DecodeForCompare

func DecodeForCompare(r io.Reader) *Proc

func DecodeJson

func DecodeJson(r io.Reader, cont Proc) *Proc

func Deref

func Deref(
	stream Stream,
	getStream func([]byte) (Stream, error),
) *Proc

func FilterProc

func FilterProc(
	stream Stream,
	predict func(*Token) bool,
) *Proc

func IterStream

func IterStream(
	stream Stream,
	cont Proc,
) Proc

func IterSubTrees

func IterSubTrees(
	subs []*Tree,
	index int,
	cont Proc,
) Proc

func IterSubTreesFunc

func IterSubTreesFunc(
	subs []*Tree,
	index int,
	fn func(*Tree) (*Token, error),
	cont Proc,
) Proc

func IterTokens

func IterTokens(
	tokens Tokens,
	index int,
	cont Proc,
) Proc

func IterTree

func IterTree(
	tree *Tree,
	cont Proc,
) Proc

func IterTreeFunc

func IterTreeFunc(
	tree *Tree,
	fn func(*Tree) (*Token, error),
	cont Proc,
) Proc

func Marshal

func Marshal(value any) *Proc

func MarshalArray

func MarshalArray(ctx Ctx, value reflect.Value, index int, cont Proc) Proc

func MarshalAsTuple

func MarshalAsTuple(ctx Ctx, tuple []any, cont Proc) Proc

func MarshalCtx

func MarshalCtx(ctx Ctx, value any) *Proc

func MarshalMap

func MarshalMap(ctx Ctx, value reflect.Value, cont Proc) Proc

func MarshalMapIter

func MarshalMapIter(ctx Ctx, iter *reflect.MapIter, tuples []*MapTuple, cont Proc) Proc

func MarshalMapTuples

func MarshalMapTuples(ctx Ctx, tuples []*MapTuple, cont Proc) Proc

func MarshalStruct

func MarshalStruct(ctx Ctx, value reflect.Value, cont Proc) Proc

func MarshalStructFields

func MarshalStructFields(ctx Ctx, value reflect.Value, cont Proc) Proc

func MarshalTuple

func MarshalTuple(ctx Ctx, items []reflect.Value, cont Proc) Proc

func MarshalValue

func MarshalValue(ctx Ctx, value reflect.Value, cont Proc) Proc

func TapMarshal

func TapMarshal(ctx Ctx, value any, fn func(Ctx, reflect.Value)) *Proc

func Tee

func Tee(stream Stream, sinks ...Sink) *Proc

func TeeProc

func TeeProc(stream Stream, sinks []Sink, cont Proc) Proc

func (Proc) MarshalSB

func (p Proc) MarshalSB(ctx Ctx, cont Proc) Proc

func (*Proc) Next

func (p *Proc) Next(token *Token) (err error)

type Ref

type Ref []byte

func (Ref) MarshalSB

func (r Ref) MarshalSB(ctx Ctx, cont Proc) Proc

func (*Ref) UnmarshalSB

func (r *Ref) UnmarshalSB(ctx Ctx, cont Sink) Sink

type SBMarshaler

type SBMarshaler interface {
	MarshalSB(ctx Ctx, cont Proc) Proc
}

type SBUnmarshaler

type SBUnmarshaler interface {
	UnmarshalSB(ctx Ctx, cont Sink) Sink
}

type Sink

type Sink func(*Token) (Sink, error)

func AltSink

func AltSink(sinks ...Sink) Sink

func CollectTokens

func CollectTokens(tokens *Tokens) Sink

func CollectValueTokens

func CollectValueTokens(tokens *Tokens) Sink

func ConcatSinks

func ConcatSinks(sinks ...Sink) Sink

func Discard

func Discard(token *Token) (Sink, error)

func Encode

func Encode(w io.Writer) Sink

func EncodeBuffer

func EncodeBuffer(w io.Writer, buf []byte, cont Sink) Sink

func EncodedLen

func EncodedLen(ret *int, cont Sink) Sink

func ExpectKind

func ExpectKind(ctx Ctx, kind Kind, cont Sink) Sink

func FilterSink

func FilterSink(sink Sink, fn func(*Token) bool) Sink

func Hash

func Hash(
	newState func() hash.Hash,
	target *[]byte,
	cont Sink,
) Sink

func HashCompound

func HashCompound(
	newState func() hash.Hash,
	state hash.Hash,
	fn func([]byte, *Token) error,
	cont Sink,
) Sink

func HashFunc

func HashFunc(
	newState func() hash.Hash,
	target *[]byte,
	fn func([]byte, *Token) error,
	cont Sink,
) Sink

func TapUnmarshal

func TapUnmarshal(ctx Ctx, target any, fn func(Ctx, Token, reflect.Value)) Sink

func Unmarshal

func Unmarshal(target any) Sink

func UnmarshalArray

func UnmarshalArray(
	ctx Ctx,
	target reflect.Value,
	cont Sink,
) Sink

func UnmarshalGenericMap

func UnmarshalGenericMap(
	ctx Ctx,
	target reflect.Value,
	cont Sink,
) Sink

func UnmarshalGenericSlice

func UnmarshalGenericSlice(
	ctx Ctx,
	target reflect.Value,
	cont Sink,
) Sink

func UnmarshalMap

func UnmarshalMap(
	ctx Ctx,
	target reflect.Value,
	valueType reflect.Type,
	cont Sink,
) Sink

func UnmarshalNewStruct

func UnmarshalNewStruct(
	ctx Ctx,
	target reflect.Value,
	cont Sink,
) Sink

func UnmarshalSlice

func UnmarshalSlice(
	ctx Ctx,
	target reflect.Value,
	valueType reflect.Type,
	cont Sink,
) Sink

func UnmarshalStruct

func UnmarshalStruct(
	ctx Ctx,
	target reflect.Value,
	valueType reflect.Type,
	cont Sink,
) Sink

func UnmarshalTuple

func UnmarshalTuple(
	ctx Ctx,
	target reflect.Value,
	valueType reflect.Type,
	cont Sink,
) Sink

func UnmarshalTupleTyped

func UnmarshalTupleTyped(ctx Ctx, types []reflect.Type, target *Tuple, cont Sink) Sink

func UnmarshalValue

func UnmarshalValue(ctx Ctx, target reflect.Value, cont Sink) Sink

func (Sink) Marshal

func (s Sink) Marshal(o any) (Sink, error)

func (Sink) Sink

func (s Sink) Sink(token *Token) (Sink, error)

func (Sink) UnmarshalSB

func (s Sink) UnmarshalSB(ctx Ctx, cont Sink) Sink

type Stream

type Stream = *Proc

func ConcatStreams

func ConcatStreams(streams ...Stream) Stream

func FindByHash

func FindByHash(
	stream Stream,
	hash []byte,
	newState func() hash.Hash,
) (
	subStream Stream,
	err error,
)

type TapTree

type TapTree struct {
	Func func(*Tree)
}

func (TapTree) IsTreeOption

func (TapTree) IsTreeOption()

type Token

type Token struct {
	Value any
	Kind  Kind
}

func (*Token) Invalid

func (t *Token) Invalid() bool

func (Token) MarshalSB

func (t Token) MarshalSB(ctx Ctx, cont Proc) Proc

func (*Token) Reset

func (t *Token) Reset()

func (*Token) UnmarshalSB

func (t *Token) UnmarshalSB(ctx Ctx, cont Sink) Sink

func (*Token) Valid

func (t *Token) Valid() bool

type Tokens

type Tokens []Token

func MustTokensFromStream

func MustTokensFromStream(stream Stream) Tokens

func TokensFromStream

func TokensFromStream(stream Stream) (tokens Tokens, err error)

func (Tokens) Iter

func (t Tokens) Iter() *Proc

type Tree

type Tree struct {
	*Token
	Paired *Tree
	Hash   []byte
	Subs   []*Tree
}

func MustTreeFromStream

func MustTreeFromStream(stream Stream, options ...TreeOption) *Tree

func TreeFromStream

func TreeFromStream(
	stream Stream,
	options ...TreeOption,
) (*Tree, error)

func (*Tree) FillHash

func (t *Tree) FillHash(
	newState func() hash.Hash,
) (
	err error,
)

func (*Tree) Iter

func (t *Tree) Iter() *Proc

func (*Tree) IterFunc

func (t *Tree) IterFunc(
	fn func(*Tree) (*Token, error),
) *Proc

type TreeOption

type TreeOption interface {
	IsTreeOption()
}

type Tuple

type Tuple []any

func (Tuple) MarshalSB

func (t Tuple) MarshalSB(ctx Ctx, cont Proc) Proc

func (*Tuple) UnmarshalSB

func (t *Tuple) UnmarshalSB(ctx Ctx, cont Sink) Sink

type TypedTuple

type TypedTuple struct {
	Types  []reflect.Type
	Values Tuple
}

func (*TypedTuple) UnmarshalSB

func (t *TypedTuple) UnmarshalSB(ctx Ctx, cont Sink) Sink

type UnmarshalFunc

type UnmarshalFunc func(Ctx, Sink) Sink

func (UnmarshalFunc) UnmarshalSB

func (f UnmarshalFunc) UnmarshalSB(ctx Ctx, cont Sink) Sink

type WithHash

type WithHash struct {
	NewHashState func() hash.Hash
}

func (WithHash) IsTreeOption

func (WithHash) IsTreeOption()

Jump to

Keyboard shortcuts

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