reader

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 7 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(s *settings)

Option is an option to modify the behavior of the `Get(key string, options ...Option)` method which is used by all methods.

func AcceptEmpty

func AcceptEmpty(accept bool) Option

AcceptEmpty, if set to true, makes the code distinguish between unset keys and empty values. By default, the code does not distinguish between the two cases.

func ForceLowercase

func ForceLowercase(lowercase bool) Option

ForceLowercase forces the string values read from the reader to be lowercased or not, depending on the `lowercase` argument given.

func IsRetro

func IsRetro(currentKey string) Option

IsRetro indicates that all the keys given to the reader function are retro-compatible keys, and that the currentKey given should be used instead.

func RetroKeys

func RetroKeys(retroKeys ...string) Option

RetroKeys specifies a list of keys that are deprecated and replaced by the current key. The oldest retro-compatible key should be placed first in the list of retro keys, so it gets checked first, which is especially important if for example default variable values are set in the program or operating system matching more recent keys. The `handleDeprecatedKey` function is called when a deprecated key is used, with the source name, the deprecated key and the current key as arguments.

type Reader

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

Reader is a settings sources reader and parser.

func New

func New(readerSettings Settings) *Reader

New creates a new reader using the settings given.

func (*Reader) BoolPtr

func (r *Reader) BoolPtr(key string, options ...Option) (boolPtr *bool, err error)

BoolPtr returns a pointer to a `bool` from the value found at the given key.

  • 'true' string values are: "enabled", "yes", "on", "true".
  • 'false' string values are: "disabled", "no", "off", "false".

The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

Otherwise, if the value is not one of the above, an error is returned with the source and key in its message.

func (*Reader) CSV

func (r *Reader) CSV(key string, options ...Option) (values []string)

CSV returns a slice of strings from a comma separated value found at the given key. The entire CSV string value may be modified depending on the parse default settings and the parse options given. The parse default settings are to:

  • Trim line endings suffixes \r\n and \n.
  • Trim spaces.
  • Trim quotes.
  • Force lowercase.

The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVInt

func (r *Reader) CSVInt(key string, options ...Option) (values []int, err error)

CSVInt returns a slice of int from a comma separated value found at the given key and returns an error if any value is not a valid int string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVInt16

func (r *Reader) CSVInt16(key string, options ...Option) (values []int16, err error)

CSVInt16 returns a slice of int16 from a comma separated value found at the given key and returns an error if any value is not a valid int16 string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVInt32

func (r *Reader) CSVInt32(key string, options ...Option) (values []int32, err error)

CSVInt32 returns a slice of int32 from a comma separated value found at the given key and returns an error if any value is not a valid int32 string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVInt64

func (r *Reader) CSVInt64(key string, options ...Option) (values []int64, err error)

CSVInt64 returns a slice of int64 from a comma separated value found at the given key and returns an error if any value is not a valid int64 string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVInt8

func (r *Reader) CSVInt8(key string, options ...Option) (values []int8, err error)

CSVInt8 returns a slice of int8 from a comma separated value found at the given key and returns an error if any value is not a valid int8 string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVNetipAddrPorts

func (r *Reader) CSVNetipAddrPorts(key string, options ...Option) (
	addrPorts []netip.AddrPort, err error)

CSVNetipAddrPorts returns a slice of netip.AddrPort from a comma separated value found at the given key and returns an error if any value is not a valid netip.AddrPort string.

The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

The entire CSV string value may be modified depending on the parse default settings and the parse options given. The parse default settings are to:

  • Trim line endings suffixes \r\n and \n.
  • Trim spaces.
  • Trim quotes.
  • Force lowercase.

func (*Reader) CSVNetipAddresses

func (r *Reader) CSVNetipAddresses(key string, options ...Option) (
	addresses []netip.Addr, err error)

CSVNetipAddresses returns a slice of netip.Addr from a comma separated value found at the given key and returns an error if any value is not a valid netip.Addr string.

The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

