47fac22230
- .claude/CLAUDE.md - .claude/commands/subagentes.md - .claude/rules/INDEX.md - .mcp.json - bash/functions/cybersecurity/analyze_dns.md - bash/functions/cybersecurity/audit_http_headers.md - bash/functions/cybersecurity/audit_ssh_config.md - bash/functions/cybersecurity/check_firewall.md - bash/functions/cybersecurity/detect_suspicious_users.md - bash/functions/cybersecurity/encrypt_file.md - ... Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
57 lines
2.1 KiB
Markdown
57 lines
2.1 KiB
Markdown
---
|
|
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, pendiente-usar]
|
|
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).
|