configfile

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package configfile reads and edits the settings files emulators keep, in their own formats: INI, TOML, YAML, XML and JSON. An edit changes only the value it names and leaves the rest of the file as the emulator wrote it, so a person's own changes, comments and keys this package knows nothing about survive.

The files live in a folder the emulator can write to, and an emulator is treated as something that may have been taken over, so every reader here expects hostile input: it refuses files over MaxSize, never panics, and a value can never add a line, a section or an element of its own.

Index

Constants

View Source
const MaxSize = 4 << 20

MaxSize is the largest file this reads or edits. An emulator's own is a few kilobytes.

Variables

View Source
var Formats = []string{"ini", "toml", "yaml", "xml", "json"}

Formats this package knows.

Functions

func Apply

func Apply(format string, old []byte, edits []Edit) ([]byte, error)

Apply makes edits to a file's text and returns the new text. old may be empty: the file is then made. Paths are, by format:

  • ini: [section, key]
  • toml: [table..., key], dotted tables as their parts
  • yaml: [key, key, ...] through nested block maps
  • xml: [element, element, ...] from the root element down to the one whose text is set
  • json: [key, ...] through nested objects

A value that would end up somewhere it can't be written plainly (in a TOML array, under a YAML key that holds a list, in an XML element with children of its own) is refused rather than guessed at.

func Known

func Known(format string) bool

Known says whether a format is one of Formats.

func SameValue

func SameValue(format string, read, written any) bool

SameValue says whether a value read from a file is the one written: as text for INI (where space around it doesn't count) and XML, as the same type and value for the others.

Types

type Edit

type Edit struct {
	Path  []string
	Value any
}

Edit sets one value. Path names it in the file's own terms (see Apply); Value is a bool, an int64, a float64 or a string. In JSON it may also be a []any or a map[string]any of those (and of each other), which replaces whatever the file had there, list or object, whole.

type Entry

type Entry struct {
	Path  []string
	Value any
}

Entry is one value found in a file.

func Entries

func Entries(format string, data []byte) ([]Entry, error)

Entries lists the plain values a file holds, in the order they appear. Values are typed where the format types them (TOML, YAML, JSON); INI and XML values are text. A list or a table written in one value is given as its text.

Jump to

Keyboard shortcuts

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