ez

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2022 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigFileEnvFlag

func ConfigFileEnvFlag[T any, TP ConfigWithConfigPath[T]](ctx context.Context, cfg TP, df DecoderFactory, params Params[T]) (*dials.Dials[T], error)

ConfigFileEnvFlag takes advantage of the ConfigWithConfigPath cfg to indicate what file to read and uses the passed decoder. Configuration values provided by the returned Dials are the result of stacking the sources in the following order:

  • configuration file
  • environment variables
  • flags it registers with the standard library flags package

The contents of cfg for the defaults cfg.ConfigPath() is evaluated on the stacked config with the file-contents omitted (using a "blank" source)

func FileExtensionDecoderConfigEnvFlag

func FileExtensionDecoderConfigEnvFlag[T any, TP ConfigWithConfigPath[T]](ctx context.Context, cfg TP, params Params[T]) (*dials.Dials[T], error)

FileExtensionDecoderConfigEnvFlag takes advantage of the ConfigWithConfigPath cfg and thinly wraps ConfigFileEnvFlag and and thinly wraps ConfigFileEnvFlag choosing the dials.Decoder used when handling the file contents based on the file extension (from the limited set of JSON, YAML and TOML).

func JSONConfigEnvFlag

func JSONConfigEnvFlag[T any, TP ConfigWithConfigPath[T]](ctx context.Context, cfg TP, params Params[T]) (*dials.Dials[T], error)

JSONConfigEnvFlag takes advantage of the ConfigWithConfigPath cfg, thinly wraping ConfigFileEnvFlag with the decoder statically set to JSON.

func TOMLConfigEnvFlag

func TOMLConfigEnvFlag[T any, TP ConfigWithConfigPath[T]](ctx context.Context, cfg TP, params Params[T]) (*dials.Dials[T], error)

TOMLConfigEnvFlag takes advantage of the ConfigWithConfigPath cfg, thinly wraping ConfigFileEnvFlag with the decoder statically set to TOML.

func YAMLConfigEnvFlag

func YAMLConfigEnvFlag[T any, TP ConfigWithConfigPath[T]](ctx context.Context, cfg TP, params Params[T]) (*dials.Dials[T], error)

YAMLConfigEnvFlag takes advantage of the ConfigWithConfigPath cfg, thinly wraping ConfigFileEnvFlag with the decoder statically set to YAML.

Types

type ConfigWithConfigPath

type ConfigWithConfigPath[T any] interface {
	*T
	// ConfigPath implementations should return the configuration file to
	// be read as the first return-value, and true, or an empty string and
	// false.
	ConfigPath() (string, bool)
}

ConfigWithConfigPath is an interface config struct that supplies a ConfigPath() method to indicate which file to read as the config file once populated.

type DecoderFactory

type DecoderFactory func(string) dials.Decoder

DecoderFactory should return the appropriate decoder based on the config file path that is passed as the string argument to DecoderFactory

type Params

type Params[T any] struct {
	// OnWatchedError registers a callback to record any errors encountered
	// while stacking or verifying a new version of a configuration (if
	// file-watching is enabled)
	OnWatchedError dials.WatchedErrorHandler[T]
	// OnNewConfig registers a callback to record new config versions
	// reported while watching the config
	OnNewConfig dials.NewConfigHandler[T]

	// WatchConfigFile allows one to watch the config file by using the
	// watching file source.
	WatchConfigFile bool

	// FlagConfig sets the flag NameConfig
	FlagConfig *flag.NameConfig

	// FlagSource one to use a different flag source instead of the
	// default commandline-source from the flag package.
	// This explicitly supports use with the pflag package's source.Set type.
	FlagSource dials.Source

	// DisableAutoSetToSlice allows you to set whether sets (map[string]struct{})
	// should be automatically converted to slices ([]string) so they can be
	// naturally parsed by JSON, YAML, or TOML parsers.  This is named as a
	// negation so AutoSetToSlice is enabled by default.
	DisableAutoSetToSlice bool

	// DialsTagNameDecoder indicates the naming scheme in use for
	// dials tags in this struct (copied from field-names if unspecified).
	// This is only useful if using a FileFieldNameEncoder (below)
	//
	// In many cases, the default of caseconversion.DecodeGoCamelCase
	// should work. This field exists to allow for other naming schemes.
	// (e.g. SCREAMING_SNAKE_CASE).
	//
	// See the caseconversion package for available decoders.
	// Note that this does not affect the flags or environment-variable
	// naming.  To manipulate flag naming, see `WithFlagConfig`.
	DialsTagNameDecoder caseconversion.DecodeCasingFunc

	// FileFieldNameEncoder allows one to manipulate the casing of the keys
	// in the configuration file.  See the DialsTagNameDecoder field for
	// controlling how dials splits field-names into "words".
	// Fields that lack a `dials` tag's formatting.  If the `dials` tag is unspecified, the struct
	// field's name will be used.  The encoder argument should indicate the format
	// that dials should expect to find in the file.
	//
	// For instance if you leave the `dials` tag unspecified and want a
	// field named `SecretValues` in your configuration to map to a value
	// in your config named "secret-values" you can set:
	//   Params {
	//	DialsTagNameDecoder: caseconversion.DecodeGoCamelCase,
	//	FileFieldNameEncoder: caseconversion.EncodeKebabCase,
	//   }
	// Note that this does not affect the flags or environment variable
	// naming.  To manipulate flag naming, see [Params.FlagConfig].
	FileFieldNameEncoder caseconversion.EncodeCasingFunc
}

Params defines options for the configuration functions within this package All fields can be left empty, zero values will be replaced with sane defaults. As such, it is expected that struct-literals of this type will be sparsely populated in almost all cases.

Jump to

Keyboard shortcuts

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