nflex

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2022 License: MIT Imports: 10 Imported by: 1

README

nflex - common interface to parsed config files

GoDoc unit tests report card codecov

Install:

go get github.com/muir/nflex

Nflex is a common wrapper around multiple configuration file unpackers. It is lower-level than just unpacking into an map[string]interface{} and thus avoids adding incorrect type information to YAML files that are parsed that way.

It currently supports:

  • YAML
  • JSON

It supports merging data from multiple files.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDoesNotExist = fmt.Errorf("requested item does not exist")
View Source
var ErrWrongType = fmt.Errorf("requested item is not the requested type")

Functions

func IsUnknownFileTypeError

func IsUnknownFileTypeError(err error) bool

func UnknownFileTypeError

func UnknownFileTypeError(err error) error

UnknownFileType annotates an error as being caused by not knowing the file type.

Types

type CanMutate

type CanMutate interface {
	Source
	Mutate(Mutation) Source
}

CanMutate must be implemented by sources that wrap or contain other sources. The Mutate method allows the wrapped sources to be rewrapped or modifed in some way. The mutation only applies to the inner source, not the source that CanMutate.

type MultiSource

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

func NewMultiSource

func NewMultiSource(sources ...Source) *MultiSource

NewMultiSource creates a source that is the combination of multiple sources. For containers (maps, slices) this new source combines the elements from the provided sources. For scalar values, it takes its value from the first source that has a value present for the field in question.

func (*MultiSource) AddSource

func (m *MultiSource) AddSource(source Source)

AddSource adds an additional source to a MultiSource, modifying the MultiSource

func (*MultiSource) Copy

func (m *MultiSource) Copy() *MultiSource

func (*MultiSource) Exists

func (m *MultiSource) Exists(keys ...string) bool

func (*MultiSource) GetBool

func (m *MultiSource) GetBool(keys ...string) (bool, error)

func (*MultiSource) GetFloat

func (m *MultiSource) GetFloat(keys ...string) (float64, error)

func (*MultiSource) GetInt

func (m *MultiSource) GetInt(keys ...string) (int64, error)

func (*MultiSource) GetString

func (m *MultiSource) GetString(keys ...string) (string, error)

func (*MultiSource) Keys

func (m *MultiSource) Keys(keys ...string) ([]string, error)

func (*MultiSource) Len

func (m *MultiSource) Len(keys ...string) (int, error)

func (*MultiSource) Mutate

func (m *MultiSource) Mutate(mutation Mutation) Source

func (*MultiSource) Recurse

func (m *MultiSource) Recurse(keys ...string) Source

func (*MultiSource) Type

func (m *MultiSource) Type(keys ...string) NodeType

type Mutation

type Mutation func(Source) Source

Mutation is a function that transforms a source into a new source

func MultiSourceSetCombine

func MultiSourceSetCombine(combine bool) Mutation

MultiSourceSetCombine sets the behvior for which how many sources are used for slices and maps in a MultiSource. The default is true: slices and maps are combined. Slices are appended and maps are combined.

If combine is false there can be some surprising behavior because paths may exist beyond what keys says. For example, suppose we have two objects:

one:
	map:
		key1: value1
two:
	map:
		key2: value2

With combine=false, keys(map) = [key1] but lookup(map.key2) = value2

func MultiSourceSetFirst

func MultiSourceSetFirst(first bool) Mutation

MultiSourceSetFirst sets the priority for which source is evaluated first for MultiSource sources. For scalars (int, string, etc), the first evaluated source that has a value is the value returned. The default is first=true which means that the first (not last) source wins.

func (Mutation) Apply

func (m Mutation) Apply(source Source) Source

Apply transforms sources and the sources embedded in other sources into a new source by applying the Mutation to both.

func (Mutation) Combine

func (m Mutation) Combine(n Mutation) Mutation

Combine turns two mutations into a single mutation which provides some efficincy when applying -- it's less effort to apply a combined pair of muations that to apply them serially.

type NodeType

type NodeType int
const (
	Undefined NodeType = iota // node does not exist
	Slice
	Nil
	String
	Float
	Int
	Bool
	Map
)

func (NodeType) String added in v0.1.6

func (i NodeType) String() string

type Source

type Source interface {
	Exists(keys ...string) bool
	GetBool(keys ...string) (bool, error)
	GetInt(keys ...string) (int64, error)
	GetFloat(keys ...string) (float64, error)
	GetString(keys ...string) (string, error)
	Recurse(keys ...string) Source // can return nil
	Keys(keys ...string) ([]string, error)
	Len(keys ...string) (int, error)
	Type(keys ...string) NodeType
}

Source is an abstraction of model encodings. If you need to work with arbitray encodings of data models, you either need to unpack the encoded data into a interface{} or you need an abstraction. This is such an abstraction.

func CombineSources

func CombineSources(sources ...Source) Source

CombineSources expects any MultiSource to be the first source provided. Nil sources are allowed and filtered out. The result may be nil if all inputs are nil.

func NewPrefixSource

func NewPrefixSource(source Source, prefix ...string) Source

NewPrefixSource wraps a Source such that it applies a path prefix to the entire source.

func UnmarshalFile

func UnmarshalFile(file string, args ...UnmarshalFileArg) (Source, error)

func UnmarshalJSON

func UnmarshalJSON(data []byte) (Source, error)

func UnmarshalYAML

func UnmarshalYAML(data []byte) (Source, error)

func WithOffset added in v0.1.0

func WithOffset(source Source, offsets ...int) Source

WithOffset returns a modified source that offsets slice lookups such that it requires a larger key to do the lookup.

type UnmarshalFileArg

type UnmarshalFileArg func(*unmarshalOpts)

func WithFS

func WithFS(fs fs.FS) UnmarshalFileArg

Jump to

Keyboard shortcuts

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