Giter Club home page Giter Club logo

env's Introduction

env

Wrapper for godotenv with extra functionality to bind to values.

Binding functions are heavily inspired by the gin binding package.

Supported types:

  • int/uint (all types)
  • bool (strconv.Parsebool + yes/no)
  • float32/64
  • string
  • pointers
  • arrays
  • slices
  • structs
  • time.Time and time.Duration

set parameters

there are options modifying the behaviour of the set function.

Prefix is used to specify prefix to be prepended to all values. default is "".

Sep is used when combining keys. e.g. prefix is "API" and key is "PORT" and separator is "" the resulting key is "API_PORT". default is "".

Tag is used to specify the tag to use when getting key. default is "env".

ArrSep is used to specify the separator of array and slice values from env. e.g. sep="," - "1,2,3" => [1, 2, 3]. default is ",".

AutoFormatMissingKeys is used to signal is you want missing keys to be automatically formated. See formating for more info.

struct tag

the main struct tag has options to modify the behaviour when setting the value. tag parts are separated by a comma. key-value parts have the format key=value, boolean parts only have a key.

here are the available parts

Name

it is the first value and does not have a key. this, together with the prefix, forms the key to get env variable.

Default

the value to set is there is no env variable. this value requires the key default with a value.

Required

if there is no env variable with key and this is present, the set function will return an error saying there is a missing key. this is a boolean part where the key can be one of: required, require, or req.

DefaultValueSeparator

in the case where the default value is used the value of the default will be split by a value. by default this value will be env.DefaultValueSeparator (default: " ") but this can be specified on a per field basis with the DefaultValueSeparator part. this is a key-value part with the key being either separator or sep.

SkipOnNoValue

This is primarily used for pointer types. When the type is a pointer, the set function will try to set the value if it is null and then try to set the underlying value. this is good for structs or other types where you want to set the underlying value, but not for primitive types such as *int where you want to have nil when there is no value and not 0. Use keys skip_on_no_value or snv to skip setting values when no env variable was found for the key.

structs

use env.Bind to set structs. when settings structs the substructs will get the parents key appended as a prefix. a substruct named DB will give all values in the struct a DB prefix. this will add up if there will be many levels of prefixes.

See _example/main.go for use.

time.Time

time.Time is a struct but is treated differently. For time.Time fields there are a few more tags that can be set. this is taken from the gin binding function.

time_format can be used to specify the time format. RFC3339 will be used if nothing is specified.

time_utc can be used to specify time to be utc if not time_location can be used to specify location acording to time.LoadLocation

single values

the Get function is used to first generate the values to emulate a struct field by generating tags before binding. first parameter is the normal format for the env struct tag. other tags can be specified in format key=value

Unmarshal interface

before setting value the set function will take the address of the value and check if that implements either the StringUnmarshaler or TextUnmarshaler interfaces and will use that if it exists.

Auto Key formating

examples of formating:

  • "DBName" -> "DB_NAME"
  • "DB" -> "DB"
  • "Name" -> "NAME"
  • "AllowedOrigins" -> "ALLOWED_ORIGINS"
  • "TestA" -> "TEST_A"

env's People

Contributors

evenboee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.