The entire CSV string value may be modified depending on the parse default settings and the parse options given. The parse default settings are to:

  • Trim line endings suffixes \r\n and \n.
  • Trim spaces.
  • Trim quotes.
  • Force lowercase.

func (*Reader) CSVNetipPrefixes

func (r *Reader) CSVNetipPrefixes(key string, options ...Option) (
	prefixes []netip.Prefix, err error)

CSVNetipPrefixes returns a slice of netip.Prefix from a comma separated value found at the given key and returns an error if any value is not a valid netip.Prefix string.

The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

The entire CSV string value may be modified depending on the parse default settings and the parse options given. The parse default settings are to:

  • Trim line endings suffixes \r\n and \n.
  • Trim spaces.
  • Trim quotes.
  • Force lowercase.

func (*Reader) CSVUint

func (r *Reader) CSVUint(key string, options ...Option) (values []uint, err error)

CSVUint returns a slice of uint from a comma separated value found at the given key and returns an error if any value is not a valid uint string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVUint16

func (r *Reader) CSVUint16(key string, options ...Option) (values []uint16, err error)

CSVUint16 returns a slice of uint16 from a comma separated value found at the given key and returns an error if any value is not a valid uint16 string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVUint32

func (r *Reader) CSVUint32(key string, options ...Option) (values []uint32, err error)

CSVUint32 returns a slice of uint32 from a comma separated value found at the given key and returns an error if any value is not a valid uint32 string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVUint64

func (r *Reader) CSVUint64(key string, options ...Option) (values []uint64, err error)

CSVUint64 returns a slice of uint64 from a comma separated value found at the given key and returns an error if any value is not a valid uint64 string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) CSVUint8

func (r *Reader) CSVUint8(key string, options ...Option) (values []uint8, err error)

CSVUint8 returns a slice of uint8 from a comma separated value found at the given key and returns an error if any value is not a valid uint8 string. The slice is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) Duration

func (r *Reader) Duration(key string, options ...Option) (
	duration time.Duration, err error)

Duration returns a `time.Duration` from the value found at the given key. If the value is not a valid time.Duration string, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) DurationPtr

func (r *Reader) DurationPtr(key string, options ...Option) (
	durationPtr *time.Duration, err error)

DurationPtr returns a pointer to a `time.Duration` from the value found at the given key. If the value is not a valid time.Duration string, an error is returned with the source and key in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) FirstKeySet

func (r *Reader) FirstKeySet(keys ...string) (firstKeySet string)

FirstKeySet returns the first key set from a list of keys and from the given sources in order. It returns an empty string if none of the keys are set in any of the sources.

func (*Reader) Float32

func (r *Reader) Float32(key string, options ...Option) (f float32, err error)

Float32 returns a `float32` from the value found at the given key. If the value is not a valid float64 string, an error is returned with the source and key in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Float32Ptr

func (r *Reader) Float32Ptr(key string, options ...Option) (
	pointer *float32, err error)

Float32Ptr returns a pointer to a `float32` from the value found at the given key. If the value is not a valid float32 string, an error is returned with the source and key in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Float64

func (r *Reader) Float64(key string, options ...Option) (f float64, err error)

Float64 returns a `float64` from the value found at the given key. If the value is not a valid float64 string, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Float64Ptr

func (r *Reader) Float64Ptr(key string, options ...Option) (
	pointer *float64, err error)

Float64Ptr returns a pointer to a `float64` from the value found at the given key. If the value is not a valid float64 string, an error is returned with the source and key in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Get

func (r *Reader) Get(key string, options ...Option) (value *string)

Get returns a value found at the given key as a string pointer.

The value is returned as `nil` if:

  • the key given is NOT set.
  • By default and unless changed by the AcceptEmpty option, if the key is set and its corresponding value is empty.

Otherwise, the value may be modified depending on the parse default settings and the parse options given.

The parse default settings are to:

  • Trim line endings suffixes \r\n and \n.
  • Trim spaces.
  • Trim quotes.
  • Force lowercase.

func (*Reader) Int

func (r *Reader) Int(key string, options ...Option) (n int, err error)

