Documentation
¶
Overview ¶
Package kv models, parses and serializes the keyval (.kv) format, spec v1.0. The library never touches the filesystem; callers own all file I/O.
Index ¶
- Constants
- func ToJSON(f *File) ([]byte, error)
- func Unmarshal(f *File, v any) error
- type Dialect
- type File
- func (f *File) Create(key Key, val string) error
- func (f *File) CreateMarker(key Key) error
- func (f *File) Delete(key Key) error
- func (f *File) DeleteTree(key Key) error
- func (f *File) Flat() string
- func (f *File) Insert(key Key, val string) error
- func (f *File) Keys(prefix Key) (string, error)
- func (f *File) Pop(key Key) (string, error)
- func (f *File) Push(key Key, val string) error
- func (f *File) Read(key Key) (string, error)
- func (f *File) Set(key Key, val string) error
- func (f *File) String() string
- func (f *File) Tree(prefix Key) (string, error)
- func (f *File) Update(key Key, val string) error
- type Key
- type Pair
- type Segment
Constants ¶
const HashLine = "# github.com/unixfile/keyval"
HashLine identifies the format, pointing at its spec. Any first line starting with # is accepted on read; this exact line is written when none exists.
Variables ¶
This section is empty.
Functions ¶
func ToJSON ¶
ToJSON converts a keyval file to indented JSON. A node with only indexed children becomes an array; any named child makes it an object, with indexed siblings keyed by their integer. Markers become {}, so an empty array does not survive a round trip. A leaf spelled exactly like a JSON number, true, false or null is emitted as that type, everything else as a string; the guessing mirrors FromJSON and is the documented edge of this lossy conversion.
Types ¶
type Dialect ¶
type Dialect int
Dialect selects the key grammar. Strict is the format's own grammar and the only one valid in a .kv file. JSONKeys also accepts the wider names of foreign JSON documents, kept verbatim: anything without a space, dot or control character, not starting with #. It exists for the JSON bridge — .json files and the JSON conversions — and must never reach a .kv file. The package-level functions are the Strict shorthand.
func (Dialect) FromJSON ¶
FromJSON converts a JSON document to a keyval file. Objects become named children, arrays indexed children, empty containers markers. Scalars keep their literal JSON spelling — 8080, true, null — which ToJSON recognizes on the way back, so scalar types round-trip. Object names must fit the dialect's key grammar and may never hold a dot, the path separator. The top-level value must be an object or an array; an empty one yields an empty file.
type File ¶
type File struct {
Hash string // first line if it starts with #, verbatim
Pairs []Pair // kept sorted by key string
}
func Marshal ¶
Marshal renders a Go value as a keyval file. Structs and maps become named children, slices and arrays indexed children, scalars leaves. Struct fields map to their lowercased name, overridden by a `kv:"name"` tag; `kv:"-"` skips a field. Nil pointers are omitted; empty maps and slices become markers. The top-level value must be a struct, map, slice or array.
func Parse ¶
Parse parses file content strictly: out-of-order lines, stale markers and every other spec violation are errors.
func ParseLenient ¶
ParseLenient parses file content the way fmt does: lines are sorted and stale markers dropped before validation, so an appended file normalizes instead of failing.
func (*File) CreateMarker ¶
opCreateMarker declares an empty container at key. The marker is removed automatically when the first child is written.
func (*File) DeleteTree ¶
func (*File) Flat ¶
Flat returns all leaf pairs as flat key-value lines in sorted order, the grep-friendly view of the file.
func (*File) Insert ¶ added in v0.3.0
opInsert writes a new leaf at the index naming the last segment of key, first shifting that sibling and every higher one up by one. An index equal to the member count appends, exactly like push; a higher one would leave a gap and fails.
func (*File) Keys ¶
Keys lists the immediate children of the node at prefix as full dotted paths in file spelling, one per line in sorted order, with a trailing dot on containers. A nil prefix lists the top level; a leaf prefix prints the leaf itself.
func (*File) Pop ¶
opPop removes the highest-indexed child of the container at key and returns what was removed: the value for a leaf, the raw lines for a branch.
func (*File) Push ¶
opPush appends a new leaf to the indexed children of the container at key. Missing intermediate nodes are created implicitly; validation keeps every sequence consecutive.
func (*File) Set ¶
Set creates the leaf at key or overwrites an existing one, the upsert behind the = operator.
type Segment ¶
type Segment struct {
Raw string
Name string // name segments only
Index int // index segments only
IsIndex bool
}
Segment is one dotted-path component: a name or an index. The first character decides which. An index segment's digits denote its integer value; leading zeros are padding.