flat

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package flat provides a flat view of an arbitrary nested structs.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedType = errors.New("unexpected type, expecting a pointer to struct")

ErrUnexpectedType is returned when flatten sees an unsupported type.

Functions

func ExpandContainersFromKeys added in v0.4.1

func ExpandContainersFromKeys(conf any, globalPrefix string, keys []string) (map[string]string, error)

ExpandContainersFromKeys mutates conf, growing slice-of-struct fields (by numeric index) and maps (by key) based on the provided UPPER_SNAKE_CASE keys. The same convention used by the env plugin applies — keys for slice elements look like <PREFIX>_<N>_<TAIL>; for primitive maps <PREFIX>_<KEY>; for struct-valued maps <PREFIX>_<KEY>_<TAIL> where TAIL matches one of the inner struct's leaf env names (longest match wins).

Pointer element types ([]*T, map[string]*T) are supported — empty slots are allocated as &T{}.

Returns a map from each leaf field's flat path (e.g. "Items.0.Host") to its computed env-style name (e.g. "ITEMS_0_HOST"). Both env and Vault plugins use this to look up values by env-style name after re-flattening the (now expanded) conf.

globalPrefix is uppercased and prepended (with an underscore) to all top-level field env names. Pass "" if not using a prefix.

func MakeEnvName added in v0.4.1

func MakeEnvName(globalPrefix, name string) string

MakeEnvName prepends globalPrefix (uppercased) to name with an underscore separator, matching the convention used by ExpandContainersFromKeys.

Types

type Field

type Field interface {
	Name() string
	EnvName() string
	Tag(key string) (string, bool)
	ParentTag() reflect.StructTag

	Meta() map[string]string

	String() string
	Set(value string) error
	IsZero() bool

	FieldValue() reflect.Value
	FieldType() reflect.StructField
}

Field describe an interface to our flat structs fields.

type Fields

type Fields []Field

Fields is a slice of Field.

func View

func View(s any) (Fields, error)

View provides a flat view of the provided structs an array of fields. sub-struct fields are prefixed with the struct key (not type) followed by a dot, this is repeated for each nested level.

Jump to

Keyboard shortcuts

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