document

package
v0.1.0-dev.20260813041250 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package document provides structured document I/O for YAML and JSON files. It encapsulates the read-deserialize and serialize-write patterns used throughout the codebase, with consistent error wrapping, permission modes, directory creation, and format detection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Read

func Read[T any](r io.Reader) (*T, error)

Read deserializes a structured document from a reader. YAML decoding is used unconditionally since JSON is a valid subset of YAML.

Type Parameters:

  • T: the target type for deserialization

Parameters:

  • r: the reader to read from

Returns:

  • *T: pointer to the deserialized value
  • error: wraps read and parse errors

func ReadFile

func ReadFile[T any](path string) (*T, error)

ReadFile deserializes a structured document from disk. Format is inferred from the file extension: .json → JSON, .yaml/.yml/anything else → YAML.

Type Parameters:

  • T: the target type for deserialization

Parameters:

  • path: filesystem path to the document

Returns:

  • *T: pointer to the deserialized value
  • error: wraps both I/O and parse errors with the file path for context

func Write

func Write(path string, v any, opts ...Option) error

Write serializes v to disk as a structured document. Format is inferred from the file extension. Creates parent directories (0o750) if needed. Default file permission is 0o600; override with WithPerm.

Parameters:

  • path: filesystem path for the output document
  • v: value to serialize
  • opts: optional configuration (WithPerm, WithIndent, WithHeader)

Returns:

  • error: wraps marshal, directory creation, and write errors with the file path for context

Types

type Option

type Option func(*writeOpts)

Option configures Write behavior.

func WithHeader

func WithHeader(header string) Option

WithHeader prepends a literal string before the serialized content. A trailing newline is appended if not present.

Parameters:

  • header: text to prepend (e.g., a generated-file comment or disclaimer)

Returns:

  • Option: a write option that sets the header

func WithIndent

func WithIndent(prefix, indent string) Option

WithIndent controls JSON indentation. Ignored for YAML. Default is 2-space indent with no prefix.

Parameters:

  • prefix: prefix string prepended to each line (typically empty)
  • indent: indent string used for each level of nesting

Returns:

  • Option: a write option that sets JSON indentation

func WithPerm

func WithPerm(mode os.FileMode) Option

WithPerm overrides the default 0o600 file permission.

Parameters:

  • mode: the file permission mode to use

Returns:

  • Option: a write option that sets the file permission

Jump to

Keyboard shortcuts

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