envx

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Example (Env)
envs := [][2]string{
	{"EXAMPLE__ADDRESS", "asset://group/filename.png"},
	{"EXAMPLE__DATABASE_0", "mysql://u:p@host1:3306/db1?ssl=off"},
	{"EXAMPLE__DATABASE_1", "mysql://u:p@host2:3306/db2?ssl=off"},
	{"EXAMPLE__Endpoint", "http://username:password@localhost:8888/root?key=value1&key=value2"},
	{"EXAMPLE__Password", "password"},
	{"EXAMPLE__REDIS_instance1", "redis://u1:p1@host1:3306/1"},
	{"EXAMPLE__REDIS_instance2", "redis://u2:p2@host2:3306/2"},
}
for _, v := range envs {
	_ = os.Setenv(v[0], v[1])
}

defer func() {
	for _, v := range envs {
		_ = os.Unsetenv(v[0])
	}
}()

target := TestData{}

// read configurations from env vars and decode to target
dec := envx.NewDecoder(envx.ParseGroupFromEnv("EXAMPLE"))
must.NoError(dec.Decode(&target))

// encode target
grp := envx.NewGroup("EXAMPLE")
enc := envx.NewEncoder(grp)
must.NoError(enc.Encode(target))

// output dotenv raw and masked
fmt.Println(string(grp.Bytes()))
fmt.Println(string(grp.MaskBytes()))
Output:

EXAMPLE__ADDRESS=asset://group/filename.png
EXAMPLE__DATABASE_0=mysql://u:p@host1:3306/db1?ssl=off
EXAMPLE__DATABASE_1=mysql://u:p@host2:3306/db2?ssl=off
EXAMPLE__Endpoint=http://username:password@localhost:8888/root?key=value1&key=value2
EXAMPLE__NilPointer=0
EXAMPLE__Password=password
EXAMPLE__REDIS_instance1=redis://u1:p1@host1:3306/1
EXAMPLE__REDIS_instance2=redis://u2:p2@host2:3306/2

EXAMPLE__ADDRESS=asset://group/filename.png
EXAMPLE__DATABASE_0=mysql://u:--------@host1:3306/db1?ssl=off
EXAMPLE__DATABASE_1=mysql://u:--------@host2:3306/db2?ssl=off
EXAMPLE__Endpoint=http://username:--------@localhost:8888/root?key=value1&key=value2
EXAMPLE__NilPointer=0
EXAMPLE__Password=--------
EXAMPLE__REDIS_instance1=redis://u1:--------@host1:3306/1
EXAMPLE__REDIS_instance2=redis://u2:--------@host2:3306/2

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	TextMarshallerT   = reflect.TypeFor[encoding.TextMarshaler]()
	TextUnmarshallerT = reflect.TypeFor[encoding.TextUnmarshaler]()
)

Functions

func DotEnv

func DotEnv(values map[string]string) []byte

Types

type Code

type Code int8
const (
	E_DEC__INVALID_VALUE Code = iota + 1
	E_DEC__INVALID_VALUE_CANNOT_SET
	E_DEC__INVALID_MAP_KEY_TYPE
	E_DEC__FAILED_UNMARSHAL
	E_ENC__INVALID_MAP_KEY_TYPE
	E_ENC__INVALID_MAP_KEY_VALUE
	E_ENC__INVALID_ENV_KEY
	E_ENC__FAILED_MARSHAL
	E_ENC__DUPLICATE_GROUP_KEY
)

func (Code) Message

func (c Code) Message() string

type Decoder

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

func NewDecoder

func NewDecoder(g *Group) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v any) error

type Encoder

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

func NewEncoder

func NewEncoder(g *Group) *Encoder

func (*Encoder) Encode

func (d *Encoder) Encode(v any) error

type Group

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

func NewGroup

func NewGroup(name string) *Group

func ParseGroupFromEnv

func ParseGroupFromEnv(prefix string) *Group

func (*Group) Add

func (g *Group) Add(v *Var) bool

func (*Group) Bytes

func (g *Group) Bytes() []byte

func (*Group) Get

func (g *Group) Get(key string) *Var

func (*Group) Key

func (g *Group) Key(key string) string

func (*Group) Len

func (g *Group) Len() int

func (*Group) MapEntries

func (g *Group) MapEntries(k string) []string

func (*Group) MaskBytes

func (g *Group) MaskBytes() []byte

func (*Group) Name

func (g *Group) Name() string

func (*Group) SliceLength

func (g *Group) SliceLength(k string) int

func (*Group) Values

func (g *Group) Values() map[string]*Var

type PathWalker

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

func NewPathWalker

func NewPathWalker() *PathWalker

func (*PathWalker) Enter

func (pw *PathWalker) Enter(i any)

func (*PathWalker) Leave

func (pw *PathWalker) Leave()

func (*PathWalker) Paths

func (pw *PathWalker) Paths() []any

func (*PathWalker) String

func (pw *PathWalker) String() string

type Var

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

func NewVar

func NewVar(key, val string) *Var

func (*Var) Key

func (v *Var) Key() string

func (*Var) Optional

func (v *Var) Optional() bool

func (*Var) Value

func (v *Var) Value() string

Jump to

Keyboard shortcuts

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