Documentation ¶
Overview ¶
Package codecapi is used by the codec package and by code generated by codec.GenerateFile. It should NOT be used directly.
Index ¶
- Variables
- func Fail(err error)
- func Failf(format string, args ...interface{})
- func Register(t reflect.Type, tcb func() TypeCodec)
- func TypeString(t reflect.Type, pkgPaths map[string]string) string
- type DecodeOptions
- type Decoder
- func (d *Decoder) Decode(p interface{}) (err error)
- func (d *Decoder) DecodeAny() interface{}
- func (d *Decoder) DecodeBool() bool
- func (d *Decoder) DecodeByte() byte
- func (d *Decoder) DecodeBytes() []byte
- func (d *Decoder) DecodeComplex() complex128
- func (d *Decoder) DecodeFloat() float64
- func (d *Decoder) DecodeInt() int64
- func (d *Decoder) DecodeString() string
- func (d *Decoder) DecodeUint() uint64
- func (d *Decoder) NextStructField(fieldMap []int) int
- func (d *Decoder) StartList() int
- func (d *Decoder) StartPtr() (bool, interface{})
- func (d *Decoder) StartStruct()
- func (d *Decoder) StoreRef(p interface{})
- func (d *Decoder) UnknownField(typeName string)
- type EncodeOptions
- type Encoder
- func (e *Encoder) Encode(x interface{}) (err error)
- func (e *Encoder) EncodeAny(x interface{})
- func (e *Encoder) EncodeBool(b bool)
- func (e *Encoder) EncodeByte(b byte)
- func (e *Encoder) EncodeBytes(b []byte)
- func (e *Encoder) EncodeComplex(c complex128)
- func (e *Encoder) EncodeFloat(f float64)
- func (e *Encoder) EncodeInt(i int64)
- func (e *Encoder) EncodeNil()
- func (e *Encoder) EncodeString(s string)
- func (e *Encoder) EncodeUint(u uint64)
- func (e *Encoder) EndStruct()
- func (e *Encoder) StartList(len int)
- func (e *Encoder) StartPtr(isNil bool, p interface{}) bool
- func (e *Encoder) StartStruct()
- type NonStruct
- type TypeCodec
Constants ¶
This section is empty.
Variables ¶
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 ¶
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
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 ¶
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) DecodeByte ¶
func (*Decoder) DecodeBytes ¶
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 ¶
DecodeFloat decodes a float64.
func (*Decoder) DecodeString ¶
DecodeString decodes a string.
func (*Decoder) NextStructField ¶
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 ¶
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 ¶
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 ¶
UnknownField should be called by a struct decoder when it sees a field number that it doesn't know.
type EncodeOptions ¶
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
func NewEncoder ¶
func NewEncoder(w io.Writer, opts EncodeOptions) *Encoder
func (*Encoder) EncodeAny ¶
func (e *Encoder) EncodeAny(x interface{})
EncodeAny encodes a Go type. The type must have been registered with Register.
func (*Encoder) EncodeByte ¶
func (*Encoder) EncodeBytes ¶
EncodeBytes encodes a byte slice.
func (*Encoder) EncodeComplex ¶
func (e *Encoder) EncodeComplex(c complex128)
EncodeComplex encodes a complex128.
func (*Encoder) EncodeFloat ¶
EncodeFloat encodes a float64.
func (*Encoder) EncodeString ¶
EncodeString encodes a string.
func (*Encoder) EndStruct ¶
func (e *Encoder) EndStruct()
EndStruct should be called after encoding a struct.
func (*Encoder) StartList ¶
StartList should be called before encoding any sequence of variable-length values.
func (*Encoder) StartPtr ¶
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) SetFieldMap ¶ added in v0.4.0
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.