config

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 9 Imported by: 0

README

Common Go TOML configuration file code shared between my projects

GoDoc Build Coverage

License

This project is subject to the MIT License. See LICENSE information for details.

Documentation

Overview

Package config provides functions and types to define config objects and load and save them to TOML files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Defaults

func Defaults[C any](cfg *C, defaultsData []byte) (*C, error)

Defaults creates a default config from the given default config data (usually statically embedded).

func Load

func Load[C any](cfg *C, path string, defaultsData []byte, strict bool) (*C, error)

Load loads a config from a file. Before loading the config from the given path, the config object is initialized using the given default data.

If the strict flag is set non-matching fields will cause errors. Otherwise they are silently ignored.

func MarshalEnum

func MarshalEnum[E comparable](e E, marshalMap map[E]string) ([]byte, error)

MarshalEnum is a helper function to implement encoding.TextMarshaler for an enum type based on the given map.

func MarshalStringerEnum added in v0.0.2

func MarshalStringerEnum[E fmt.Stringer](e E) ([]byte, error)

MarshalStringerEnum is a helper function to implement encoding.TextMarshaler for an enum type based on the enum type's fmt.Stringer interface.

func Save

func Save(cfg any, path string, perm os.FileMode) error

Save saves the given config object to the given path.

func UnmarshalEnum

func UnmarshalEnum[E comparable](unmarshalMap map[string]E, text []byte) (E, error)

UnmarshalEnum is a helper function to implement encoding.TextUnmarshaler for an enum type.

Types

type DurationSpec

type DurationSpec time.Duration

DurationSpec supports Marshaling of time.Duration values.

func (*DurationSpec) MarshalText

func (spec *DurationSpec) MarshalText() ([]byte, error)

See encoding.TextMarshaler

func (*DurationSpec) UnmarshalText

func (spec *DurationSpec) UnmarshalText(text []byte) error

See encoding.TextUnmarshaler

type NetworkSpec

type NetworkSpec struct {
	netip.Prefix
}

NetworkSpec supports Marshaling of netip.Prefix values.

func (*NetworkSpec) MarshalText

func (spec *NetworkSpec) MarshalText() ([]byte, error)

See encoding.TextMarshaler

func (*NetworkSpec) UnmarshalText

func (spec *NetworkSpec) UnmarshalText(text []byte) error

See encoding.TextUnmarshaler

type NetworkSpecs

type NetworkSpecs []NetworkSpec

NetworkSpecs defines an array of [NetworkSpec]s.

func (NetworkSpecs) Prefixes

func (specs NetworkSpecs) Prefixes() []netip.Prefix

Prefixes returns the [netip.Prefix]es contained in this array.

type RegexpSpec

type RegexpSpec struct {
	*regexp.Regexp
}

RegexpSpec supports Marshaling of regexp.Regexp values.

func (*RegexpSpec) MarshalText

func (spec *RegexpSpec) MarshalText() ([]byte, error)

See encoding.TextMarshaler

func (*RegexpSpec) UnmarshalText

func (spec *RegexpSpec) UnmarshalText(text []byte) error

See encoding.TextUnmarshaler

type TimeLocationSpec

type TimeLocationSpec struct {
	*time.Location
}

TimeLocationSpec supports Marshaling of time.Location values.

func (*TimeLocationSpec) MarshalText

func (spec *TimeLocationSpec) MarshalText() ([]byte, error)

See encoding.TextMarshaler

func (*TimeLocationSpec) UnmarshalText

func (spec *TimeLocationSpec) UnmarshalText(text []byte) error

See encoding.TextUnmarshaler

type URLSpec

type URLSpec struct {
	*url.URL
}

URLSpec supports Marshaling of url.URL values.

func (*URLSpec) MarshalText

func (spec *URLSpec) MarshalText() ([]byte, error)

See encoding.TextMarshaler

func (*URLSpec) UnmarshalText

func (spec *URLSpec) UnmarshalText(text []byte) error

See encoding.TextUnmarshaler

type URLSpecs

type URLSpecs []URLSpec

URLSpecs defines an array of [URLSpec]s.

func (URLSpecs) URLs

func (specs URLSpecs) URLs() []*url.URL

URLs returns the [url.URL]s contained in this array.

Jump to

Keyboard shortcuts

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