Documentation
¶
Overview ¶
Package docfile reads the JSON and YAML documents the tool is configured by.
Both formats decode to the same shape -- JSON's value model -- so everything downstream (schema validation, strict struct decoding, the settings layers) handles one representation and cannot behave differently depending on which syntax a maintainer preferred.
Index ¶
- Variables
- func Find(dir, base string) (string, error)
- func IsYAML(path string) bool
- func ParseTree(path string, data []byte) (map[string]any, error)
- func ReadTree(path string) (map[string]any, error)
- func ToJSON(path string, data []byte) ([]byte, error)
- func WriteAtomic(path string, data []byte) (err error)
Constants ¶
This section is empty.
Variables ¶
var Extensions = []string{".json", ".yaml", ".yml"}
Extensions are tried in this order. JSON first, because every shipped config is JSON and a lookup should find it without a second stat.
Functions ¶
func Find ¶
Find returns dir/base with whichever supported extension exists.
Two at once is an error, not a preference order: a tabler.json beside a tabler.yaml means someone converted the file and forgot to delete the original, and silently picking one runs a config nobody is editing. Returns os.ErrNotExist (wrapped) when none exists.
func ParseTree ¶ added in v0.1.1
ParseTree is ReadTree on bytes already in hand; path picks the format.
func ReadTree ¶
ReadTree reads a document into a generic map. Numbers decode as float64, exactly as encoding/json produces them, whichever syntax the file used.
func ToJSON ¶
ToJSON converts a document to canonical JSON bytes. JSON input is returned as-is after a validity check; YAML is decoded and re-encoded.
func WriteAtomic ¶ added in v0.1.1
WriteAtomic replaces path with data so that a reader sees either the old file or the new one, never a partial write.
The bytes go to a temporary file in the same directory (a rename is only atomic within one filesystem), are fsynced, take the existing file's permissions (0644 for a new file), and are renamed over the target. The directory is fsynced afterwards where the platform allows it, so the rename itself survives a crash. On any failure the temporary file is removed and the target is untouched.
Types ¶
This section is empty.