feat(infra): funciones impuras Go para carga de env/config (dotenv, env_require, config_from_env, config_from_file)
- dotenv_load: parser .env con no-sobreescritura y soporte de comillas - env_require: os.Getenv con error descriptivo fail-fast - env_require_all: verifica multiples vars y lista todas las faltantes - config_from_env: reflection sobre struct tags env/default/required/secret, 5 tipos soportados - config_from_file: JSON via stdlib, YAML stub con not-implemented - 25 tests unitarios, todos PASS Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
---
|
||||
name: config_from_env
|
||||
kind: function
|
||||
lang: go
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "func ConfigFromEnv(target any) error"
|
||||
description: "Puebla una struct de configuracion desde variables de entorno usando reflection y struct tags (env, default, required, secret). Soporta string, int, int64, bool, float64 y []string (comma-separated). Acumula todos los errores antes de retornar."
|
||||
tags: [config, env, reflect, infra, tags]
|
||||
uses_functions: []
|
||||
uses_types: []
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: [fmt, os, reflect, strconv, strings]
|
||||
params:
|
||||
- name: target
|
||||
desc: "puntero no-nil a struct con tags env, default, required y/o secret en sus campos exportados"
|
||||
output: "nil si la struct se poblo correctamente, error si falta una variable required o hay un tipo incompatible"
|
||||
tested: true
|
||||
tests:
|
||||
- "populate string field desde env"
|
||||
- "populate int field con conversion"
|
||||
- "populate bool field con conversion"
|
||||
- "populate float64 field"
|
||||
- "populate slice string comma-separated"
|
||||
- "valor default usado cuando env no seteada"
|
||||
- "required sin valor retorna error"
|
||||
- "int invalido retorna error descriptivo"
|
||||
- "puntero nil retorna error"
|
||||
test_file_path: "functions/infra/config_from_env_test.go"
|
||||
file_path: "functions/infra/config_from_env.go"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```go
|
||||
type AppConfig struct {
|
||||
Host string `env:"HOST" default:"localhost"`
|
||||
Port int `env:"PORT" default:"8080" required:"true"`
|
||||
APIKey string `env:"API_KEY" required:"true" secret:"true"`
|
||||
Tags []string `env:"TAGS" default:"prod,stable"`
|
||||
Debug bool `env:"DEBUG" default:"false"`
|
||||
Timeout float64 `env:"TIMEOUT" default:"30.0"`
|
||||
}
|
||||
|
||||
var cfg AppConfig
|
||||
if err := ConfigFromEnv(&cfg); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Sin dependencias externas — solo stdlib. Tipos soportados: string, int/int64, uint/uint64, bool, float32/float64, []string. Los []string se parsean splitteando por coma y trimeando espacios. El tag secret:"true" no tiene efecto en runtime (solo documentacion — ver ConfigDump para ocultar al loggear).
|
||||
Reference in New Issue
Block a user