envconf

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DotEnv added in v0.0.2

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

func StringifyPath

func StringifyPath(paths ...any) string

Types

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
Example
grp := envconf.NewGroup("TEST")

grp.Add(envconf.NewVar("MapStringString_Key", "Value"))
grp.Add(envconf.NewVar("MapStringInt_Key", "100"))
grp.Add(envconf.NewVar("MapStringInt64_Key", "101"))
grp.Add(envconf.NewVar("MapStringPassword_Key", "password"))
grp.Add(envconf.NewVar("Slice_0_A", "2"))
grp.Add(envconf.NewVar("Slice_0_B", "string"))
grp.Add(envconf.NewVar("Array_0_A", "string"))
grp.Add(envconf.NewVar("Array_0_B", "2"))

dec := envconf.NewDecoder(grp)
enc := envconf.NewEncoder(grp)

v := &struct {
	MapStringString   map[string]string
	MapStringInt      map[string]int
	MapStringInt64    map[string]int64
	MapStringPassword map[string]datatypes.Password
	Slice             []struct {
		A int
		B string
	}
	Array [1]struct {
		B int
		A string
	}
}{}

if err := dec.Decode(v); err != nil {
	return
}
if err := enc.Encode(v); err != nil {
	return
}

demoVar := envconf.NewVar("VarName", "")
fmt.Println(demoVar.GroupName(grp.Name))
fmt.Println(string(grp.Bytes()))
fmt.Println(string(grp.MaskBytes()))
Output:

TEST__VarName
TEST__Array_0_A=string
TEST__Array_0_B=2
TEST__MapStringInt64_Key=101
TEST__MapStringInt_Key=100
TEST__MapStringPassword_Key=password
TEST__MapStringString_Key=Value
TEST__Slice_0_A=2
TEST__Slice_0_B=string

TEST__Array_0_A=string
TEST__Array_0_B=2
TEST__MapStringInt64_Key=101
TEST__MapStringInt_Key=100
TEST__MapStringPassword_Key=--------
TEST__MapStringString_Key=Value
TEST__Slice_0_A=2
TEST__Slice_0_B=string

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 ErrInvalidDecodeValue

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

func NewCannotSetErr

func NewCannotSetErr(t reflect.Type) *ErrInvalidDecodeValue

func NewInvalidDecodeError

func NewInvalidDecodeError(t reflect.Type, reason string) *ErrInvalidDecodeValue

func NewInvalidValueErr

func NewInvalidValueErr() *ErrInvalidDecodeValue

func (*ErrInvalidDecodeValue) Error

func (e *ErrInvalidDecodeValue) Error() string

func (*ErrInvalidDecodeValue) Is

func (e *ErrInvalidDecodeValue) Is(target error) bool

type ErrUnexpectMapKeyType

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

func NewErrMarshalUnexpectMapKeyType

func NewErrMarshalUnexpectMapKeyType(t reflect.Type) *ErrUnexpectMapKeyType

func NewErrUnmarshalUnexpectMapKeyType

func NewErrUnmarshalUnexpectMapKeyType(t reflect.Type) *ErrUnexpectMapKeyType

func (*ErrUnexpectMapKeyType) Error

func (e *ErrUnexpectMapKeyType) Error() string

func (*ErrUnexpectMapKeyType) Is

func (e *ErrUnexpectMapKeyType) Is(target error) bool

type ErrUnexpectMapKeyValue

type ErrUnexpectMapKeyValue string

func (ErrUnexpectMapKeyValue) Error

func (e ErrUnexpectMapKeyValue) Error() string

type Group

type Group struct {
	Name   string
	Values map[string]*Var
}

func NewGroup

func NewGroup(name string) *Group

func ParseGroupFromEnv added in v0.0.2

func ParseGroupFromEnv(prefix string) *Group
Example
package main

import (
	"fmt"
	"os"

	"github.com/sincospro/conf/envconf"
)

func main() {
	_ = os.Setenv("TEST__Array_0_A", "string")
	_ = os.Setenv("TEST__Array_0_B", "2")
	_ = os.Setenv("TEST__MapStringInt64_Key", "101")
	_ = os.Setenv("TEST__MapStringInt_Key", "100")
	_ = os.Setenv("TEST__MapStringPassword_Key", "password")
	_ = os.Setenv("TEST__MapStringString_Key", "Value")
	_ = os.Setenv("TEST__Slice_0_A", "2")
	_ = os.Setenv("TEST__Slice_0_B", "string")

	grp := envconf.ParseGroupFromEnv("TEST")

	_ = os.Unsetenv("TEST__Array_0_A")
	_ = os.Unsetenv("TEST__Array_0_B")
	_ = os.Unsetenv("TEST__MapStringInt64_Key")
	_ = os.Unsetenv("TEST__MapStringInt_Key")
	_ = os.Unsetenv("TEST__MapStringPassword_Key")
	_ = os.Unsetenv("TEST__MapStringString_Key")
	_ = os.Unsetenv("TEST__Slice_0_A")
	_ = os.Unsetenv("TEST__Slice_0_B")

	fmt.Print(string(grp.DotEnv(nil)))

}
Output:

TEST__Array_0_A=string
TEST__Array_0_B=2
TEST__MapStringInt64_Key=101
TEST__MapStringInt_Key=100
TEST__MapStringPassword_Key=password
TEST__MapStringString_Key=Value
TEST__Slice_0_A=2
TEST__Slice_0_B=string

func (*Group) Add

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

func (*Group) Bytes added in v0.0.2

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

func (*Group) Del

func (g *Group) Del(name string)

func (*Group) DotEnv added in v0.0.2

func (g *Group) DotEnv(valuer func(*Var) string) []byte

func (*Group) Get

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

func (*Group) Len

func (g *Group) Len() int

func (*Group) MapEntries

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

func (*Group) MaskBytes added in v0.0.2

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

func (*Group) Reset

func (g *Group) Reset()

func (*Group) SliceLength

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

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 {
	Name  string
	Value string
	Mask  string

	VarOptions
}

func NewVar

func NewVar(name, value string) *Var

func (*Var) GroupName

func (v *Var) GroupName(prefix string) string

func (*Var) ParseOption

func (v *Var) ParseOption(flag map[string]struct{})

type VarOptions added in v0.0.2

type VarOptions struct {
	Optional bool
}

Jump to

Keyboard shortcuts

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