envstruct

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2025 License: MIT Imports: 8 Imported by: 0

README

EnvStruct

EnvStruct simplifies configuring applications via environment variables. It provides tools to unmarshal structured configuration directly from the environment into a struct, with support for custom parsers and help documentation.

Installation

To use EnvStruct, install it via go get:

go get github.com/bobcatalyst/envstruct

Usage

Defining a Configuration Struct

Define your application's configuration as a Go struct with env tags for environment variable names:

package main

import (
    "envstruct"
    "fmt"
)

type Config struct {
    Goroot string        `env:"GOROOT" description:"Go root directory" parser:"absFile"`
    Foo    string        `env:"FOO" description:"An example variable" default:"bar" parser:"string"`
    Help   func() string `env-help:""`
}

func main() {
    cfg, err := envstruct.Unmarshal[Config]()
    if err != nil {
        panic(err)
    }

    fmt.Println("GOROOT:", cfg.Goroot)
    fmt.Println("FOO:", cfg.Foo)
    fmt.Println("Help:", cfg.Help())
}
Supported Parsers

EnvStruct includes several built-in parsers:

  • string: Parses strings (default parser).
  • absFile: Resolves file paths to their absolute form.
  • args: Parses command-line arguments.
  • ipv4: Validates and parses IPv4 addresses.
  • port: Validates and parses network port numbers.
Adding Custom Parsers

You can add your own parsers by implementing the Parser interface:

package parsers

type MyCustomParser struct{}

func (MyCustomParser) Name() string { return "myCustomParser" }
func (MyCustomParser) Parse(s string) (MyType, error) {
    // Custom parsing logic here
}

Then register the parser:

package main

import (
    "envstruct"
    "yourmodule/parsers"
)

func init() {
    envstruct.RegisterParser[parsers.MyCustomParser]()
}
Generating Help Documentation

EnvStruct automatically generates help documentation for environment variables. The env-help tag allows mapping a function to retrieve the help string:

Help   func() string `env-help:""`

Call the function in your application to display usage information:

fmt.Println(cfg.Help())
Using .env Files

Place your environment variables in a .env file, and EnvStruct will load them automatically:

GOROOT=/usr/local/go
FOO=myValue

Documentation

Index

Constants

View Source
const (
	TagEnv         = "env"
	TagEnvHelp     = "env-help"
	TagDescription = "description"
	TagDefault     = "default"
	TagParser      = "parser"
)

Variables

View Source
var (
	ErrInvalid          = errors.New("nil value")
	ErrNotStructPointer = errors.New("not a struct")
)
View Source
var ErrNotFound = errors.New("env value not found")

Functions

func RegisterParser

func RegisterParser[P Parser[T], T any]()

func Unmarshal

func Unmarshal[T any]() (v T, _ error)

Types

type Default added in v0.0.3

type Default[T any] interface {
	Parser[T]
	Default() string
}

type ErrMismatchedParseTypes

type ErrMismatchedParseTypes struct {
	Parser reflect.Type
	Struct reflect.Type
}

func (*ErrMismatchedParseTypes) Error

func (err *ErrMismatchedParseTypes) Error() string

type ErrOpFail

type ErrOpFail[Op fmt.Stringer] struct {
	Op    Op
	Field reflect.StructField
	Err   error
}

func (*ErrOpFail[Op]) Error

func (err *ErrOpFail[Op]) Error() string

func (*ErrOpFail[Op]) Unwrap

func (err *ErrOpFail[Op]) Unwrap() error

type ErrParserNotFound

type ErrParserNotFound struct {
	Name string
}

func (*ErrParserNotFound) Error

func (err *ErrParserNotFound) Error() string

type ErrTagNotSet

type ErrTagNotSet struct {
	Name string
}

func (*ErrTagNotSet) Error

func (err *ErrTagNotSet) Error() string

type NamedType added in v0.0.5

type NamedType[T any] interface {
	Parser[T]
	TypeName() string
}

type OpReadTags

type OpReadTags struct{}

func (OpReadTags) String

func (OpReadTags) String() string

type Parser

type Parser[T any] interface {
	Name() string
	Parse(string) (T, error)
}

type TagOp

type TagOp string

type TagOpParse

type TagOpParse struct{ Name string }

func (TagOpParse) String

func (top TagOpParse) String() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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