preset

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package preset holds named bundles of barqr options.

A preset is the answer to "we always send the same fourteen query parameters". The caller names one — `?preset=print` — and the service expands it into the option keys the request layer already understands, in the same dot notation the query string and multipart transports use (`style.module`, `output.dpi`, `encode.ecc`). Nothing here knows what those keys mean; expansion and validation stay with the request layer, so adding an option to Request makes it presettable with no change to this package.

Presets come from two places: the built-ins compiled into the binary, and a directory of JSON files named by BARQR_PRESETS_PATH. The directory is operator-supplied and therefore untrusted input — see Load for the guards.

Index

Constants

View Source
const (
	// MaxFileBytes caps one preset file. A preset is a flat map of a few dozen
	// short strings; 64 KiB is two orders of magnitude of headroom, and
	// anything larger is a mistake or an attack rather than a preset.
	MaxFileBytes = 64 << 10

	// MaxPresets caps how many presets a Set may hold, built-ins included.
	// The names are served on /v1/presets and offered as typo suggestions, so
	// an unbounded count would turn a mounted directory into a response-size
	// and CPU problem elsewhere in the service.
	MaxPresets = 256

	// MaxOptions caps the option keys in one preset. The request layer has
	// well under a hundred settable paths, so a preset needing more than this
	// is not describing a barqr request.
	MaxOptions = 128
)

Limits on what a presets directory may contain. They exist because the directory is mounted by an operator and read at boot: a runaway file or a runaway file count must fail loudly and cheaply, not exhaust the process.

Variables

View Source
var (
	// ErrNotADirectory means the configured presets path is not a directory.
	ErrNotADirectory = errors.New("presets path is not a directory")
	// ErrUnreadable means the presets directory could not be listed at all,
	// which is a configuration fault rather than a bad individual file.
	ErrUnreadable = errors.New("presets directory could not be read")
)

Sentinel errors for the preset package.

Functions

func ValidName

func ValidName(name string) bool

ValidName reports whether name is a legal preset name.

The rule is `^[a-z0-9][a-z0-9_-]{0,63}$`: lowercase alphanumerics, hyphens and underscores, starting with an alphanumeric, at most 64 characters.

Types

type Kind

type Kind string

Kind distinguishes the two sorts of preset.

const (
	// KindLayout sets format, resolution and error correction: it answers
	// "where is this code going".
	KindLayout Kind = "layout"
	// KindTheme sets shapes and colours only, and nothing else, so it composes
	// with any layout and any output format.
	KindTheme Kind = "theme"
)

Preset kinds.

type Preset

type Preset struct {
	// Name is the slug the caller asks for. It matches ValidName.
	Name string `json:"name"`
	// Kind separates a layout from a theme, so a caller — or the documentation
	// UI — can offer them as the two different questions they answer: where is
	// this code going, and what should it look like. Empty means layout.
	Kind Kind `json:"kind,omitempty"`
	// Description is one line of prose for /v1/presets listings.
	Description string `json:"description,omitempty"`
	// Options maps dot-notation option keys to values.
	Options map[string]any `json:"options"`
}

Preset is a named bundle of options.

Options are keyed in the request layer's dot notation — "style.module", "output.format", "encode.ecc" — and the values are whatever JSON produced: string, float64, bool. They are applied as if the caller had sent them, so an explicit parameter on the request overrides the preset's value for the same key.

func (Preset) Clone

func (p Preset) Clone() Preset

Clone returns a copy whose Options map shares nothing with the receiver.

Sets are read-only after construction and are shared by every in-flight request, so handing out the internal map would let one request's option merge corrupt every later request that names the same preset.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set is an immutable collection of presets, looked up by name.

The zero value is not usable; build one with Builtin or Load.

func Builtin

func Builtin() *Set

Builtin returns the presets compiled into the binary.

The returned Set is a fresh copy, so a caller that loads a directory over it cannot mutate the built-ins seen by anything else.

func Load

func Load(dir string) (*Set, []string, error)

Load returns the built-in presets overlaid with the JSON files in dir.

A user preset whose name matches a built-in **overrides** it: the file wins, completely, and the built-in's options are not merged underneath. That is the point — an operator who wants "print" to mean 600 dpi in their shop should not have to fight the value compiled into the binary.

An empty dir means built-ins only, which is the default configuration.

A malformed, oversized or badly named file is a warning, not a failure: one bad file in a mounted directory must not stop the service booting. The warnings are returned so the caller can log them at boot; they name the file but never its path. An error is returned only when the directory itself cannot be used, which is a configuration fault the operator must see.

dir is operator-controlled input and is treated as untrusted. See the guards inline below: extension, file type, containment, name shape, size, and count.

func (*Set) All

func (s *Set) All() []Preset

All returns every preset, sorted by name. The copies are safe to mutate.

func (*Set) Get

func (s *Set) Get(name string) (Preset, bool)

Get returns the preset with the given name.

Lookup is case-insensitive and ignores surrounding space, because a preset name arrives from a query string typed by a human.

func (*Set) Len

func (s *Set) Len() int

Len returns the number of presets in the set.

func (*Set) Names

func (s *Set) Names() []string

Names returns every preset name, sorted, for listings and suggestions.

Jump to

Keyboard shortcuts

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