Documentation
¶
Overview ¶
Package gobspect is a decode-only introspection library for Go's encoding/gob wire format. It reads arbitrary gob streams without requiring the original Go types, producing a structured Value AST and human-readable output via Format.
Basic usage ¶
ins := gobspect.New()
stream := ins.Stream(r)
for v, err := range stream.Values() {
if err != nil {
log.Fatal(err)
}
fmt.Println(gobspect.Format(v))
}
// stream.Types() now contains every type encountered
To collect all values at once:
values, err := ins.Stream(r).Collect()
Value AST ¶
Stream.Values returns an iterator over Value nodes. Each Value is one of the concrete node types listed below; use a type switch to dispatch on them:
- StructValue — a gob struct with named Field entries
- MapValue — a gob map with MapEntry key/value pairs
- SliceValue — a gob slice
- ArrayValue — a gob array
- IntValue, UintValue, FloatValue, ComplexValue — numeric scalars
- BoolValue — a boolean
- StringValue — a string
- BytesValue — a []byte
- InterfaceValue — an interface value carrying a concrete type name and inner Value
- NilValue — a nil pointer or nil interface
- OpaqueValue — raw bytes produced by GobEncoder, BinaryMarshaler, or TextMarshaler, with an optional best-effort decoded form in the Decoded field
Type metadata ¶
Stream.Types returns TypeInfo for every type definition in the stream as the stream is consumed. By the time a value is yielded by Stream.Values, all type definitions it references are already present in Stream.Types and all [TypeRef.Name] fields are resolved. Stream.TypeByID provides O(1) lookup by stream-scoped type ID.
Stream.Schema drains the stream and returns all type definitions formatted as a Schema.
Looking up types during iteration ¶
stream := ins.Stream(r)
for v, err := range stream.Values() {
if err != nil { log.Fatal(err) }
sv, ok := v.(gobspect.StructValue)
if !ok { continue }
ti, ok := stream.TypeByID(sv.TypeID())
if ok {
fmt.Printf("type %s has %d fields\n", ti.Name, len(ti.Fields))
}
}
Opaque types ¶
Types that implement GobEncoder, BinaryMarshaler, or TextMarshaler are represented as OpaqueValue. New pre-registers decoders for common types:
- time.Time (encoded as RFC 3339 with nanosecond precision)
- math/big.Int and math/big.Rat (auto-detected by wire format)
- math/big.Float (registered under "math/big.Float")
- UUID types from github.com/google/uuid and github.com/gofrs/uuid
- github.com/shopspring/decimal.Decimal
Additional decoders can be registered with Inspector.RegisterDecoder. TextMarshaler blobs are always decoded as UTF-8 strings automatically.
Formatting ¶
Format renders any Value as a human-readable string. FormatTo writes the same output to an io.Writer and propagates write errors. Structs are always indented; maps, slices, and arrays are inlined when short (≤72 chars) and indented otherwise. Map entries are sorted by key for deterministic output. Use WithIndent, WithMaxBytes, and WithRawOpaques to adjust the output.
Decoding limits ¶
Pass WithReadLimit to New to cap total bytes read from a stream:
ins := gobspect.New(gobspect.WithReadLimit(10 << 20)) // 10 MiB
Path-based navigation ¶
The companion subpackage github.com/codepuke/gobspect/query provides path-based navigation of decoded Value trees. Use query.AllPathSeq for lazy, streaming enumeration of matching values (early-break safe), or query.AllPath to collect all matches into a slice.
Index ¶
- Variables
- func CompareValues(a, b Value) int
- func CompareValuesFold(a, b Value) int
- func Equal(a, b Value) bool
- func Format(v Value, opts ...FormatOption) string
- func FormatBytes(b []byte, format BytesFormat, maxBytes int) string
- func FormatTo(w io.Writer, v Value, opts ...FormatOption) error
- func ToJSON(v Value) ([]byte, error)
- func ToJSONIndent(v Value, prefix, indent string) ([]byte, error)
- func ValueKind(v Value) string
- type ArrayValue
- type BoolValue
- type BytesFormat
- type BytesValue
- type ColorScheme
- type ComplexValue
- type DecoderFunc
- type Field
- type FieldDecl
- type FieldInfo
- type FloatValue
- type FormatOption
- func WithBytesFormat(f BytesFormat) FormatOption
- func WithColor(scheme ColorScheme) FormatOption
- func WithIndent(indent string) FormatOption
- func WithInlineWidth(n int) FormatOption
- func WithMapOrder(order MapOrder) FormatOption
- func WithMaxBytes(n int) FormatOption
- func WithRawOpaques(raw bool) FormatOption
- func WithRedactKeys(cfg RedactConfig) FormatOption
- func WithRedactTypes(cfg RedactTypesConfig) FormatOption
- type Inspector
- type IntValue
- type InterfaceValue
- type MapEntry
- type MapOrder
- type MapValue
- type MessageInfo
- type NilValue
- type OpaqueValue
- type Option
- type RedactConfig
- type RedactTypesConfig
- type Schema
- func (s *Schema) Format(opts ...SchemaFormatOption) string
- func (s *Schema) FormatTo(w io.Writer, opts ...SchemaFormatOption) error
- func (s *Schema) JSON() ([]byte, error)
- func (s *Schema) JSONIndent(prefix, indent string) ([]byte, error)
- func (s *Schema) String() string
- func (s *Schema) TypeByName(name string) (*TypeDecl, bool)
- type SchemaFormatOption
- type SliceValue
- type Stats
- type Stream
- func (s *Stream) Collect() ([]Value, error)
- func (s *Stream) Messages() iter.Seq2[MessageInfo, error]
- func (s *Stream) Schema() (*Schema, error)
- func (s *Stream) SkipCount() int
- func (s *Stream) Stats() (*Stats, error)
- func (s *Stream) TypeByID(id int) (TypeInfo, bool)
- func (s *Stream) Types() []TypeInfo
- func (s *Stream) Values() iter.Seq2[Value, error]
- type StringValue
- type StructValue
- type Style
- type TypeDecl
- type TypeInfo
- type TypeKind
- type TypeRef
- type TypeStats
- type UintValue
- type Value
Constants ¶
This section is empty.
Variables ¶
var ANSIColorScheme = ColorScheme{ FieldName: Style{Prefix: "\x1b[32m", Suffix: "\x1b[0m"}, TypeHeader: Style{Prefix: "\x1b[1;36m", Suffix: "\x1b[0m"}, CloseBrace: Style{}, String: Style{Prefix: "\x1b[33m", Suffix: "\x1b[0m"}, Number: Style{Prefix: "\x1b[35m", Suffix: "\x1b[0m"}, Bool: Style{Prefix: "\x1b[36m", Suffix: "\x1b[0m"}, Nil: Style{Prefix: "\x1b[36m", Suffix: "\x1b[0m"}, OpaquePrefix: Style{Prefix: "\x1b[2m", Suffix: "\x1b[0m"}, OpaqueValue: Style{}, Bytes: Style{Prefix: "\x1b[2m", Suffix: "\x1b[0m"}, }
ANSIColorScheme is a pre-built ColorScheme that uses ANSI escape codes to produce syntax-highlighted terminal output. The colors match the coloring used by cmd/gq.
var NoColorScheme = ColorScheme{}
NoColorScheme is a zero-valued ColorScheme that produces no color output.
Functions ¶
func CompareValues ¶
CompareValues returns -1, 0, or +1 ordering a before, equal to, or after b. InterfaceValue is unwrapped from both sides before dispatch. Same-kind numerics: Int vs Int uses int64, Uint vs Uint uses uint64. Cross-numeric comparisons use float64; large integer values near the limits of float64 precision may not compare correctly. Composite types (struct, map, slice, array) fall back to Format output.
func CompareValuesFold ¶
CompareValuesFold is identical to CompareValues except strings are compared case-insensitively using strings.ToLower. It does not handle all Unicode case equivalences (e.g. ß vs SS).
func Equal ¶ added in v0.2.0
Equal reports whether a and b are structurally equal Values. Equality is strict: the two kinds must match, composite shapes must line up exactly, and primitives compare by native value. Cross-kind numeric equivalence (e.g. IntValue{5} vs FloatValue{5}) returns false — use CompareValues if you need the permissive numeric coercion.
InterfaceValue wrappers are unwrapped on both sides before comparison. An InterfaceValue's outer TypeName does not participate in equality: only the inner concrete value matters.
func Format ¶
func Format(v Value, opts ...FormatOption) string
Format renders v as a human-readable string. Structs are always rendered as indented field trees. Maps, slices, and arrays are rendered inline when their formatted length fits within the inline width (default 72, overridden by WithInlineWidth) and none of their elements require multi-line rendering; otherwise they are indented. Map entries are sorted by formatted key for deterministic output.
func FormatBytes ¶
func FormatBytes(b []byte, format BytesFormat, maxBytes int) string
FormatBytes encodes b using the given BytesFormat, truncating to maxBytes raw bytes before encoding when maxBytes > 0 and len(b) exceeds it. A '…' suffix is appended when truncation occurs. An empty slice always returns "[]".
The printable-UTF-8 shortcut used by Format for BytesValue is intentionally absent here; the requested format is always applied.
func FormatTo ¶
func FormatTo(w io.Writer, v Value, opts ...FormatOption) error
FormatTo renders v as a human-readable string and writes it to w. Structs are always rendered as indented field trees. Maps, slices, and arrays are rendered inline when their formatted length fits within the inline width (default 72, overridden by WithInlineWidth) and none of their elements require multi-line rendering; otherwise they are indented. Map entries are sorted by formatted key for deterministic output. The first write error aborts rendering and is returned.
func ToJSON ¶
ToJSON serializes a Value as a discriminated-union JSON object. Every node carries a "kind" field with the concrete type name in lowercase snake_case. See the package documentation for the full field mapping per kind.
func ToJSONIndent ¶
ToJSONIndent is like ToJSON but applies indentation. prefix and indent follow the same semantics as encoding/json.MarshalIndent.
Types ¶
type ArrayValue ¶
ArrayValue represents a decoded gob array.
func (ArrayValue) TypeID ¶
func (v ArrayValue) TypeID() int
type BytesFormat ¶
type BytesFormat int
BytesFormat controls how raw byte slices are rendered by Format.
const ( BytesHex BytesFormat = iota // lowercase hex string (default) BytesBase64 // standard base64 (RFC 4648, with padding) BytesLiteral // Go-style: []byte{0xde, 0xad, ...} )
func ParseBytesFormat ¶
func ParseBytesFormat(s string) (BytesFormat, bool)
ParseBytesFormat converts a string to a BytesFormat constant. Accepts "hex", "base64", or "literal" (case-insensitive). An empty string maps to BytesHex. Returns (BytesHex, false) for any unrecognised value.
type BytesValue ¶
type BytesValue struct{ V []byte }
BytesValue holds a decoded []byte.
func (BytesValue) TypeID ¶
func (BytesValue) TypeID() int
type ColorScheme ¶
type ColorScheme struct {
FieldName Style // struct field names, map keys for string-keyed maps
TypeHeader Style // struct/map/slice/array type name ("Foo", "[]int", "map[string]int")
CloseBrace Style // "{" and "}" — opening and closing braces
String Style // quoted string values
Number Style // int, uint, float, complex
Bool Style // true, false
Nil Style // nil
OpaquePrefix Style // "(TypeName)" marker
OpaqueValue Style // the rendered opaque value after the prefix
Bytes Style // hex / base64 / literal byte output
}
ColorScheme controls how different token roles are wrapped during rendering. A zero-valued ColorScheme produces no change to the output (identity).
type ComplexValue ¶
type ComplexValue struct{ Real, Imag float64 }
ComplexValue holds a decoded complex number.
func (ComplexValue) TypeID ¶
func (ComplexValue) TypeID() int
type DecoderFunc ¶
DecoderFunc decodes the raw bytes of a GobEncoder, BinaryMarshaler, or TextMarshaler blob into a human-meaningful value.
The returned value should be a simple Go type (string, int, float, map, etc.) suitable for display. It does not need to reconstruct the original Go type.
type FloatValue ¶
type FloatValue struct{ V float64 }
FloatValue holds a decoded floating-point number.
func (FloatValue) TypeID ¶
func (FloatValue) TypeID() int
type FormatOption ¶
type FormatOption func(*formatConfig)
FormatOption configures the behavior of Format.
func WithBytesFormat ¶
func WithBytesFormat(f BytesFormat) FormatOption
WithBytesFormat controls how BytesValue and [OpaqueValue.Raw] are rendered. When set explicitly, the printable UTF-8 shortcut (render as a quoted string) is suppressed and the requested format is always used.
func WithColor ¶
func WithColor(scheme ColorScheme) FormatOption
WithColor applies the given ColorScheme to the rendered output. Each token role is wrapped with its corresponding Style's Prefix and Suffix. A zero-valued scheme (NoColorScheme) is the identity: no escape sequences are emitted and the output is byte-identical to calling Format without this option.
func WithIndent ¶
func WithIndent(indent string) FormatOption
WithIndent sets the indentation string used for nested output. Default: " ".
func WithInlineWidth ¶
func WithInlineWidth(n int) FormatOption
WithInlineWidth sets the maximum character count at which maps, slices, and arrays are rendered inline rather than indented. The plain-text (no-color) length of the full inline form is compared against n. Default: 72. Pass 0 to use the default explicitly.
func WithMapOrder ¶
func WithMapOrder(order MapOrder) FormatOption
WithMapOrder sets the ordering of map entries in Format output. The default (MapOrderSorted) sorts entries by their plain-text formatted key for deterministic output. MapOrderInsertion skips sorting and iterates entries in the order they appear in [MapValue.Entries] (wire order).
func WithMaxBytes ¶
func WithMaxBytes(n int) FormatOption
WithMaxBytes sets the maximum number of raw bytes rendered for OpaqueValue and BytesValue output. Default: 64. Zero means no limit. Applies to all byte formats (hex, base64, literal): the byte slice is truncated before encoding when this limit is exceeded.
func WithRawOpaques ¶
func WithRawOpaques(raw bool) FormatOption
WithRawOpaques controls whether decoded [OpaqueValue]s still show their raw bytes. When true, the raw bytes are always shown even when [OpaqueValue.Decoded] is set.
func WithRedactKeys ¶
func WithRedactKeys(cfg RedactConfig) FormatOption
WithRedactKeys redacts the rendered value of any struct field or map entry whose key matches one of cfg.Keys (case-sensitive exact match). The value is replaced at render time; the AST is never modified.
Key matching for struct fields is by field name. For map entries, matching is by the formatted key string (the result of rendering the key through Format). Glob and regex matching are intentionally out of scope.
func WithRedactTypes ¶
func WithRedactTypes(cfg RedactTypesConfig) FormatOption
WithRedactTypes redacts all values whose type name matches one of the names in cfg.Types. Checked against [StructValue.TypeName], [InterfaceValue.TypeName], and [OpaqueValue.TypeName]. May be combined with WithRedactKeys; both rules apply. cfg.Char and cfg.TextLength control the fill character and output length, identical to their meaning in RedactConfig.
type Inspector ¶
type Inspector struct {
// contains filtered or unexported fields
}
Inspector is the top-level entry point. It holds the opaque decoder registry and decoding options. Create one with New.
func (*Inspector) RegisterDecoder ¶
func (ins *Inspector) RegisterDecoder(typeName string, dec DecoderFunc)
RegisterDecoder adds or overrides the opaque decoder for the given type name.
func (*Inspector) RegisterUnnamedDecoder ¶
func (ins *Inspector) RegisterUnnamedDecoder(dec DecoderFunc)
RegisterUnnamedDecoder appends dec to the list of decoders tried for opaque values whose gob wire type name is empty. Decoders are tried in registration order; the first one that returns a non-error result wins.
type InterfaceValue ¶
type InterfaceValue struct {
TypeName string // concrete type name from the stream
Value Value // the concrete value, or NilValue for nil
}
InterfaceValue represents a decoded interface value.
func (InterfaceValue) TypeID ¶
func (InterfaceValue) TypeID() int
type MapValue ¶
type MapValue struct {
TypeName string
GobTypeID int
KeyType string // descriptive label for the key type
ElemType string // descriptive label for the element type
Entries []MapEntry
}
MapValue represents a decoded gob map.
type MessageInfo ¶ added in v0.2.0
type MessageInfo struct {
Index int // 0-based counter of messages in the stream
Offset int64 // byte offset of the length prefix from the start of the stream
BodyLen int // length of the message body (the bytes after the length prefix)
TypeID int // signed type ID from the start of the body: negative = type def, positive = value
Body []byte // raw body bytes (including the type-ID prefix at the start)
}
MessageInfo describes a single length-prefixed message as it appears on the wire, without requiring the message body to be decoded into a Value. It is yielded by Stream.Messages and can be used for size profiling, stream indexing, or recovering framing information during error diagnostics.
func (MessageInfo) IsTypeDef ¶ added in v0.2.0
func (m MessageInfo) IsTypeDef() bool
IsTypeDef reports whether the message carries a type definition.
type OpaqueValue ¶
type OpaqueValue struct {
TypeName string // from CommonType.Name
GobTypeID int // stream-scoped type ID; use with Stream.TypeByID
Encoding string // "gob", "binary", or "text"
Raw []byte // the undecoded blob
Decoded any // best-effort decoded form, nil if no decoder matched
}
OpaqueValue holds the raw bytes for a GobEncoder, BinaryMarshaler, or TextMarshaler value, along with any best-effort decoded form.
func (OpaqueValue) TypeID ¶
func (v OpaqueValue) TypeID() int
type Option ¶
type Option func(*Inspector)
Option is a functional option for New.
func WithReadLimit ¶
WithReadLimit sets the maximum total bytes read from a stream. Zero means no limit.
func WithSkipCorruptValues ¶ added in v0.2.0
WithSkipCorruptValues configures the inspector to continue past individual value-message decode failures instead of aborting the stream. Each skipped message is counted and available via Stream.SkipCount. Errors in type- definition messages remain fatal because they would leave the type registry inconsistent and subsequent values undecodable.
Enable this when inspecting archived logs that may contain occasional bad records; leave it off for strict validation.
func WithTimeFormat ¶
WithTimeFormat sets the layout used to render time.Time opaque values. The layout must be a valid Go time format string (see the time package). Default: time.RFC3339Nano.
type RedactConfig ¶
type RedactConfig struct {
Keys []string // exact field/key names that trigger redaction
Char rune // fill character; defaults to '*'
TextLength int // number of Char runes to emit; 0 = see type-level doc
}
RedactConfig controls value redaction by field or map key name.
When TextLength is 0 (the default), the number of fill characters emitted depends on the rendered form of the value being redacted:
- Single-line values: emit len([]rune(rendered)) fill chars, preserving the visual width of the original text.
- Multi-line values (e.g. nested structs): emit exactly 3 fill chars ("***"). Counting runes across a multi-line rendering is not meaningful — the rune total includes indentation, newlines, and braces rather than content length — and inserting a flat string where a multi-line value was breaks visual alignment regardless. A short placeholder is unambiguous and clean.
Set TextLength > 0 to always emit exactly that many fill chars, regardless of the original rendered length or whether it is single- or multi-line.
Note: Char is repeated by Unicode code point, not by terminal display width. Multibyte fill characters (e.g. '█') produce the requested number of code points; terminal column width may differ.
type RedactTypesConfig ¶
type RedactTypesConfig struct {
Types []string // type names that trigger redaction
Char rune // fill character; defaults to '*'
TextLength int // number of Char runes to emit; 0 = preserve original length
}
RedactTypesConfig controls value redaction by type name.
type Schema ¶
Schema represents a complete parsed Gob schema comprising multiple type declarations.
func FormatSchema ¶
FormatSchema converts a []TypeInfo slice into an AST-based *Schema, covering all top-level named types. Named types with mechanically generated names (containing brackets, e.g. "[]int") are safely excluded from the top-level schema output. Anonymous (unnamed) types appear only inline within other declarations.
To control rendering, pass SchemaFormatOption values to Schema.Format or Schema.FormatTo after calling FormatSchema.
func (*Schema) Format ¶
func (s *Schema) Format(opts ...SchemaFormatOption) string
Format renders the Schema as a string, applying any provided SchemaFormatOptions. A zero-valued ColorScheme (the default) produces plain text identical to Schema.String.
func (*Schema) FormatTo ¶
func (s *Schema) FormatTo(w io.Writer, opts ...SchemaFormatOption) error
FormatTo renders the Schema to w, applying any provided SchemaFormatOptions. The first write error aborts rendering and is returned.
func (*Schema) JSON ¶ added in v0.2.0
JSON returns a compact machine-readable representation of the schema. The output is an array of type declarations with stable keys; downstream tools (code generators, documentation, compatibility checkers) can consume it without parsing Go-style syntax.
func (*Schema) JSONIndent ¶ added in v0.2.0
JSONIndent is like Schema.JSON but formats the output with the given indentation (see encoding/json.MarshalIndent).
type SchemaFormatOption ¶
type SchemaFormatOption func(*formatConfig)
SchemaFormatOption configures the rendering of a Schema. Unlike FormatOption, which applies to value trees, SchemaFormatOption covers only the subset of formatting options meaningful for type-declaration output.
func SchemaWithColor ¶
func SchemaWithColor(scheme ColorScheme) SchemaFormatOption
SchemaWithColor sets the ColorScheme used when rendering a Schema. A zero-valued ColorScheme (the default) produces plain text identical to Schema.String.
func SchemaWithIndent ¶
func SchemaWithIndent(indent string) SchemaFormatOption
SchemaWithIndent sets the indentation string used inside struct bodies. The default is two spaces.
type SliceValue ¶
SliceValue represents a decoded gob slice.
func (SliceValue) TypeID ¶
func (v SliceValue) TypeID() int
type Stats ¶ added in v0.2.0
type Stats struct {
// TotalMessages counts every length-prefixed message in the stream
// (type definitions + values).
TotalMessages int
// TotalBodyBytes is the sum of every message body's length in bytes
// (excluding the length prefix itself). It is a close approximation of
// "bytes on the wire" for the stream.
TotalBodyBytes int64
// TypeDefMessages counts messages that carry a type definition.
TypeDefMessages int
// ValueMessages counts messages that carry a top-level value.
ValueMessages int
// ByType summarises value messages grouped by the top-level type ID.
// Entries are sorted by ValueCount descending, then by Name ascending for
// determinism. TypeInfo from the stream's registry is also included.
ByType []TypeStats
// DecodedOpaques is the number of opaque values (anywhere in the tree)
// whose registered decoder returned a non-nil Decoded value.
DecodedOpaques int
// UndecodedOpaques is the number of opaque values (anywhere in the tree)
// for which no decoder matched — their Decoded field is nil.
UndecodedOpaques int
// Skipped is the count of corrupt value messages silently skipped because
// [WithSkipCorruptValues] was enabled. Zero in strict mode.
Skipped int
}
Stats is a population-level summary of a gob stream: message counts, byte consumption, type distribution, struct-field presence rates, and opaque decoder coverage. Obtain one with Stream.Stats.
A Stats describes the complete stream after Stream.Stats has drained it; partial stats from an aborted pass are not exposed.
func (*Stats) Format ¶ added in v0.2.0
Format writes a human-readable summary of s to w. The output is a small number of lines — total message and byte counts, then a per-type table, then opaque coverage.
func (*Stats) JSON ¶ added in v0.2.0
JSON returns the stats as a JSON document. Convenient for downstream tools that want to aggregate stats across many files.
func (*Stats) JSONIndent ¶ added in v0.2.0
JSONIndent is like Stats.JSON but with indentation.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream represents an in-progress decoding of a gob stream. Obtain one with Inspector.Stream. A Stream is consumed by calling Stream.Values, which returns an iterator over top-level decoded values. Type definitions encountered during decoding are accumulated and accessible via Stream.Types at any point.
A Stream is single-use: calling Values a second time panics. It is not safe for concurrent use.
A Stream does not own its reader. The caller is responsible for closing the underlying io.Reader if needed.
func (*Stream) Collect ¶
Collect drains the remainder of the stream and returns all decoded values. If Values has been partially consumed, Collect returns only the values that had not yet been yielded. On error, returns the values collected before the error alongside it.
func (*Stream) Messages ¶ added in v0.2.0
func (s *Stream) Messages() iter.Seq2[MessageInfo, error]
Messages returns an iterator that yields one MessageInfo per length- prefixed gob message in the stream, *without* decoding values. This is a cheap way to profile a stream (per-message byte count, type-ID distribution) or to locate message boundaries for tooling that processes the raw frames.
The stream is consumed by Messages just like by Values: you cannot call Values on a stream after Messages has drained it, and you cannot call Messages twice.
Unlike Stream.Values, Messages does not register type definitions against the decoder's type registry. If you need both decoded values and per- message framing, use Values and call Stream.TypeByID / Stream.Types at each step, or read the underlying Stream.Messages separately on a second Inspector.Stream over a rewound reader.
func (*Stream) Schema ¶
Schema drains the remainder of the stream and returns a formatted Schema over every type definition encountered. All value messages are decoded and discarded; only type information survives. Returns the partial schema alongside any error.
This implementation decodes value bodies fully and throws them away. It is not optimized for the types-only case.
func (*Stream) SkipCount ¶ added in v0.2.0
SkipCount reports the number of value messages silently skipped so far because WithSkipCorruptValues is enabled and they failed to decode. When WithSkipCorruptValues is disabled this is always zero.
func (*Stream) Stats ¶ added in v0.2.0
Stats drains the remainder of the stream, accumulating per-type and per-field statistics. Returns a partial Stats alongside any error. Value- level decode failures count toward Skipped when WithSkipCorruptValues is enabled and otherwise abort the pass.
Stats consumes the stream — a subsequent call to Values or Messages will panic just as after a normal Collect.
func (*Stream) TypeByID ¶
TypeByID returns the TypeInfo for the given stream-scoped type ID, if known.
func (*Stream) Types ¶
Types returns the live slice of type definitions encountered so far, in the order they appeared on the wire. The slice grows as the stream is consumed; callers iterating Values may call Types() at each step to see types defined up to and including the value just yielded.
TypeRef.Name fields are resolved incrementally as new type definitions arrive. When a type references another type that hasn't been seen yet, the referring TypeRef.Name is initially empty; it is filled in as soon as the referenced type's definition is processed.
The returned slice is owned by the Stream and must not be mutated. If a stable snapshot is needed, the caller should copy it with slices.Clone.
func (*Stream) Values ¶
Values returns an iterator yielding one decoded Value per top-level gob value in the stream. On error it yields (nil, err) and stops. Early break from the loop is safe; the iterator will not read past the break point.
By the time a value v is yielded, every TypeInfo that v's type graph references has been appended to the slice returned by Types, and all TypeRef.Name fields within those types have been resolved against definitions seen so far. Callers may call Types() from inside the loop body to look up the type definition for the value just received.
A Stream is single-use. Calling Values on an already-consumed Stream panics.
type StringValue ¶
type StringValue struct{ V string }
StringValue holds a decoded string.
func (StringValue) TypeID ¶
func (StringValue) TypeID() int
type StructValue ¶
StructValue represents a decoded gob struct.
func (StructValue) TypeID ¶
func (v StructValue) TypeID() int
TypeID implementations — composites and OpaqueValue return their stream-scoped type ID; pure scalars return 0.
type Style ¶
type Style struct {
Prefix, Suffix string
}
Style is a prefix/suffix pair used to wrap rendered tokens with markup or escape sequences. A zero-valued Style applies no wrapping.
type TypeDecl ¶
type TypeDecl struct {
Name string
Kind TypeKind
Fields []FieldDecl // Populated if Kind is KindStruct
TargetType string // Target expression for slices, maps, arrays
Annotation string // Opaque type annotations (GobEncoder, etc.)
}
TypeDecl represents a top-level type declaration in the schema.
type TypeInfo ¶
type TypeInfo struct {
ID int
Name string
Kind TypeKind
Fields []FieldInfo // non-nil only for KindStruct
Key *TypeRef // non-nil only for KindMap
Elem *TypeRef // non-nil for KindMap, KindSlice, KindArray
Len int // non-zero only for KindArray
}
TypeInfo describes a single type definition encountered in a gob stream.
type TypeStats ¶ added in v0.2.0
type TypeStats struct {
TypeID int
Name string
Kind TypeKind
ValueCount int
// BodyBytes is the total body-byte count of all value messages of this
// type (excluding length prefixes). Useful for spotting which types
// dominate the stream.
BodyBytes int64
// FieldPresence counts the number of value messages of this type in
// which each struct field was present (gob omits zero-valued fields).
// Keyed by field name; non-struct types leave it nil.
FieldPresence map[string]int
}
TypeStats describes how a single top-level type contributed to the stream.
type Value ¶
type Value interface {
// TypeID returns the stream-scoped type ID for composite and opaque values
// (StructValue, MapValue, SliceValue, ArrayValue, OpaqueValue).
// Pure scalar values (IntValue, UintValue, FloatValue, ComplexValue,
// BoolValue, StringValue, BytesValue, NilValue, InterfaceValue) return 0.
TypeID() int
// contains filtered or unexported methods
}
Value is the sealed interface implemented by all AST node types. Use a type switch to dispatch on the concrete type.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
demo
command
|
|
|
gq
command
Command gq is a jq-inspired CLI for inspecting gob binary streams.
|
Command gq is a jq-inspired CLI for inspecting gob binary streams. |
|
Package diff computes structural differences between two gobspect.Value trees or two streams of gobspect.Values.
|
Package diff computes structural differences between two gobspect.Value trees or two streams of gobspect.Values. |
|
Package query provides path-based navigation of decoded gobspect Value trees.
|
Package query provides path-based navigation of decoded gobspect Value trees. |
|
Package sortval provides sorting utilities for sequences of gobspect.Value nodes.
|
Package sortval provides sorting utilities for sequences of gobspect.Value nodes. |
|
Package tabular writes gobspect.Value nodes as CSV or TSV rows.
|
Package tabular writes gobspect.Value nodes as CSV or TSV rows. |