Int returns an `int` from the value found at the given key. If the value is not a valid integer string, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Int16

func (r *Reader) Int16(key string, options ...Option) (n int16, err error)

Int16 returns an `int16` from the value found at the given key. If the value is not an integer string between -32768 and 32767, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Int16Ptr

func (r *Reader) Int16Ptr(key string, options ...Option) (
	pointer *int16, err error)

Int16Ptr returns a pointer to an `int16` from the value found at the given key. If the value is not an integer string between -32768 to 32767, an error is returned with the key kind and name in its message. The value is returned as `nil` if:

  • the key given is NOT set in any of the sources.
  • By default and unless changed by the AllowEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) Int32

func (r *Reader) Int32(key string, options ...Option) (n int32, err error)

Int32 returns an `int32` from the value found at the given key. If the value is not an integer string between -2147483648 and 2147483647, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Int32Ptr

func (r *Reader) Int32Ptr(key string, options ...Option) (
	pointer *int32, err error)

Int32Ptr returns a pointer to an `int32` from the value found at the given key. If the value is not an integer string between -2147483648 to 2147483647, an error is returned with the key kind and name in its message. The value is returned as `nil` if:

  • the key given is NOT set in any of the sources.
  • By default and unless changed by the AllowEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) Int64

func (r *Reader) Int64(key string, options ...Option) (n int64, err error)

Int64 returns an `int64` from the value found at the given key. If the value is not an integer string between -2^63 and 2^63 - 1, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Int64Ptr

func (r *Reader) Int64Ptr(key string, options ...Option) (
	pointer *int64, err error)

Int64Ptr returns a pointer to an `int64` from the value found at the given key. If the value is not an integer string between -2^63 to 2^63 - 1, an error is returned with the key kind and name in its message. The value is returned as `nil` if:

  • the key given is NOT set in any of the sources.
  • By default and unless changed by the AllowEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) Int8

func (r *Reader) Int8(key string, options ...Option) (n int8, err error)

Int8 returns an `int8` from the value found at the given key. If the value is not an integer string between -128 and 127, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Int8Ptr

func (r *Reader) Int8Ptr(key string, options ...Option) (
	pointer *int8, err error)

Int8Ptr returns a pointer to an `int8` from the value found at the given key. If the value is not an integer string between -128 to 127, an error is returned with the key kind and name in its message. The value is returned as `nil` if:

  • the key given is NOT set in any of the sources.
  • By default and unless changed by the AllowEmpty option, if the key is set and its corresponding value is empty.

func (*Reader) IntPtr

func (r *Reader) IntPtr(key string, options ...Option) (intPtr *int, err error)

IntPtr returns a pointer to an `int` from the value found at the given key. If the value is not a valid integer string, an error is returned with the source and key in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) NetipAddr

func (r *Reader) NetipAddr(key string, options ...Option) (
	addr netip.Addr, err error)

NetipAddr returns a netip.Addr from the value found at the given key. If the value is not a valid netip.Addr string, an error is returned with the source and key in its message. The value is returned as the empty invalid `netip.Addr{}` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) NetipAddrPort

func (r *Reader) NetipAddrPort(key string, options ...Option) (
	addr netip.AddrPort, err error)

NetipAddrPort returns a netip.AddrPort from the value found at the given key. If the value is not a valid netip.AddrPort string, an error is returned with the source and key in its message. The value is returned as the empty invalid `netip.AddrPort{}` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) NetipPrefix

func (r *Reader) NetipPrefix(key string, options ...Option) (
	addr netip.Prefix, err error)

NetipPrefix returns a netip.Prefix from the value found at the given key. If the value is not a valid netip.Prefix string, an error is returned with the source and key in its message. The value is returned as the empty invalid `netip.Prefix{}` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) String

func (r *Reader) String(key string, options ...Option) (value string)

String returns a string from the value found at the given key, which may be modified depending on the parse default settings and the parse options given. The parse default settings are to:

  • Trim line endings suffixes \r\n and \n.
  • Trim spaces.
  • Trim quotes.
  • Force lowercase.

