envsnatch

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2024 License: MIT Imports: 6 Imported by: 0

README

EnvSnatch

EnvSnatch

EnvSnatch is a Go package designed for efficient environment variable loading into Go structs. It supports a range of data types and handles both required and optional variables with ease.

Features

  • Loads environment variables directly into Go structs.
  • Supports a variety of data types including strings, integers, unsigned integers, floats, and booleans.
  • Treats all struct fields as required by default, unless explicitly marked as optional.
  • Optional fields can be marked using the optional:"true" tag.

Upcoming Features

We are constantly working to improve EnvSnatch and plan to add more features in the future. Any help or contributions to these features are highly welcome:

  • Additional Validations: Alongside the current 'required' and 'optional' validations, we aim to introduce more validation types like 'size', 'min', 'max', etc., to provide more control over the data loaded from environment variables.
  • Support for Other File Types: Currently, EnvSnatch supports .env files. We are looking to extend support to other file formats such as .json, .yml, etc., in future releases. This will allow more flexibility in how configuration data is structured and stored.

Supported Data Types

EnvSnatch supports the following data types:

  • string
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • bool

Optional Fields

All struct fields are considered required by default. Optional fields can be marked with optional:"true" to indicate that no error should be reported if the environment variable is not set.

Example:

type Config struct {
// ...
OptionalField string `env:"OPTIONAL_FIELD" optional:"true"`
}

Installation

go get github.com/mayckol/envsnatch

Usage

Here's how to use EnvSnatch:

package main

import (
	"fmt"
	"log"
	"github.com/mayckol/envsnatch"
)

type Config struct {
	Port int `env:"PORT"`
	// ... other fields
	OptionalField string `env:"OPTIONAL_FIELD" optional:"true"`
}

func main() {
	var cfg Config
	envReaderWithoutFile, err := envsnatch.NewEnvSnatch() // take the environment variables from the system
	if err != nil {
		log.Fatal(err)
	}
	// =================================================================================================================
	
	// WITHOUT .env file
	failedFields, err := envReaderWithoutFile.Unmarshal(&cfg)
	// type UnmarshalingErr struct {
	//	Field  string
	//	Reason string
	//}

	// treat failedFields as a slice of UnmarshalingErr
	fmt.Println("Failed fields:", failedFields) // Failed fields is a slice of UnmarshalingErr
	//if err != nil {
	//	log.Fatal(err) // handle unexpected error
	//}

	fmt.Printf("Loaded config: %+v\n", cfg)

	// =================================================================================================================
	
	// WITH .env file
	envReaderWithFile, err := envsnatch.NewEnvSnatch()
	if err != nil {
		log.Fatal(err)
	}
	envReaderWithFile.AddPath(".") // path to the .env file
	envReaderWithFile.AddFileName(".env") // name of the .env file
	// PORT=3000
	// OPTIONAL_FIELD=
	failedFields, err = envReaderWithFile.Unmarshal(&cfg)
	fmt.Println("Failed fields needs to be empty:", failedFields) // Failed fields needs to be empty
}

Contributing

Your contributions make the open-source community a great place to learn, inspire, and create. Any contributions you make to future features and enhancements are greatly appreciated.

  • Fork the Project
  • Create your Feature Branch (git checkout -b feature/AmazingFeature)
  • Commit your Changes (git commit -m 'Add some AmazingFeature')
  • Push to the Branch (git push origin feature/AmazingFeature)
  • Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnvSnatch

type EnvSnatch struct {
	UnmarshalingErrs []UnmarshalingErr
	// contains filtered or unexported fields
}

func NewEnvSnatch

func NewEnvSnatch() (*EnvSnatch, error)

func (*EnvSnatch) AddFileName

func (e *EnvSnatch) AddFileName(fileName string)

func (*EnvSnatch) AddPath

func (e *EnvSnatch) AddPath(path string)

func (*EnvSnatch) Unmarshal

func (e *EnvSnatch) Unmarshal(config interface{}) (*[]UnmarshalingErr, error)

type UnmarshalingErr

type UnmarshalingErr struct {
	Field  string
	Reason string
}

Jump to

Keyboard shortcuts

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