Documentation
¶
Overview ¶
Package jsonfile reads and writes JSON files atomically and provides typed directory collections for on-disk document stores.
Use ReadStrict when decoding config or state files where unknown JSON fields should fail fast. All reads reject trailing content after the first value.
Index ¶
- Constants
- func CapList[T any](items []T, limit int) []T
- func EntryDirName(e os.DirEntry) (string, bool)
- func ListDir[T any](dir string, match EntryMatch, load func(id string) (T, error), ...) ([]T, error)
- func Read[T any](path string) (T, error)
- func ReadStrict[T any](path string) (T, error)
- func Write(path string, v any) error
- func WriteAtomic(path string, data []byte, perm os.FileMode) (err error)
- type Collection
- type EntryMatch
Constants ¶
const DefaultFilePerm = 0o644
DefaultFilePerm is the mode used for persisted JSON documents.
Variables ¶
This section is empty.
Functions ¶
func EntryDirName ¶
EntryDirName matches subdirectories and uses the directory name as id.
func ListDir ¶
func ListDir[T any](dir string, match EntryMatch, load func(id string) (T, error), keep func(T) bool) ([]T, error)
ListDir loads documents from dir using match and load.
func ReadStrict ¶
ReadStrict decodes one JSON document and rejects unknown object fields.
Types ¶
type Collection ¶
type Collection[T any] struct { Dir func(root string) string Match EntryMatch Path func(root, id string) (string, error) ID func(T) string Less func(a, b T) bool }
Collection persists typed JSON documents under a root directory.
func (Collection[T]) List ¶
func (c Collection[T]) List(root string, keep func(T) bool, limit int) ([]T, error)
List returns documents under root, optionally filtered and capped.
func (Collection[T]) Load ¶
func (c Collection[T]) Load(root, id string) (T, error)
Load reads one document by id from root.
func (Collection[T]) Save ¶
func (c Collection[T]) Save(root string, doc T) error
Save writes doc to its configured path under root.
type EntryMatch ¶
EntryMatch selects directory entries and returns an id for loading.
func EntryFileSuffix ¶
func EntryFileSuffix(suffix string) EntryMatch
EntryFileSuffix matches regular files with the given suffix.