327937124f
- 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>
52 lines
1.7 KiB
Markdown
52 lines
1.7 KiB
Markdown
---
|
|
name: config_from_file
|
|
kind: function
|
|
lang: go
|
|
domain: infra
|
|
version: "1.0.0"
|
|
purity: impure
|
|
signature: "func ConfigFromFile(path string, target any) error"
|
|
description: "Carga configuracion desde un archivo en target. Soporta JSON (encoding/json stdlib). YAML es stub: retorna 'not implemented'. Extension determina el formato."
|
|
tags: [config, file, json, yaml, infra]
|
|
uses_functions: []
|
|
uses_types: []
|
|
returns: []
|
|
returns_optional: false
|
|
error_type: "error_go_core"
|
|
imports: [encoding/json, fmt, os, strings]
|
|
params:
|
|
- name: path
|
|
desc: "ruta al archivo de configuracion (.json, .yaml, .yml)"
|
|
- name: target
|
|
desc: "puntero a struct o mapa donde se va a deserializar la configuracion"
|
|
output: "nil si el archivo se cargo y parseo correctamente, error si el archivo no existe, tiene formato invalido, o la extension no esta soportada"
|
|
tested: true
|
|
tests:
|
|
- "carga JSON valido en struct"
|
|
- "JSON invalido retorna error"
|
|
- "archivo inexistente retorna error"
|
|
- "extension YAML retorna not implemented"
|
|
- "extension desconocida retorna error"
|
|
test_file_path: "functions/infra/config_from_file_test.go"
|
|
file_path: "functions/infra/config_from_file.go"
|
|
---
|
|
|
|
## Ejemplo
|
|
|
|
```go
|
|
type DBConfig struct {
|
|
Host string `json:"host"`
|
|
Port int `json:"port"`
|
|
Database string `json:"database"`
|
|
}
|
|
|
|
var cfg DBConfig
|
|
if err := ConfigFromFile("config/database.json", &cfg); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
```
|
|
|
|
## Notas
|
|
|
|
JSON: implementacion completa via encoding/json de stdlib. YAML: stub que retorna error "not implemented" — para YAML real agregar gopkg.in/yaml.v3. El formato se determina por la extension del archivo (case-insensitive). Se puede combinar con ConfigFromEnv: cargar defaults del archivo y luego sobreescribir con env vars.
|