mockfile

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package mockfile reads mock documents and preserves their source while splitting YAML streams.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func File

func File[M, A any](path string, missingHint func(string) string, multiHint string, build func(Document, A) (M, error)) builder[M, A]

File defers reading path until each Build, and then builds its single document with build.

Types

type Document

type Document struct {
	// Origin identifies the input in problems, including a YAML document number after splitting.
	Origin string
	// Source is the source identifier recorded on the mock, such as its file path or admin origin.
	Source string
	// Format selects the JSON or YAML decoder.
	Format string
	// Dir is where relative body files open. Empty uses the working directory.
	Dir string
	// Data holds the source document, before schema validation or templating.
	Data []byte
	// Err retains a read or parse failure from Load for the protocol's build pass.
	Err error
}

Document is a mock's source data, or an error Load retains for a server to report with its other build errors.

func Load

func Load(paths []string, missingHint func(string) string) []Document

Load reads files in their given order and expands their YAML documents. A failed file contributes one Document with Err set, so servers can report every bad file while keeping their existing all-or-nothing build path.

func Read

func Read(path string, missingHint func(string) string) (Document, error)

Read reads a JSON or YAML mock file. missingHint optionally explains how to find a missing file. Relative body files start at the returned Dir. Failures are config.Error values with scope mock.

func (Document) Builder

func (doc Document) Builder[M, A any](multiHint string, build func(Document, A) (M, error)) builder[M, A]

Builder defers building this single document until Build supplies the server.

func (Document) Decode

func (doc Document) Decode[T any](schema *config.Schema[T], dst *T, check func(string, []config.Problem) []config.Problem) error

Decode loads the document into dst, then adds check's cross-field problems when parsing succeeded. It preserves schema problems and their order; a retained file error passes through before schema loading.

func (Document) Single

func (doc Document) Single(hint string) (Document, error)

Single selects one nonempty document, or leaves an empty stream for the mock schema to validate. hint tells the caller how to load multiple documents when that is what they supplied.

func (Document) Split

func (doc Document) Split() ([]Document, error)

Split expands YAML documents in order, retaining source paths and physical document numbers in their origins. JSON and unsupported formats pass through for the mock schema to validate. Empty YAML documents add no mocks.

Jump to

Keyboard shortcuts

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