settings

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: Apache-2.0 Imports: 15 Imported by: 2

Documentation

Overview

Package settings is like a Swiss Army knife for handling app configurations in Go. It's designed to make your life easier when dealing with all sorts of settings - think of it as your go-to toolkit for configuration management.

Here's the lowdown on what this package brings to the table:

  • Marshaller and Unmarshaller: These interfaces are your friends for transforming settings to and from byte slices. They're super handy for storage or network transmission.
  • String, Bool, Int, Uint etc.: Meet the fundamental building blocks for your settings.
  • Blueprint: This is the brains of the operation. It lets you define and manage settings schemas, validate inputs, and even extend configurations with more settings.
  • Schema: This is the blueprint's state. It's a collection of settings that can be compiled into a schema to store schema version. It also provides a way to create profiles.
  • Profile: Think of it as your settings' memory. It keeps track of user preferences and application settings, making sure everything's in place and up-to-date.
  • Preferences: This is the profile's state. It's a collection of settings that can be compiled into a profile to store user preferences.
  • Reflective Magic: We use reflection (responsibly!) to dynamically handle fields in your structs. This means less boilerplate and more action.

Index

Constants

View Source
const (
	KindSettings = Kind(vars.KindInterface)
	KindCustom   = Kind(vars.KindByteSlice)

	KindInvalid  = Kind(vars.KindInvalid)
	KindBool     = Kind(vars.KindBool)
	KindInt      = Kind(vars.KindInt)
	KindUint     = Kind(vars.KindUint)
	KindString   = Kind(vars.KindString)
	KindDuration = Kind(vars.KindDuration)
)

Variables

View Source
var (
	ErrSettings = errors.New("settings")
	ErrSetting  = errors.New("setting")
	ErrProfile  = fmt.Errorf("%w: profile", ErrSettings)
	ErrSpec     = errors.New("spec error")
)
View Source
var (
	ErrBlueprint = errors.New("settings blueprint")
)
View Source
var (
	ErrSchema = errors.New("schema")
)

Functions

This section is empty.

Types

type Blueprint

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

func New

func New(s Settings) (*Blueprint, error)

func (*Blueprint) AddSpec

func (b *Blueprint) AddSpec(spec SettingSpec) error

func (*Blueprint) AddValidator

func (b *Blueprint) AddValidator(key, desc string, fn func(s Setting) error)

func (*Blueprint) Describe

func (b *Blueprint) Describe(key string, lang language.Tag, description string)

func (*Blueprint) Extend

func (b *Blueprint) Extend(group string, ext Settings)

func (*Blueprint) GetSpec

func (b *Blueprint) GetSpec(key string) (SettingSpec, error)

func (*Blueprint) Schema

func (b *Blueprint) Schema(module, version string) (Schema, error)

type Bool

type Bool bool

Bool represents a setting type based on a boolean.

func (Bool) MarshalSetting

func (b Bool) MarshalSetting() ([]byte, error)

MarshalSetting converts the Bool setting to a byte slice, representing "true" or "false".

func (Bool) SettingKind

func (b Bool) SettingKind() Kind

func (Bool) String

func (b Bool) String() string

func (*Bool) UnmarshalSetting

func (b *Bool) UnmarshalSetting(data []byte) error

UnmarshalSetting updates the Bool setting from a byte slice, interpreting it as "true" or "false".

type Duration

type Duration time.Duration

func (Duration) MarshalSetting

func (d Duration) MarshalSetting() ([]byte, error)

func (Duration) SettingKind

func (d Duration) SettingKind() Kind

func (Duration) String

func (d Duration) String() string

func (*Duration) UnmarshalSetting

func (d *Duration) UnmarshalSetting(data []byte) error

type ExecutionMode

type ExecutionMode int

executionMode determines the context in which the application is running.

const (
	ModeUnknown    ExecutionMode = 1 << iota // Unknown mode
	ModeProduction                           // Running as a compiled binary
	ModeDevel                                // Running with `go run`
	ModeTesting                              // Running in a test context
)

func (ExecutionMode) MarshalText

func (e ExecutionMode) MarshalText() ([]byte, error)

func (ExecutionMode) String

func (e ExecutionMode) String() string

func (*ExecutionMode) UnmarshalText

