ldf

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ValueTypeString = ValueType(iota) // An encoding-independent string (could be utf-8 or utf-16 depending on usage).
	ValueTypeI32

	ValueTypeFloat
	ValueTypeDouble
	ValueTypeU32

	ValueTypeBool
	ValueTypeU64
	ValueTypeI64

	ValueTypeUtf8 // A string intended to always be encoded in utf-8.
)

Variables

This section is empty.

Functions

func MarshalLines added in v0.10.0

func MarshalLines(v any) ([]byte, error)

MarshalLines returns the textual LDF encoding of v with each field separated by a comma and newline character.

See MarshalText for details.

func MarshalText

func MarshalText(v any) ([]byte, error)

MarshalText returns the textual LDF encoding of v.

MarshalText only encodes top-level structs and maps. If MarshalText encounters a type it cannot encode it will return an error.

The encoding of fields can be customized through the "ldf" key in the field's tag as a comma-separated list. The first option is always the custom field name. Using the name "-" will ignore the field. The other options can be specified in any order. These options include:

  • omitempty: indicates that the struct field should not be encoded if the value is equal to its type's zero value or if the value's length is 0.
  • raw (string only): indicates that the field should be encoded as the ValueType, ValueTypeUtf8.

Strings are encoded with the value type ValueTypeString. To encode a string in utf-16, use either the String16 or []uint16 types.

Fields with type []uint8 (or []byte) are encoded with the value type ValueTypeUtf8.

Fields with type int or uint are encoded to ValueTypeI64 and ValueTypeU64 respectively.

Fields with an interface type will encode the value contained within the interface.

Fields that implement the encoding.TextMarshaler interface are treated as string types and obey the "raw" option.

Embedded structs are encoded as if their fields were at the same level as their parent struct. Field tags on embedded structs are ignored.

Example fields:

// Encodes: "Field=0:"
Field string

// Encodes: "MyField=0:"
Field string `ldf:"MyField"`

// Encodes: "MyField=0:" if Field is not empty
Field string `ldf:"MyField,omitempty"`

// Encodes: "Field=13:"
Field string `ldf:",raw"`

Map key types must be a string. Map values follow the same encoding rules as struct fields.

func UnmarshalText added in v0.10.0

func UnmarshalText(data []byte, v any) error

UnmarshalText parses the textual LDF encoded data into v. UnmarshalText only decodes pointers to structs or maps with a string key type.

Fields, by default, are delimited by a comma, a newline character, or a CRLF. The delimiter can be changed on a custom decoder by calling TextDecoder.SetDelim.

UnmarshalText returns an error if the encoded value type does not match the struct field's type.

If the field type is a slice, a new slice is created.

Fields that implement the encoding.TextUnmarshaler interface are only unmarshaled if the value type is either ValueTypeString or ValueTypeUtf8. If the field's type is not a pointer, the pointer type to that field is checked for compatibility with encoding.TextUnmarshaler.

When decoding a map:

Types

type String16 added in v0.10.0

type String16 []uint16

func ToString16 added in v0.10.0

func ToString16(s string) String16

func (String16) String added in v0.10.0

func (s String16) String() string

type TextDecoder

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

func NewTextDecoder

func NewTextDecoder(r io.Reader) *TextDecoder

func (*TextDecoder) Decode

func (d *TextDecoder) Decode(v any) error

func (TextDecoder) Err

func (d TextDecoder) Err() error

func (*TextDecoder) Next

func (d *TextDecoder) Next() bool

func (*TextDecoder) SetDelim added in v0.10.0

func (d *TextDecoder) SetDelim(delim *regexp.Regexp)

func (TextDecoder) Token

func (d TextDecoder) Token() Token

type TextEncoder

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

func NewTextEncoder

func NewTextEncoder(w io.Writer, delim ...string) *TextEncoder

func (*TextEncoder) Encode

func (e *TextEncoder) Encode(v any) error

type Token

type Token struct {
	Name  string
	Type  ValueType
	Value []byte
}

type ValueType

type ValueType int

func (ValueType) String

func (t ValueType) String() string

Jump to

Keyboard shortcuts

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