Documentation ¶
Overview ¶
Package env provides conversion from structure to and from environment variables.
It supports converting struct fields to environment variables using field tags, handling different data types, and transforming strings between different case conventions, which is useful for generating or parsing environment variables, JSON tags, or command line flags.
The package also defines several case conversion functions that aid in manipulating strings to fit conventional casing for various programming and configuration contexts. Additionally, it provides functions to serialize structs into slices of key-value pairs where the keys are derived from struct field names transformed to upper snake case by default, or specified explicitly via struct field tags.
It also includes functionality to deserialize environment variables back into struct fields, handling pointers and nested structs appropriately, as well as providing shell-compatible output for environment variable definitions.
The package leverages reflection to dynamically handle arbitrary struct types, and has 0 dependencies.
Index ¶
- func CamelCaseToLowerKebabCase(s string) string
- func CamelCaseToLowerSnakeCase(s string) string
- func CamelCaseToUpperSnakeCase(s string) string
- func SerializeValue(value interface{}) string
- func SetFromEnv(prefix string, s interface{}) []error
- func ShellQuote(input string) string
- func SplitByCase(input string) []string
- func ToShell(kvl []KeyValue) string
- func ToShellWithPrefix(prefix string, kvl []KeyValue) string
- type KeyValue
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CamelCaseToLowerKebabCase ¶
CamelCaseToLowerKebabCase converts a string from camelCase or CamelCase to lower-kebab-case. Handles cases like HTTPServer -> http-server. Good for command line flags for instance.
func CamelCaseToLowerSnakeCase ¶
CamelCaseToLowerSnakeCase converts a string from camelCase or CamelCase to lowe_snake_case. Handles cases like HTTPServer -> http_server. Good for JSON tags for instance.
func CamelCaseToUpperSnakeCase ¶
CamelCaseToUpperSnakeCase converts a string from camelCase or CamelCase to UPPER_SNAKE_CASE. Handles cases like HTTPServer -> HTTP_SERVER and httpServer -> HTTP_SERVER. Good for environment variables.
func SerializeValue ¶
func SerializeValue(value interface{}) string
func SetFromEnv ¶
func ShellQuote ¶ added in v0.1.1
Escape characters such as the result string can be embedded as a single argument in a shell fragment e.g for ENV_VAR=<value> such as <value> is safe (no $(cmd...) no ` etc`).
func SplitByCase ¶
Split strings into words, using CamelCase/camelCase/CAMELCase rules.
func ToShellWithPrefix ¶
This convert the key value pairs to bourne shell syntax (vs newer bash export FOO=bar).
Types ¶
type KeyValue ¶
type KeyValue struct { Key string // Must be safe (is when coming from Go struct names but could be bad with env:). QuotedValue string // (Must be) Already quoted/escaped. }
Intermediate result list from StructToEnvVars(), both the Key and QuotedValue must be shell safe/non adversarial as they are emitted as is by String() with = in between. Using StructToEnvVars produces safe values even with adversarial input (length notwithstanding).
func StructToEnvVars ¶
StructToEnvVars converts a struct to a map of environment variables. The struct can have a `env` tag on each field. The tag should be in the format `env:"ENV_VAR_NAME"`. The tag can also be `env:"-"` to exclude the field from the map. If the field is exportable and the tag is missing we'll use the field name converted to UPPER_SNAKE_CASE (using CamelCaseToUpperSnakeCase()) as the environment variable name.