reader

package
v0.4.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 6 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 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) CSVNetipAddresses

func (r *Reader) CSVNetipAddresses(key string, options ...Option) (
	prefixes []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) 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 `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) 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) 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) 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) 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) 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.

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{env.New(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(false), 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
Package parse provides generic functions to parse values from strings found from given sources interfaces.
Package parse provides generic functions to parse values from strings found from given sources interfaces.
sources
env

Jump to

Keyboard shortcuts

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