envtag

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package envtag provides tools to parse tags from strings or struct fields.

Index

Constants

View Source
const (
	EnvKey     = "env"
	DefaultKey = "default"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error added in v0.4.0

type Error struct {
	TagString   string
	Unsupported []string
}

func (*Error) Error added in v0.4.0

func (e *Error) Error() string

type FieldNameNormalizer added in v0.4.0

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

FieldNameNormalizer normalizes struct field names to a format that is commonly used in environment variables, e.g. MyFieldName becomes MY_FIELD_NAME.

func (*FieldNameNormalizer) Normalize added in v0.4.0

func (n *FieldNameNormalizer) Normalize(fieldName, prefix string) string

type Normalizer added in v0.4.0

type Normalizer interface {
	Normalize(str, prefix string) string
}

type NormalizerFunc added in v0.4.0

type NormalizerFunc func(fieldName, prefix string) string

func (NormalizerFunc) Normalize added in v0.4.0

func (fn NormalizerFunc) Normalize(fieldName, prefix string) string

type Options added in v0.4.0

type Options struct {
	// EnvKey is used to look up the env tag string from a reflect.StructTag.
	EnvKey string
	// DefaultKey is used to look up the default value string from a
	// reflect.StructTag.
	DefaultKey string
	// Normalizer is used to normalize a reflect.StructField's name
	// when no name is provided in the env tag string.
	Normalizer Normalizer
	// StrictTags ignores fields that do not have an env tag when set to true.
	StrictTags bool
}

func DefaultOptions added in v0.4.0

func DefaultOptions() Options

DefaultOptions returns an Options with default values.

type Tag

type Tag struct {
	// Name of the tag which will be used to construct the environment
	// variable's full name.
	Name string
	// Default is an optional default value for the field.
	Default string
	// Ignore indicates the field should be ignored.
	Ignore bool
	// Inline indicates the field will be "flattened" when it is a struct. This
	// means it's child fields will be treated as if they are part of the parent
	// struct.
	Inline bool
	// Include indicates the structs child fields should be included, even if
	// they do not have an env tag and Options.StrictTag is set to true.
	Include bool
}

func ParseStructField added in v0.4.0

func ParseStructField(opts Options, field reflect.StructField, prefix string) (tag Tag, err error)

ParseStructField uses the field's reflect.StructTag to lookup the tag string according to the provided Options. It will always return a usable Tag, even if an error has occurred.

func ParseTag

func ParseTag(str string) (Tag, error)

ParseTag parses str into a Tag. It will always return a usable Tag, even if an error has occurred.

func (Tag) IsEmpty

func (t Tag) IsEmpty() bool

IsEmpty indicates if Tag is considered empty.

func (Tag) ShouldIgnore added in v0.4.0

func (t Tag) ShouldIgnore() bool

ShouldIgnore indicates if Tag should be ignored.

Jump to

Keyboard shortcuts

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