codecapi

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package codecapi is used by the codec package and by code generated by codec.GenerateFile. It should NOT be used directly.

Index

Constants

This section is empty.

Variables

View Source
var BuiltinTypes []reflect.Type

Functions

func Fail

func Fail(err error)

Fail aborts the current encoding or decoding with the given error. It never returns.

func Failf

func Failf(format string, args ...interface{})

Failf calls fmt.Errorf with the given arguments, then Fail. It never returns.

func Register

func Register(t reflect.Type, tcb func() TypeCodec)

Register records t for use by Encoders and Decoders. All types subject to encoding must be registered, even builtin types.

func TypeString added in v0.4.0

func TypeString(t reflect.Type, pkgPaths map[string]string) string

TypeString constructs a string from a reflect.Type.

If pkgPaths is nil, then the returned string uses fully qualified package paths, and the result is unique to the the type provided that the type is not defined inside a function. (reflect.Type provides no way to distinguish such a type from another, identically named type at top level.)

Otherwise, pkgPaths is used to determine what to emit for a fully-qualified package path. If the path is not found in the map, then its last component is used. In this mode, TypeString generates valid Go type expressions, provided the path mapping corresponds to the context of the generated code; that is, the current package's path is mapped to the empty string, and other packages are mapped to their import identifiers in the file.

Types

type DecodeOptions

type DecodeOptions struct {
	DisallowUnknownFields bool
}

type Decoder

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

func NewDecoder

func NewDecoder(r io.Reader, opts DecodeOptions) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(p interface{}) (err error)

Decode decodes a value encoded with Encoder.Encode and stores the result in the value pointed to by p. The decoded value must be assignable to the pointee's type; no conversions are performed. Decode returns io.EOF if there are no more values.

func (*Decoder) DecodeAny

func (d *Decoder) DecodeAny() interface{}

DecodeAny decodes a value encoded by EncodeAny.

func (*Decoder) DecodeBool

func (d *Decoder) DecodeBool() bool

DecodeBool decodes a bool.

func (*Decoder) DecodeByte

func (d *Decoder) DecodeByte() byte

func (*Decoder) DecodeBytes

func (d *Decoder) DecodeBytes() []byte

DecodeBytes decodes a byte slice. It makes a copy of a portion of the underlying buffer.

func (*Decoder) DecodeComplex

func (d *Decoder) DecodeComplex() complex128

DecodeComplex decodes a complex128.

func (*Decoder) DecodeFloat

func (d *Decoder) DecodeFloat() float64

DecodeFloat decodes a float64.

func (*Decoder) DecodeInt

func (d *Decoder) DecodeInt() int64

DecodeInt decodes a signed integer.

func (*Decoder) DecodeString

func (d *Decoder) DecodeString() string

DecodeString decodes a string.

func (*Decoder) DecodeUint

func (d *Decoder) DecodeUint() uint64

DecodeUint decodes a uint64.

func (*Decoder) NextStructField

func (d *Decoder) NextStructField(fieldMap []int) int

NextStructField should be called by a struct decoder in a loop. It returns the field number of the next encoded field, or -1 if there are no more fields.

func (*Decoder) StartList

func (d *Decoder) StartList() int

StartList should be called before decoding any sequence of variable-length values. It returns -1 if the encoded list was nil. Otherwise, it returns the length of the sequence.

func (*Decoder) StartPtr

func (d *Decoder) StartPtr() (bool, interface{})

StartPtr should be called before decoding a pointer. If the first return value is false, the destination should not be set. Otherwise, if the second return value is non-nil, assign it to the destination. Otherwise, proceed with decoding into the destination.

func (*Decoder) StartStruct

func (d *Decoder) StartStruct()

StartStruct should be called before decoding a struct.

func (*Decoder) StoreRef

func (d *Decoder) StoreRef(p interface{})

StoreRef should be called by a struct decoder immediately after it allocates a struct pointer.

func (*Decoder) UnknownField

func (d *Decoder) UnknownField(typeName string)

UnknownField should be called by a struct decoder when it sees a field number that it doesn't know.

type EncodeOptions

type EncodeOptions struct {
	TrackPointers bool
	Buffer        []byte
}

type Encoder

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

func NewEncoder

func NewEncoder(w io.Writer, opts EncodeOptions) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(x interface{}) (err error)

Encode encodes x.

func (*Encoder) EncodeAny

func (e *Encoder) EncodeAny(x interface{})

EncodeAny encodes a Go type. The type must have been registered with Register.

func (*Encoder) EncodeBool

func (e *Encoder) EncodeBool(b bool)

EncodeBool encodes a bool.

func (*Encoder) EncodeByte

func (e *Encoder) EncodeByte(b byte)

func (*Encoder) EncodeBytes

func (e *Encoder) EncodeBytes(b []byte)

EncodeBytes encodes a byte slice.

func (*Encoder) EncodeComplex

func (e *Encoder) EncodeComplex(c complex128)

EncodeComplex encodes a complex128.

func (*Encoder) EncodeFloat

func (e *Encoder) EncodeFloat(f float64)

EncodeFloat encodes a float64.

func (*Encoder) EncodeInt

func (e *Encoder) EncodeInt(i int64)

EncodeInt encodes a signed integer.

func (*Encoder) EncodeNil

func (e *Encoder) EncodeNil()

func (*Encoder) EncodeString

func (e *Encoder) EncodeString(s string)

EncodeString encodes a string.

func (*Encoder) EncodeUint

func (e *Encoder) EncodeUint(u uint64)

EncodeUint encodes a uint64.

func (*Encoder) EndStruct

func (e *Encoder) EndStruct()

EndStruct should be called after encoding a struct.

func (*Encoder) StartList

func (e *Encoder) StartList(len int)

StartList should be called before encoding any sequence of variable-length values.

func (*Encoder) StartPtr

func (e *Encoder) StartPtr(isNil bool, p interface{}) bool

StartPtr should be called before encoding a pointer. The isNil argument says whether the pointer is nil. The p argument is the pointer. If StartPtr returns false, encoding should not proceed.

func (*Encoder) StartStruct

func (e *Encoder) StartStruct()

type NonStruct added in v0.4.0

type NonStruct struct{}

NonStruct defines TypeCodec methods that don't apply to non-struct types. It is intended to be embedded in TypeCodecs for such types.

func (NonStruct) Fields added in v0.4.0

func (NonStruct) Fields() []string

func (NonStruct) SetFieldMap added in v0.4.0

func (NonStruct) SetFieldMap([]int)

type TypeCodec added in v0.4.0

type TypeCodec interface {
	Fields() []string           // the names of all the struct fields, if this is a struct
	TypesUsed() []reflect.Type  // all the types this codec uses, not including itself
	SetCodecs([]TypeCodec)      // a codec for each element of TypesUsed
	SetFieldMap(fieldMap []int) // mapping from from encoded to generated struct fields
	Encode(*Encoder, interface{})
	Decode(*Decoder) interface{}
}

A TypeCodec handles encoding and decoding of a particular type.

Jump to

Keyboard shortcuts

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