envar

package
v0.0.122 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: MIT Imports: 6 Imported by: 0

README

ENVAR

компонент предназначен для чтения env (настроек окружения)

Пример использования

package main

import (
	"fmt"
	"log"

	"github.com/nobuenhombre/suikat/pkg/envar"
)

type AppConfig struct {
	Path           string  `env:"PATH:string=/some/default/path"`
	Port           int     `env:"PORT:int=8080"`
	Coefficient    float64 `env:"COEFFICIENT:float64=75.31"`
	MakeSomeAction bool    `env:"MSA:bool=false"`
}

func (cfg *AppConfig) Load() error {
	return envar.Load(cfg)
}

func main() {
	cfg := &AppConfig{}

	err := cfg.Load()
	if err != nil {
		log.Fatalf("ENV config error [%v]", err)
	}

	fmt.Printf("%#v", cfg)
}

Documentation

Overview

Package envar provides syntactic sugar for working with the environment variables. Allows you to describe using structure tags how to use environment variables to fill in this structure.

Index

Constants

View Source
const (
	TagEnvExample      = "NAME:valueType=defaultValue"
	CountPartsTagData  = 2
	CountPartsNameType = 2
)

Variables

This section is empty.

Functions

func Load added in v0.0.14

func Load(structData interface{}) error

Load field of target struct from flag like described in tags

func NewTagProcessor added in v0.0.14

func NewTagProcessor() refavour.TagProcessor

Types

type EnvVar

type EnvVar struct {
	// Key - name environment variable
	Key string

	// DefaultValue - default value if environment variable is not set
	DefaultValue interface{}
}

EnvVar describe tag for struct receiver

func (*EnvVar) GetBool

func (ev *EnvVar) GetBool() bool

GetBool read bool value from environment variable ev.Key

func (*EnvVar) GetFloat64 added in v0.0.14

func (ev *EnvVar) GetFloat64() float64

GetFloat64 read float64 value from environment variable ev.Key

func (*EnvVar) GetInt

func (ev *EnvVar) GetInt() int

GetInt read int value from environment variable ev.Key

func (*EnvVar) GetString

func (ev *EnvVar) GetString() string

GetString read string value from environment variable ev.Key

type FieldInfo added in v0.0.47

type FieldInfo struct {
	Type         reflect.Type
	Name         string
	ValueType    string
	DefaultValue string
}

type TagInfo added in v0.0.47

type TagInfo struct {
	Tag string
}

func (*TagInfo) GetFieldInfo added in v0.0.47

func (tag *TagInfo) GetFieldInfo(typeField reflect.StructField, valueField reflect.Value) (interface{}, error)

Jump to

Keyboard shortcuts

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