ub

package
v0.8.0-a.9 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package ub encodes Go values as unobin language literals, the counterpart to encoding/json for unobin's own syntax. Values that implement Marshaler control their own representation; everything else walks reflectively. Marshal emits one-line output; MarshalIndent adds newlines and indentation for nested containers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal returns v as a one-line UB expression.

func MarshalIndent

func MarshalIndent(v any, prefix, indent string) ([]byte, error)

MarshalIndent returns v as a multi-line UB expression. prefix is prepended to every line except the first; indent is the per-depth indent string. Atomic values render the same in either form.

func Unmarshal

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

Unmarshal parses data as a UB expression and stores the result in the value pointed to by v. v must be a non-nil pointer. The decode rules mirror the encoder's: bool, integer, float, string, list, map, struct, time.Time, time.Duration, and []byte all decode reflectively. Types implementing Unmarshaler receive the value's UB form (re-rendered from the parsed AST, so callers see a canonical representation rather than the input's original whitespace) at every level: top, struct field, map value, slice element.

Types

type Marshaler

type Marshaler interface {
	MarshalUB() ([]byte, error)
}

Marshaler is implemented by types that supply their own unobin representation. The returned bytes are a single UB expression inserted verbatim into the output; callers control quoting, escaping, and structure.

type Tag

type Tag struct {
	Name      string
	Skip      bool
	Omitempty bool
	Sensitive bool
	Unknown   []string
}

Tag is a field's parsed ub struct tag. Name is the tag's own name and may be empty; FieldName resolves the kebab-case fallback. Unknown collects options outside the recognized set so the schema reader can warn on a typo such as "sensitiv" instead of quietly accepting it.

func ParseTag

func ParseTag(value string) Tag

ParseTag reads the value of a ub struct tag: a name, possibly empty, followed by comma-separated options. A value of exactly "-" skips the field. The name and each option are trimmed of spaces; omitempty, squash, and sensitive are the recognized options. Every reader of the tag resolves it through here, so the schema a factory compiles against and the values it encodes and decodes at run time follow one grammar.

func (Tag) FieldName

func (t Tag) FieldName(goName string) string

FieldName is the map key for a field under the ub tag convention: the tag's name, or the kebab-cased Go field name when the tag names nothing.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalUB(data []byte) error
}

Unmarshaler is implemented by types that parse their own unobin representation. UnmarshalUB receives the bytes of the value as they appeared in the input (with surrounding whitespace trimmed by the parser) and populates the receiver.

Jump to

Keyboard shortcuts

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