Giter VIP home page Giter VIP logo

settings's Introduction

Tests GitHub release MIT license codecov Go Linter help wanted

settings

The package looks up necessary environment variables and uses them to specify settings for your application. In addition, the package validates the final struct using standard validate tags.

Contents

  1. Installation
  2. Description
  3. Limitations

1. Installation

Use go get to install the package:

go get github.com/kaatinga/settings

Then, import the package into your own code:

import "github.com/kaatinga/settings"

2. Description

How to use

Create a settings model where you can use tags env, default and validate. Announce a variable and call Load():

type Settings struct {
    Port       string `env:"PORT" validate:"numeric"`
    Database   string `env:"DATABASE"`
    CacheSize  byte `env:"CACHE_SIZE" default:"50"`
    LaunchMode string `env:"LAUNCH_MODE"`
}

var settings Settings
err := Load(&settings)
if err != nil {
    return err
}

The env tag must contain the name of the related environment variable. The default tag contains a default value that is used in case the environment variable was not found. The validate tag may contain an optional validation rule fallowing the documentation of the validator package.

Supported types

Type Real type
string -
boolean -
~int -
~uint -
time.Duration int64

Nested structs

Nested structs can be added via pointer or without pointer. Example:

type Model2 struct {
    CacheSize   byte `env:"CACHE_SIZE"`
}

type Model3 struct {
    Port        string `env:"PORT validate:"numeric"`
}

type Model1 struct {
    Database    string `env:"DATABASE"`
    Model2      *Model2
    Model3      Model3
}

The nested structs that added via pointer must not be necessarily initialized:

var settings Model1
if err := Load(&settings); err != nil {
    return err
}

Nonetheless, if you want, you can do it.

var settings = Model1{Model2: new(Model2)}
if err := Load(&settings); err != nil {
    return err
}

3. Limitations

The configuration model has some limitations in how it is arranged and used.

Empty structs are not allowed

If you add an empty struct to your configuration model, Load() returns error.

Load() accepts only pointer to your configuration model

The root model must be initialized and added to the Load() signature via pointer:

err := Load(&EnvironmentSettings)
if err != nil {
    return err
}

Otherwise, the function returns error.

settings's People

Contributors

art-frela avatar kaatinga avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

settings's Issues

enhancement: add default values in tags

type settings struct {
	Port       string `env:"PORT" validate:"numeric" default_env:"8080"`
	Database   string `env:"DATABASE" default_env:"postgres://postgres:test@localhost:5432/postgres?sslmode=disable "`
	CacheSize  byte   `env:"CACHE_SIZE"`
	LaunchMode string `env:"LAUNCH_MODE" default_env:"stage"`
}

If my config will be consist of 100500 fields, then I must set all of those env variables? No, thanks! Better I will be use another env package...

P.S. name of the package not go convention I think (env_loader.... ;-) )

receive a review

Hello visitor, if you are reading this text and keen at Go language, please, make a review of the package.

Cordially yours,
Michael Gunkoff

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.