If the key is not set, the empty string is returned.

func (*Reader) Uint

func (r *Reader) Uint(key string, options ...Option) (n uint, err error)

Uint returns an `uint` from the value found at the given key. If the value is not a valid unsigned integer string, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Uint16

func (r *Reader) Uint16(key string, options ...Option) (n uint16, err error)

Uint16 returns an `uint16` from the value found at the given key. If the value is not an integer string between 0 and 65535, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Uint16Ptr

func (r *Reader) Uint16Ptr(key string, options ...Option) (
	uint16Ptr *uint16, err error)

Uint16Ptr returns a pointer to an `uint16` from the value found at the given key. If the value is not a valid integer string between 0 and 65535, an error is returned with the key its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Uint32

func (r *Reader) Uint32(key string, options ...Option) (n uint32, err error)

Uint32 returns an `uint32` from the value found at the given key. If the value is not an integer string between 0 and 4294967295, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Uint32Ptr

func (r *Reader) Uint32Ptr(key string, options ...Option) (
	uint32Ptr *uint32, err error)

Uint32Ptr returns a pointer to an `uint32` from the value found at the given key. If the value is not a valid integer string between 0 and 4294967295 an error is returned with the source and key in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Uint64

func (r *Reader) Uint64(key string, options ...Option) (n uint64, err error)

Uint64 returns an `uint64` from the value found at the given key. If the value is not a valid integer string bigger than 0, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Uint64Ptr

func (r *Reader) Uint64Ptr(key string, options ...Option) (
	pointer *uint64, err error)

Uint64Ptr returns a pointer to an `uint64` from the value found at the given key. If the value is not a valid integer string bigger than 0, an error is returned with the source and key in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Uint8

func (r *Reader) Uint8(key string, options ...Option) (n uint8, err error)

Uint8 returns an `uint8` from the value found at the given key. If the value is not an integer string between 0 and 255, an error is returned with the source and key in its message. The value is returned as `0` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) Uint8Ptr

func (r *Reader) Uint8Ptr(key string, options ...Option) (uint8Ptr *uint8, err error)

Uint8Ptr returns a pointer to an `uint8` from the value found at the given key. If the value is not a valid integer string between 0 and 255, an error is returned with the source and key in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

func (*Reader) UintPtr

func (r *Reader) UintPtr(key string, options ...Option) (
	pointer *uint, err error)

UintPtr returns a pointer to an `uint` from the value found at the given key. If the value is not a valid unsigned integer string matching the current system architecture, an error is returned with the key name and source name in its message. The value is returned as `nil` if:

  • the given key is NOT set.
  • By default and unless changed by the AllowEmpty option, if the given key is set and its corresponding value is empty.

type Settings

type Settings struct {
	// Sources is a slice of sources where a source at
	// a lower index has a higher priority.
	// It defaults to:
	// []reader.Source{flag.New(os.Args), env.New(env.Settings{Environ: os.Environ()})}
	Sources []Source
	// HandleDeprecatedKey is called when encountering a deprecated
	// key, and defaults to a no-op function.
	HandleDeprecatedKey func(source, deprecatedKey, currentKey string)
	// DefaultOptions are the default options to use for every method call.
	// They default to ForceLowercase(true), AcceptEmpty(false).
	DefaultOptions []Option
}

Settings is the settings to create a new reader.

type Source

type Source interface {
	// String can return for example 'environment variable' or 'flag'.
	// It should be singular so it can be used in error messages
	// together with the key to serve as a source kind, for example:
	// environment variable ENV_KEY: some problem
	String() string
	// Get returns the value of the key and whether it is set.
	Get(key string) (value string, isSet bool)
	// KeyTransform transforms a standardized key to a key specific to
	// the source. For example SERVER_ADDRESS becomes server-address for
	// the flags source.
	KeyTransform(key string) string
}

Source is a named key-value source.

Directories

Path Synopsis
sources
env

Jump to

Keyboard shortcuts

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