annotations

package
v0.0.0-...-341aec2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2025 License: GPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}, annotations *StructAnnotations) ([]byte, error)

Marshal marshals a struct to bytes using the field annotations

func MarshalStruct

func MarshalStruct(v interface{}) ([]byte, error)

MarshalStruct marshals a struct to bytes by automatically parsing its annotations

func MarshalStructWithSize

func MarshalStructWithSize(v interface{}, outputSize int) ([]byte, error)

MarshalStructWithSize marshals a struct to bytes with a specific output size

func MarshalWithOptions

func MarshalWithOptions(v interface{}, annotations *StructAnnotations, opts *MarshalOptions) ([]byte, error)

MarshalWithOptions marshals a struct to bytes using the field annotations and options

func MarshalWithOptionsStruct

func MarshalWithOptionsStruct(v interface{}, opts *MarshalOptions) ([]byte, error)

MarshalWithOptionsStruct marshals a struct to bytes by parsing annotations from v automatically, applying the provided options.

func Unmarshal

func Unmarshal(data []byte, v interface{}, annotations *StructAnnotations) error

Unmarshal unmarshals bytes into a struct using the field annotations

func UnmarshalStruct

func UnmarshalStruct(data []byte, v interface{}) error

UnmarshalStruct unmarshals bytes into a struct by automatically parsing its annotations

func UnmarshalWithOptions

func UnmarshalWithOptions(data []byte, v interface{}, annotations *StructAnnotations, opts *UnmarshalOptions) error

UnmarshalWithOptions unmarshals bytes into a struct using the field annotations and options

func UnmarshalWithOptionsStruct

func UnmarshalWithOptionsStruct(data []byte, v interface{}, opts *UnmarshalOptions) error

UnmarshalWithOptionsStruct unmarshals bytes into a struct by parsing annotations from v automatically, applying the provided options.

Types

type Endianness

type Endianness int

Endianness represents the byte order

const (
	// BigEndian represents big-endian byte order
	BigEndian Endianness = iota
	// LittleEndian represents little-endian byte order
	LittleEndian
	// HostEndian represents the host's native byte order
	HostEndian
)

type FieldAnnotation

type FieldAnnotation struct {
	// ByteOffset is the offset in bytes from the start of the struct
	ByteOffset int
	// BitOffset is the absolute bit offset from the start of the struct (for bitfields)
	BitOffset int
	// BitLength is the length in bits for bitfield fields
	BitLength int
	// Endianness specifies the byte order for multi-byte fields
	Endianness Endianness
	// Skip indicates this field should be skipped during marshaling/unmarshaling
	Skip bool
	// FieldName is the original struct field name
	FieldName string
	// FieldType is the reflect.Type of the field
	FieldType reflect.Type
	// ArrayLength is the length for array fields
	ArrayLength int
	// IsArray indicates if this is an array field
	IsArray bool
	// IsBitfield indicates if this is a bitfield
	IsBitfield bool
	// Reserved indicates this is a reserved/padding field
	Reserved bool
	// HexAsDec indicates values should be treated as hex digits representing decimal
	// e.g., 0x2024 should be treated as year 2024, not 8228
	HexAsDec bool
	// ListSize specifies the field name that contains the count for a list
	// When set, this field is a list whose length is determined at runtime
	ListSize string
	// ListTerminator specifies a byte sequence that terminates the list
	// When the unmarshaler encounters this sequence, it stops reading list elements
	ListTerminator []byte
}

FieldAnnotation represents the annotations for a struct field

func ParseTag

func ParseTag(tag string, fieldType reflect.Type) (*FieldAnnotation, error)

ParseTag parses the custom struct tag for annotations Tag format: `offset:"byte:4,bit:2,len:3,endian:be"`

type MarshalOptions

type MarshalOptions struct {
	// IncludeReserved indicates whether to marshal reserved fields
	IncludeReserved bool
	// IncludeSkipped indicates whether to marshal skipped fields
	IncludeSkipped bool
	// OutputSize forces a specific output buffer size (0 means use calculated size)
	OutputSize int
}

MarshalOptions controls marshaling behavior

type StructAnnotations

type StructAnnotations struct {
	// Fields contains annotations for each field
	Fields []FieldAnnotation
	// TotalSize is the total size of the struct in bytes
	TotalSize int
	// Name is the struct name
	Name string
}

StructAnnotations represents all annotations for a struct

func ParseStruct

func ParseStruct(structType reflect.Type) (*StructAnnotations, error)

ParseStruct parses all annotations for a struct

func (*StructAnnotations) GetFieldByName

func (sa *StructAnnotations) GetFieldByName(name string) *FieldAnnotation

GetFieldByName finds a field annotation by its field name

func (*StructAnnotations) GetFieldByOffset

func (sa *StructAnnotations) GetFieldByOffset(byteOffset int) *FieldAnnotation

GetFieldByOffset finds a field annotation by its byte offset

type UnmarshalOptions

type UnmarshalOptions struct {
	// IncludeReserved indicates whether to unmarshal reserved fields
	IncludeReserved bool
	// IncludeSkipped indicates whether to unmarshal skipped fields
	IncludeSkipped bool
}

UnmarshalOptions controls unmarshaling behavior

Jump to

Keyboard shortcuts

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