cfg

package
v0.0.0-...-6e98937 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SaveSettings

func SaveSettings(path string, s Settings) error

SaveSettings saves the settings as JSON to the path. It will remove any private keys.

func StringToNrgba

func StringToNrgba(s string) (color.NRGBA, bool)

StringToNrgba converts a string to a color. Current supported formats: #FFFFFF #FFFFFFFF

func WriteJson

func WriteJson(s Settings) ([]byte, error)

Types

type Builder

type Builder interface {
	// Settings answers the raw settings. Generally treat this
	// as read-only, and use AddSettings() to make any changes.
	Settings() map[string]any

	// NewSettings answers a new empty settings.
	NewSettings() map[string]any

	// AddSettings adds the new settings to the settings being built.
	AddSettings(map[string]any)
}

Builder is used to add new settings during a Settings construction.

type EnvMatcher

type EnvMatcher interface {
	// Answer a key if a match, or else "". The key
	// might be the same key in the args, or it might be modified.
	Match(key string) (string, error)
}

EnvMatcher is used by WithEnv to accept env vars and potentially modify the name.

func EnvPattern

func EnvPattern(pattern string) EnvMatcher

EnvPattern loads all env vars that match the pattern into the Settings. See path.Match() for match rules.

func EnvPrefix

func EnvPrefix(prefix string) EnvMatcher

EnvPrefix loads all env vars that match the prefix into the Settings. The prefix will be stripped.

type Option

type Option func(Builder, oferrors.Block)

Option used during construction of a Settings. Clients can add to the settings with the Builder.

func WithEnv

func WithEnv(match EnvMatcher) Option

WithEnv loads all matching env vars.

func WithFS

func WithFS(fsys fs.FS, pattern string, processors ...Process) Option

WithFS loads all files that match the pattern into the Settings. All matched files must be in JSON format. See path.Match() for match rules.

func WithKeys

func WithKeys(src Settings, keys []string) Option

WithKeys adds all keys in the supplied settings.

func WithMap

func WithMap(m map[string]any) Option

WithMap adds all keys in the supplied map. Note this is only a shallow copy of the top layer, anything below that is the original reference.

func WithSettings

func WithSettings(srcs ...Settings) Option

WithSettings acts as a deep copy on src.

func WithString

func WithString(s string) Option

type Process

type Process func(map[string]any) map[string]any

type Settings

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

Settings stores a tree of settings values, accessed via a path syntax ("path/to/value"). Settings are immutable. Modifications must be made via creating a new Settings.

func NewEmptySettings

func NewEmptySettings() Settings

func NewSettings

func NewSettings(opts ...Option) (Settings, error)

func (Settings) AllKeys

func (s Settings) AllKeys() iter.Seq[string]

AllKeys iterates all keys at the top level.

func (Settings) Bool

func (s Settings) Bool(path string) (bool, bool)

Bool answers the bool value at the given path. The value must be a bool, a string (with value "true" or "t") or an element of a slice (for example, if the Settings contains "fruits": ["apple", "orange"], then Bool("fruits/apple") will return true).

func (Settings) Float64

func (s Settings) Float64(path string) (float64, bool)

Float64 answers the float64 value at the given path.

func (Settings) Int64

func (s Settings) Int64(path string) (int64, bool)

Int64 answers the int64 value at the given path.

func (Settings) Length

func (s Settings) Length(path string) int

Length answers the length of the slice at path, or 0 if path is not a slice.

func (Settings) MustBool

func (s Settings) MustBool(path string, fallback bool) bool

MustBool answers the bool value at the given path or fallback if path is absent.

func (Settings) MustFloat64

func (s Settings) MustFloat64(path string, fallback float64) float64

MustFloat64 answers the bool value at the given path or fallback if path is absent.

func (Settings) MustInt64

func (s Settings) MustInt64(path string, fallback int64) int64

MustInt64 answers the bool value at the given path or fallback if path is absent.

func (Settings) MustNRGBA

func (s Settings) MustNRGBA(path string, r, g, b, a uint8) color.NRGBA

MustNRGBA answers the NRGBA value at the given path or fallback if path is absent. Path must point to a hex value containing rgba components, i.e. "#ffffff" or "#ffffffff"

func (Settings) MustRectF

func (s Settings) MustRectF(path string, r geo.RectF) geo.RectF

MustRectF returns a rect. The path must point to the parent of keys that can be "l", "t", "r", "b", or some combination (i.e. "lr").

func (Settings) MustString

func (s Settings) MustString(path, fallback string) string

MustString answers the string value at the given path or fallback if path is absent.

func (Settings) NRGBA

func (s Settings) NRGBA(path string) (color.NRGBA, bool)

NRGBA answers the NRGBA value at the given path. Path must point to a hex value containing rgba components, i.e. "#ffffff" or "#ffffffff"

func (Settings) Print

func (s Settings) Print()

func (Settings) RectF

func (s Settings) RectF(path string) (geo.RectF, bool)

MustRectF returns a rect. The path must point to the parent of keys that can be "l", "t", "r", "b", or some combination (i.e. "lr").

func (Settings) String

func (s Settings) String(path string) (string, bool)

String answers the string value at the given path.

func (Settings) Strings

func (s Settings) Strings(path string) []string

Strings answers the string slice at the given path. If path points to a string slice, it's returned, otherwise the current keys are returned.

func (Settings) Subset

func (s Settings) Subset(path string) Settings

Subset answers a subset of the settings tree based on walking down the path. The path can have components separated with "/".

Jump to

Keyboard shortcuts

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