Skip to content

Commit

Permalink
Update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
orbisgloria committed Dec 7, 2024
1 parent 2d3f6a1 commit 20d77b3
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 19 deletions.
67 changes: 55 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,59 @@

# env

The env package provides a variety of methods for managing environment variables. It supports loading data from `.env` files into the environment and provides data transfer between the environment and custom Go data structures, allowing you to effortlessly update structure fields from environment variables or vice versa, set environment variables from Go structure fields. The env package supports all standard Go data types (strings, numbers, boolean expressions, slices, arrays, etc.), as well as the complex `url.URL` type.
A powerful and flexible environment variable management package for Go with support for `.env` files, struct mapping, and advanced type conversion. It supports loading data from `.env` files into the environment and provides data transfer between the environment and custom Go data structures, allowing you to effortlessly update structure fields from environment variables or vice versa, set environment variables from Go structure fields. The env package supports all standard Go data types (strings, numbers, boolean expressions, slices, arrays, etc.), as well as the complex `url.URL` type.

## Features

The main features of the env module include:
- **Concurrent Processing**: Parse `.env` files using configurable parallel processing.
- **Bi-directional Mapping**: Convert between environment variables and Go structures.
- **Rich Type Support**: Handle all Go basic types, arrays, slices, and custom types.
- **Advanced Parsing**: Support for variable expansion, quotes, comments, and multi-line values.
- **Prefix Filtering**: Filter environment variables by prefix.
- **Custom Interfaces**: Implement custom marshaling/unmarshaling behavior.
- **Production Ready**: Thread-safe operations and comprehensive error handling.

- setting environment variables from variables defined in an env-file;
- converting (marshaling) a Go structure to environment variables;
- extracting (unmarshaling) environment variables to a Go structure;
- setting any variables to the environment;
- deleting variables from the environment;
- checking for the presence of a variable in the environment;
- retrieving the value of a variable by key from the environment;
- clearing the environment.
The module provides synonyms for the standard methods from the os module, such as `Get` for `os.Getenv`, `Set` for `os.Setenv`, `Unset` for `os.Unsetenv`, `Clear` for `os.Clearenv`, and Environ for `os.Environ`, to manage the environment. Additionally, it implements custom methods that enable saving variables from the environment into structures.

In addition, additional methods for working with `.env` files and data exchange between environment variables and the Go structs are implemented.
## Installation

The module provides synonyms for the standard methods from the os module, such as `Get` for `os.Getenv`, `Set` for `os.Setenv`, `Unset` for `os.Unsetenv`, `Clear` for `os.Clearenv`, and Environ for `os.Environ`, to manage the environment. Additionally, it implements custom methods that enable saving variables from the environment into structures.
```bash
go get -u github.com/goloop/env
```

## Quick Start

```go
package main

import (
"fmt"
"github.com/goloop/env"
"log"
)

type Config struct {
Host string `env:"HOST" def:"localhost"`
Port int `env:"PORT" def:"8080"`
IPs []string `env:"ALLOWED_IPS" sep:","`
IsDebug bool `env:"DEBUG" def:"true"`
}

func main() {
// Load .env file.
if err := env.Load(".env"); err != nil {
log.Fatal(err)
}

// Parse environment into struct.
var cfg Config
if err := env.Unmarshal("", &cfg); err != nil {
log.Fatal(err)
}

fmt.Printf("%+v\n", cfg)
}
```

## Env-file supports

Expand Down Expand Up @@ -583,3 +618,11 @@ func main() {
// Allowed hosts: [localhost 127.0.0.1]
}
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
68 changes: 61 additions & 7 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
// Package env provides a variety of methods for managing and streamlining
// environment variables. It supports loading data from .env files into the
// environment and offers seamless data transfer between the environment and
// custom data structures, allowing for effortless updates to structure fields
// from environment variables or vice versa, set environment variables from
// structure fields. The env package supports all standard data types, as well
// as the url.URL type.
// Package env provides a comprehensive solution for managing environment
// variables in Go applications. It offers a rich set of features for handling
// environment configuration through both .env files and runtime environment
// variables.
//
// Core Features:
// - Concurrent parsing of .env files with configurable parallelism
// - Bidirectional mapping between environment variables and Go structures
// - Support for nested structures and complex data types
// - Advanced type conversion with validation
// - URL parsing and validation support
// - Flexible prefix-based filtering
// - Custom marshaling and unmarshaling interfaces
//
// The package supports loading configuration from .env files with features like:
// - Variable expansion (${VAR} or $VAR syntax)
// - Quoted values with escape sequences
// - Comments and inline comments
// - Export statements
// - Multi-line values
// - Default values
// - Custom separators for arrays/slices
//
// Type Support:
// The package handles all common Go types including:
// - Basic types: string, bool, int/uint (all sizes), float32/64
// - Complex types: url.URL, custom structs
// - Collections: arrays, slices
// - Nested structures with automatic prefix handling
// - Pointers to supported types
//
// Structure Tags:
// - env: specifies the environment variable name
// - def: provides default values
// - sep: defines separator for array/slice values
//
// Example usage:
//
// type Config struct {
// Host string `env:"HOST" def:"localhost"`
// Port int `env:"PORT" def:"8080"`
// IPs []string `env:"ALLOWED_IPS" sep:","`
// APIUrl url.URL `env:"API_URL"`
// }
//
// func main() {
// var cfg Config
// // Load .env file and parse it
// if err := env.Load(".env"); err != nil {
// log.Fatal(err)
// }
// // Map environment variables to structure
// if err := env.Unmarshal("", &cfg); err != nil {
// log.Fatal(err)
// }
// }
//
// The package is designed to be efficient and safe, with careful handling of
// concurrent operations and proper error management. It provides a clean API
// that follows Go idioms while offering powerful features for complex
// configuration scenarios.
package env

0 comments on commit 20d77b3

Please sign in to comment.