package codec
import "golang.org/x/pkgsite/internal/godoc/codec"
Package codec implements the general-purpose part of an encoder for Go values. It relies on code generation rather than reflection so it is significantly faster than reflection-based encoders like gob. It also preserves sharing among struct pointers (but not other forms of sharing, like other pointer types or sub-slices). These features are sufficient for encoding the structures of the go/ast package, which is its sole purpose.
Encoding Scheme
Every encoded value begins with a single byte that describes what (if anything) follows. A value's encoding contains enough information to skip over the value, since the decoder must be able to do that if it encounters a struct field it doesn't know.
Most of the values of that initial byte can be devoted to small unsigned integers. For example, the number 17 is represented by the single byte 17. Only a few byte values have special meanings, whose descriptions follow.
The nil code indicates that the value is nil. We don't absolutely need this: we could always represent the nil value for a type as something that couldn't be mistaken for an encoded value of that type. For instance, we could use 0 for nil in the case of slices (which always begin with the nValues code), and for pointers to numbers like *int, we could use something like "nBytes 0". But it is simpler to have a reserved value for nil.
The nBytes code indicates that an unsigned integer N is encoded next, followed by N bytes of data. This is used to represent strings and byte slices, as well numbers bigger than can fit into the initial byte. For example, the string "hi" is represented as:
nBytes 2 'h' 'i'
Unsigned integers that can't fit into the initial byte are encoded as byte sequences of length 4 or 8, holding little-endian uint32 or uint64 values. We use uint32s where possible to save space. For example, 255 is encoded as
nBytes 4 0 0 0 255
This representation is not as space-efficient as others, but improving its space usage didn't seem worth the additional complexity.
Signed integers use gob's encoding.
Floats are encoded by converting them to uints using math.Float64bits.
The nValues code is for sequences of values whose size is known beforehand, like a Go slice or array. The slice []string{"hi", "bye"} is encoded as
nValues 2 nBytes 2 'h' 'i' nBytes 3 'b' 'y' 'e'
The ref code is used to refer to an earlier encoded value. It is followed by a uint denoting the index data of the value to use.
The start and end codes delimit a value whose length is unknown beforehand. They are used for structs.
A struct is encoded as a sequence of fields between a start code and an end code. Each exported field is encoded as an integer field number followed by the field's value. Fields with zero values are omitted.
The field number is initially the position of the field in the struct declaration. This initial ordering is preserved even if fields are added or removed; new fields are numbered after the initial ones. This is accomplished by preserving the field ordering in a comment in the generated code. For example, say a struct S has fields A and B. The generated encoder would assign 0 to A and 1 to B, and include this comment in the code:
// Fields of S: A B
If later a field X was added between A and B, the generator would read the comment and preserve the original assignments of A and B. It would assign 2 to X, and the generated code would contain the new comment
// Fields of S: A B X
Values of type any are encoded as a pair of an assigned type number and the value. All types that can appear as values must be registered by calling Register. Type numbers are assigned to type names by the encoder in the order that types are encountered. The assignments are saved at the beginning of the encoded data.
Index ¶
- func GenerateFile(filename, packageName string, values ...any) error
- func Register(x any, enc encodeFunc, dec decodeFunc)
- type Decoder
- func NewDecoder(data []byte) *Decoder
- func (d *Decoder) Decode() (_ any, err error)
- func (d *Decoder) DecodeAny() any
- func (d *Decoder) DecodeBool() bool
- func (d *Decoder) DecodeBytes() []byte
- func (d *Decoder) DecodeFloat() float64
- func (d *Decoder) DecodeInt() int64
- func (d *Decoder) DecodeString() string
- func (d *Decoder) DecodeUint() uint64
- func (d *Decoder) NextStructField() int
- func (d *Decoder) StartList() int
- func (d *Decoder) StartStruct() (bool, any)
- func (d *Decoder) StoreRef(p any)
- func (d *Decoder) UnknownField(typeName string, num int)
- type Encoder
- func NewEncoder() *Encoder
- func (e *Encoder) Bytes() []byte
- func (e *Encoder) Encode(x any) (err error)
- func (e *Encoder) EncodeAny(x any)
- func (e *Encoder) EncodeBool(b bool)
- func (e *Encoder) EncodeBytes(b []byte)
- 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) StartStruct(isNil bool, p any) bool
Functions ¶
func GenerateFile ¶
GenerateFile writes encoders and decoders to filename. It generates code for the type of each given value, as well as any types they depend on. packageName is the name following the file's package declaration.
func Register ¶
func Register(x any, enc encodeFunc, dec decodeFunc)
Register records the type of x for use by Encoders and Decoders.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder decodes a Go value encoded by an Encoder. To use a Decoder: - Pass NewDecoder the return value of Encoder.Bytes. - Call the Decode method once for each call to Encoder.Encode.
func NewDecoder ¶
NewDecoder returns a Decoder for the given bytes.
func (*Decoder) Decode ¶
Decode decodes a value encoded with Encoder.Encode.
func (*Decoder) DecodeAny ¶
DecodeAny decodes a value encoded by EncodeAny.
func (*Decoder) DecodeBool ¶
DecodeBool decodes a bool.
func (*Decoder) DecodeBytes ¶
DecodeBytes decodes a byte slice. It does no copying.
func (*Decoder) DecodeFloat ¶
DecodeFloat decodes a float64.
func (*Decoder) DecodeInt ¶
DecodeInt decodes a signed integer.
func (*Decoder) DecodeString ¶
DecodeString decodes a string.
func (*Decoder) DecodeUint ¶
DecodeUint decodes a uint64.
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) StartStruct ¶
StartStruct should be called before decoding a struct pointer. If it returns false, decoding should not proceed. If it returns true and the second return value is non-nil, it is a reference to a previous value and should be used instead of proceeding with decoding.
func (*Decoder) StoreRef ¶
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 Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder encodes Go values into a sequence of bytes. To use an Encoder: - Create one with NewEncoder. - Call the Encode method one or more times. - Retrieve the resulting bytes by calling Bytes.
func NewEncoder ¶
func NewEncoder() *Encoder
NewEncoder returns an Encoder.
func (*Encoder) Bytes ¶
Bytes returns the encoded byte slice.
func (*Encoder) Encode ¶
Encode encodes x.
func (*Encoder) EncodeAny ¶
EncodeAny encodes a Go type. The type must have been registered with Register.
func (*Encoder) EncodeBool ¶
EncodeBool encodes a bool.
func (*Encoder) EncodeBytes ¶
EncodeBytes encodes a byte slice.
func (*Encoder) EncodeFloat ¶
EncodeFloat encodes a float64.
func (*Encoder) EncodeInt ¶
EncodeInt encodes a signed integer.
func (*Encoder) EncodeNil ¶
func (e *Encoder) EncodeNil()
func (*Encoder) EncodeString ¶
EncodeString encodes a string.
func (*Encoder) EncodeUint ¶
EncodeUint encodes a uint64.
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) StartStruct ¶
StartStruct should be called before encoding a struct pointer. The isNil argument says whether the pointer is nil. The p argument is the struct pointer. If StartStruct returns false, encoding should not proceed.
Source Files ¶
codec.go doc.go generate.go
- Version
- v0.0.0-20250218150137-224a1368cf02 (latest)
- Published
- Feb 18, 2025
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 2 months ago –
Tools for package owners.