formschema

package
v0.10.0-dev.4 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package formschema is the form-shaped half of every settings section: a schema of typed fields, the decoder that reads a section's YAML node by that schema, and the marks that tell the settings document which scalars are safe to show.

Index

Constants

View Source
const (
	PlainTag     = "!plain"
	UnchangedTag = "!unchanged"
)

PlainTag marks a value a schema decoded as safe to show; UnchangedTag is what a read shows and a Save sends in place of a value kept as stored.

Variables

This section is empty.

Functions

func IsNull

func IsNull(n *yaml.Node) bool

IsNull reports whether n, which must not be nil, is a YAML 1.2 core-schema null read by its text whatever its tag (a plain "", ~, null, Null or NULL), so a null retagged PlainTag still reads as absent.

func MarkPlain

func MarkPlain(n *yaml.Node)

MarkPlain retags n, and every scalar beneath it, PlainTag. A section owner calls it for each non-Secret scalar it accepts without Decode, after reading the values: yaml.v3's node.Decode refuses a !plain scalar as a bool or int. It never follows an alias. A sequence element holding a Secret field must carry an id key, so a Save can match it to its stored secret.

func ParseBool

func ParseBool(text string) (value, ok bool)

ParseBool reads a YAML 1.2 core-schema boolean's text (true, True, TRUE, false, False, FALSE); ok is false for every other text.

Types

type Field

type Field struct {
	Key, Label, Help string
	// Default is the value Decode applies when the key is absent, in the
	// key's own YAML spelling; "" means no default.
	Default string
	// Enum lists an Enum field's accepted values.
	Enum []string
	// Min and Max bound an Int, or a DurationMs in milliseconds, inclusively.
	// Set both, with Min <= Max, or neither: both zero leave it unbounded,
	// and one alone bounds the other side at zero.
	Min, Max int64
	Kind     Kind
	// Required refuses an absent key, and for String and Secret an empty one.
	Required bool
}

Field is one key of a section.

type FieldError

type FieldError struct{ Path, Message string }

FieldError is one refused value: Path is relative to the node Decode was given until Prefix makes it absolute.

func Prefix

func Prefix(prefix string, errs []FieldError) []FieldError

Prefix returns errs with each path made absolute under prefix: a path starting with an index joins without a dot, and an empty path is prefix.

type Kind

type Kind uint8

Kind is how a field's scalar text is read.

const (
	String Kind = iota + 1
	Secret
	Bool
	Int
	DurationMs
	Enum
	StringList
)

The kinds a Field's text is read as; the zero Kind is none of them.

func (Kind) MarshalText

func (k Kind) MarshalText() ([]byte, error)

MarshalText answers the kind's snake_case name and refuses the zero Kind.

func (*Kind) UnmarshalText

func (k *Kind) UnmarshalText(b []byte) error

UnmarshalText reads a snake_case kind name, refusing every other spelling.

type Schema

type Schema struct{ Fields []Field }

Schema is a section's flat fields, in form order.

func (Schema) Decode

func (s Schema) Decode(n *yaml.Node) (Values, []FieldError)

Decode reads n, a section's mapping (nil when the section is absent), by the schema. Every scalar is read from its text by its field's Kind, whatever its YAML tag, and a null is absent. Unknown and repeated keys, anchors and aliases, and values failing their field's rules are field errors at section-relative paths. Each scalar it reads for a non-Secret field is retagged PlainTag in n, whatever errors other fields have.

type Value

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

Value is one decoded field.

type Values

type Values map[string]Value

Values holds a section's decoded fields by key, defaults included; an absent key without a default has no entry.

func (Values) Bool

func (vs Values) Bool(key string) (value, ok bool)

Bool answers a Bool field.

func (Values) Duration

func (vs Values) Duration(key string) (time.Duration, bool)

Duration answers a DurationMs field.

func (Values) Int

func (vs Values) Int(key string) (int64, bool)

Int answers an Int field.

func (Values) Secret

func (vs Values) Secret(key string) (string, bool)

Secret answers a Secret field's text.

func (Values) String

func (vs Values) String(key string) (string, bool)

String answers a String or Enum field's text.

func (Values) StringList

func (vs Values) StringList(key string) ([]string, bool)

StringList answers a copy of a StringList field's elements.

Jump to

Keyboard shortcuts

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