diff --git a/README.es-ES.md b/README.es-ES.md new file mode 100644 index 0000000..5d7f72f --- /dev/null +++ b/README.es-ES.md @@ -0,0 +1,275 @@ + + +# env: Analizar variables de entorno, mapear a estructuras + +![Go](https://github.com/aacanakin/env/workflows/Go/badge.svg) [![Go Report Card](https://goreportcard.com/badge/github.com/aacanakin/env)](https://goreportcard.com/report/github.com/aacanakin/env) [![Coverage Status](https://coveralls.io/repos/github/aacanakin/env/badge.svg?branch=master)](https://coveralls.io/github/aacanakin/env?branch=master) + +env es un mapeador de variables de entorno a estructuras + +## Características + +- Capacidad para mapear variables de entorno a estructuras +- Capacidad para personalizar variables de entorno con etiquetas de estructuras +- Capacidad para analizar estructuras anidadas/subestructuras +- Capacidad para analizar estructuras anidadas/subestructuras incrustadas +- Capacidad para analizar y mapear tipos (`bool`, `string`, `int`, `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `float32`, `float64`, `complex64`, `complex128`, `struct`) + +## Primeros pasos + +### Instalación + +```sh +// Requires go1.15+ + +go get github.com/aacanakin/env +``` + +### Uso + +```go +// main.go +package main + +import "fmt" +import "github.com/aacanakin/env" + +type Config struct { + Host string + Port int + Debug bool +} + +func main() { + + var c Config + + err := env.Parse(&c) + if err != nil { + panic(err) + } + + fmt.Println("Host: ", c.Host) + fmt.Println("Port: ", c.Port) + fmt.Println("Debug: ", c.Debug) +} +``` + +Ejecutar + +```sh +$ HOST=localhost PORT=8081 debug=true go run main.go + +// Output +Host: localhost +Port: 8081 +Debug: true +``` + +### Mapeo personalizado de variables de entorno + +```go +// main.go +package main + +import "fmt" +import "github.com/aacanakin/env" + +func main() { + type Config struct { + Host string `env:"SERVICE_HOST"` + Port int `env:"SERVICE_PORT"` + Debug bool `env:"SERVICE_DEBUG"` + } + + var c Config + + err := env.Parse(&c) + if err != nil { + panic(err) + } + + fmt.Println("Host: ", c.Host) + fmt.Println("Port: ", c.Port) + fmt.Println("Debug: ", c.Debug) +} +``` + +Ejecutar + +```sh +SERVICE_HOST=localhost SERVICE_PORT=8081 SERVICE_DEBUG=true go run main.go + +// Output +Host: localhost +Port: 8081 +Debug: true +``` + +### Estructuras anidadas/Subestructuras + +```go +// main.go +package main + +import "fmt" +import "github.com/aacanakin/env" + +type db struct { + Host string `env:"DB_HOST"` + Port int `env:"DB_PORT"` +} + +type service struct { + Debug bool `env:"SERVICE_DEBUG"` +} + +type Config struct { + DB db + Service service +} + +func main() { + var c Config + + err := env.Parse(&c) + if err != nil { + panic(err) + } + + fmt.Println("DB Host: ", c.DB.Host) + fmt.Println("DB Port: ", c.DB.Port) + fmt.Println("Service Debug: ", c.Service.Debug) +} + +``` + +Ejecutar + +```sh +DB_HOST=localhost DB_PORT=3306 SERVICE_DEBUG=true go run main.go + +// Output +DB Host: localhost +DB Port: 3306 +Service Debug: true +``` + +### Uso con .env + +Instalación + +```sh +go get github.com/joho/godotenv +``` + +Crear un archivo .env + +```sh +# .env +HOST=localhost +PORT=8081 +``` + +```go +// main.go +package main + +import ( + "github.com/joho/godotenv" + "github.com/aacanakin/env" + "log" +) + +type Config struct { + Host string + Port uint16 +} + +func main() { + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } + + var c Config + + err = env.Parse(&c) + + log.Println("Host: ", c.Host) + log.Println("Port: ", c.Port) +} +``` + +### Configuración de solo lectura + +Dado que es una buena práctica mantener las variables de entorno inmutables, también será una buena práctica mantener la estructura analizada inmutable. Sin embargo, el paquete `reflect` de Go no permite establecer campos no exportados. Por lo tanto, para mantener la estructura inmutable; + +- Crear un paquete de configuración + +```go +package config + +import "github.com/aacanakin/env" + +type conf struct { + Host string + Port uint16 +} + +type Config struct { + conf conf +} + +func (c Config) Host() string { + return c.conf.Host +} + +func (c Config) Port() uint16 { + return c.conf.Port +} + +func New() (*Config, error) { + var c conf + err := env.Parse(&c) + if err != nil { + return nil, err + } + + return &Config{c}, nil +} +``` + +- Úselo en su main.go + +```go +package main + +import ( + "fmt" + + "github.com/aacanakin/env_test/config" +) + +func main() { + c, err := config.New() + if err != nil { + panic(err) + } + + // HERE, c.conf is not accessible + fmt.Println("Host:", c.Host()) + fmt.Println("Port:", c.Port()) +} + +``` + +NOTA: Esto parece que no es muy idiomático en Go. Los comentarios y sugerencias son bienvenidos. + +## Hoja de ruta + +- [ ] Restricción de etiqueta `omitempty` +- [ ] Restricción de etiqueta `file` +- [ ] Capacidad para personalizar la clave de la etiqueta (la predeterminada es env) +- [ ] Proporcionar un analizador basado en instancias +- [ ] Proporcionar un ejemplo de configuración de solo lectura con un paquete conf personalizado +- [ ] Lanzar v0.1