goenv

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2021 License: GPL-3.0 Imports: 7 Imported by: 1

README

Simple env to struct library in Go

Supported data types:

  1. bool
  2. int, int8, int16, int32, int64
  3. uint, uint8, uint16, uint32, uint64
  4. float32, float64
  5. string

Supported struct tags: env, default

env is a tag which describes environment variable name

default is a tag which describes default value for given environment variable

Installation

go get github.com/MrWebUzb/goenv

Run tests

make test

or

go test ./...

Usage example

package main

import (
	"fmt"
	"log"

	"github.com/MrWebUzb/goenv"
)

// Config structure
type Config struct {
	Debug      bool    `env:"DEBUG" default:"true"`
	DBName     string  `env:"DB_NAME" default:"postgres"`
	Port       int64   `env:"PORT" default:"8080"`
	FeePercent float32 `env:"FEE_PERCENT" default:"3.3"`
}

func main() {
	env, err := goenv.New()
	if err != nil {
		log.Fatalf("could not create env configuration: %v\n", err)
		return
	}

	cfg := &Config{}

	if err := env.Parse(cfg); err != nil {
		log.Fatalf("could not parse env to struct %v\n", err)
		return
	}

	fmt.Printf("Name: Debug, Type: %T, Value: %v\n", cfg.Debug, cfg.Debug)
	fmt.Printf("Name: DBName, Type: %T, Value: %v\n", cfg.DBName, cfg.DBName)
	fmt.Printf("Name: Port, Type: %T, Value: %v\n", cfg.Port, cfg.Port)
	fmt.Printf("Name: FeePercent, Type: %T, Value: %v\n", cfg.FeePercent, cfg.FeePercent)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoStruct error when given parametetr is not struct
	ErrNoStruct = errors.New("given parameter is not a struct")
	// ErrNoPtr error when given parameter is not pointer
	ErrNoPtr = errors.New("given parameter is not a pointer")
	// DefaultTag tag for loading env key names from struct
	DefaultTag = "env"

	// Default value of env variable
	DefaultValueTag = "default"
	// Required env variable
	DefaultRequiredTag = "required"

	// Built-in parser functions
	BuiltInParsers = map[reflect.Kind]ParserFunc{
		reflect.Bool: func(value string) (interface{}, error) {
			return strconv.ParseBool(value)
		},
		reflect.Int: func(value string) (interface{}, error) {
			return strconv.Atoi(value)
		},
		reflect.Int8: func(value string) (interface{}, error) {
			val, err := strconv.ParseInt(value, 10, 8)
			if err != nil {
				return nil, err
			}

			return int8(val), nil
		},
		reflect.Int16: func(value string) (interface{}, error) {
			val, err := strconv.ParseInt(value, 10, 16)
			if err != nil {
				return nil, err
			}

			return int16(val), nil
		},
		reflect.Int32: func(value string) (interface{}, error) {
			val, err := strconv.ParseInt(value, 10, 32)
			if err != nil {
				return nil, err
			}

			return int32(val), nil
		},
		reflect.Int64: func(value string) (interface{}, error) {
			return strconv.ParseInt(value, 10, 64)
		},
		reflect.Uint: func(value string) (interface{}, error) {
			val, err := strconv.ParseUint(value, 10, 0)
			if err != nil {
				return nil, err
			}

			return uint(val), nil
		},
		reflect.Uint8: func(value string) (interface{}, error) {
			val, err := strconv.ParseUint(value, 10, 8)
			if err != nil {
				return nil, err
			}

			return uint8(val), nil
		},
		reflect.Uint16: func(value string) (interface{}, error) {
			val, err := strconv.ParseUint(value, 10, 16)
			if err != nil {
				return nil, err
			}

			return uint16(val), nil
		},
		reflect.Uint32: func(value string) (interface{}, error) {
			val, err := strconv.ParseUint(value, 10, 32)
			if err != nil {
				return nil, err
			}

			return uint32(val), nil
		},
		reflect.Uint64: func(value string) (interface{}, error) {
			return strconv.ParseUint(value, 10, 64)
		},
		reflect.Float32: func(value string) (interface{}, error) {
			val, err := strconv.ParseFloat(value, 32)
			if err != nil {
				return nil, err
			}

			return float32(val), nil
		},
		reflect.Float64: func(value string) (interface{}, error) {
			return strconv.ParseFloat(value, 64)
		},
		reflect.String: func(value string) (interface{}, error) {
			return value, nil
		},
	}
)

Functions

This section is empty.

Types

type Config

type Config struct {
	EnvTag           string
	DefaultValueTag  string
	RequiredValueTag string
}

func New

func New(envFiles ...string) (Config, error)

func (Config) Parse

func (e Config) Parse(s interface{}, data map[string]string) error

type ParserFunc

type ParserFunc func(value string) (interface{}, error)

Jump to

Keyboard shortcuts

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