func (e *ExecutionMode) UnmarshalText(data []byte) error

type Int

type Int int

Int represents a setting type based on an integer.

func (Int) MarshalSetting

func (i Int) MarshalSetting() ([]byte, error)

MarshalSetting converts the Int setting to a byte slice for storage or transmission.

func (Int) SettingKind

func (i Int) SettingKind() Kind

func (Int) String

func (i Int) String() string

func (*Int) UnmarshalSetting

func (i *Int) UnmarshalSetting(data []byte) error

UnmarshalSetting updates the Int setting from a byte slice, interpreting it as an integer.

type Kind

type Kind uint8

func (Kind) String

func (k Kind) String() string

type Marshaller

type Marshaller interface {
	MarshalSetting() ([]byte, error)
}

type Mutability

type Mutability uint8
const (
	// Mutability
	SettingImmutable Mutability = 254
	SettingOnce      Mutability = 253
	SettingMutable   Mutability = 252
)

type Preferences

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

func NewPreferences

func NewPreferences() *Preferences

func (*Preferences) Consume

func (p *Preferences) Consume()

func (*Preferences) Set

func (p *Preferences) Set(key, val string)

type Profile

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

func (*Profile) All

func (p *Profile) All() []Setting

func (*Profile) Get

func (p *Profile) Get(key string) Setting

func (*Profile) Has

func (p *Profile) Has(key string) bool

func (*Profile) Lang

func (p *Profile) Lang() language.Tag

func (*Profile) Loaded

func (p *Profile) Loaded() bool

Loaded reports true when settings profile is loaded and optional user preferences are applied.

func (*Profile) Module

func (p *Profile) Module() string

func (*Profile) Name

func (p *Profile) Name() string

func (*Profile) Pkg

func (p *Profile) Pkg() string

func (*Profile) Set

func (p *Profile) Set(key string, val SettingField) (err error)

func (*Profile) Version

func (p *Profile) Version() string

type Schema

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

func (*Schema) Profile

func (s *Schema) Profile(name string, p *Preferences) (*Profile, error)

type Setting

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

func (Setting) IsSet

func (s Setting) IsSet() bool

func (Setting) Key

func (s Setting) Key() string

func (Setting) Kind

func (s Setting) Kind() Kind

func (Setting) String

func (s Setting) String() string

func (Setting) Value

func (s Setting) Value() vars.Variable

type SettingField

type SettingField interface {
	fmt.Stringer
	Marshaller
	SettingKind() Kind
}

type SettingSpec

type SettingSpec struct {
	IsSet       bool
	Kind        Kind
	Key         string
	Default     string
	Mutability  Mutability
	Value       string
	Required    bool
	Unmarchaler Unmarshaller
	Marchaler   Marshaller
	Settings    *Blueprint
	// contains filtered or unexported fields
}

func ParseToSpec

func ParseToSpec(s Settings) ([]SettingSpec, error)

func (SettingSpec) Validate

func (s SettingSpec) Validate() error

type Settings

type Settings interface {
	Blueprint() (*Blueprint, error)
}

type String

type String string

String represents a setting type based on a string.

func (String) MarshalSetting

func (s String) MarshalSetting() ([]byte, error)

MarshalSetting converts the String setting to a byte slice for storage or transmission.

func (String) SettingKind

func (s String) SettingKind() Kind

func (String) String

func (s String) String() string

func (*String) UnmarshalSetting

func (s *String) UnmarshalSetting(data []byte) error

UnmarshalSetting updates the String setting from a byte slice, typically read from storage or received in a message.

type Uint

type Uint uint

Uint represents a setting type based on an unsigned integer.

func (Uint) MarshalSetting

func (u Uint) MarshalSetting() ([]byte, error)

MarshalSetting converts the Uint setting to a byte slice for storage or transmission.

func (Uint) SettingKind

func (u Uint) SettingKind() Kind

func (Uint) String

func (u Uint) String() string

func (*Uint) UnmarshalSetting

func (u *Uint) UnmarshalSetting(data []byte) error

UnmarshalSetting updates the Uint setting from a byte slice, interpreting it as an unsigned integer.

type Unmarshaller

type Unmarshaller interface {
	UnmarshalSetting([]byte) error
}

Jump to

Keyboard shortcuts

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