Files
fn_registry/functions/ml/genconfig_json_marshal.md
T
egutierrez cfdf515228 chore: auto-commit (799 archivos)
- .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>
2026-05-14 00:28:20 +02:00

85 lines
3.1 KiB
Markdown

---
name: genconfig_json_marshal
kind: function
lang: go
domain: ml
version: "1.0.0"
purity: impure
signature: "func GenconfigMarshal(cfg GenerationConfig) ([]byte, error)\nfunc GenconfigUnmarshal(data []byte) (GenerationConfig, error)"
description: "Wrappers json.Marshal/Unmarshal para GenerationConfig con formato canonico (MarshalIndent 2 espacios). Garantiza roundtrip identico al Python: json.dumps(indent=2, sort_keys=False). Campos JSON en snake_case."
tags: [ml, json, marshal, unmarshal, serialization, generation, canonical, pendiente-usar]
uses_functions: []
uses_types: [generation_config_go_ml]
returns: []
returns_optional: false
error_type: "error_go_core"
imports: ["encoding/json"]
params:
- name: cfg
desc: "GenerationConfig a serializar. Campos omitempty (negative_prompt, loras, clip_skip) se omiten si son zero/nil/empty."
- name: data
desc: "JSON bytes a deserializar. Acepta formato compacto o con indent. Keys deben ser snake_case (negative_prompt, cfg_scale, model_type, etc.)."
output: "GenconfigMarshal: bytes JSON con indent 2 espacios, orden de campos segun declaracion del struct (prompt, negative_prompt, seed, steps, cfg_scale, sampler, width, height, model, loras, clip_skip). GenconfigUnmarshal: GenerationConfig poblado o error de parsing."
tested: true
tests:
- "roundtrip marshal unmarshal produce config igual"
- "json cross-language snake_case keys se deserializan correctamente"
test_file_path: "functions/ml/genconfig_test.go"
file_path: "functions/ml/genconfig_json_marshal.go"
---
## Ejemplo
```go
cfg := ml.GenerationConfig{
Prompt: "a mountain at sunset",
Seed: 1234,
Steps: 30,
CfgScale: 7.0,
Sampler: "euler",
Width: 768,
Height: 512,
Model: ml.ModelRef{Name: "sdxl-base", ModelType: "sdxl", Quantization: "fp16"},
}
b, err := ml.GenconfigMarshal(cfg)
// b == {
// "prompt": "a mountain at sunset",
// "seed": 1234,
// ...
// }
cfg2, err := ml.GenconfigUnmarshal(b)
// cfg2 == cfg (DeepEqual)
```
## Notas
### Formato canonico y compatibilidad con Python
`GenconfigMarshal` usa `json.MarshalIndent(cfg, "", " ")`. El formato resultante es identico al que produce Python con `model.model_dump_json()` o `json.dumps(data, indent=2)` cuando `sort_keys=False`:
- Keys en orden de declaracion del struct (no alfabetico).
- Indent de 2 espacios, sin trailing whitespace.
- Campos omitempty ausentes si zero: `negative_prompt` ausente si `""`, `loras` ausente si `[]`, `clip_skip` ausente si `nil`.
### Keys JSON (snake_case obligatorio)
| Campo Go | Key JSON |
|---|---|
| `Prompt` | `"prompt"` |
| `NegativePrompt` | `"negative_prompt"` |
| `Seed` | `"seed"` |
| `Steps` | `"steps"` |
| `CfgScale` | `"cfg_scale"` |
| `Sampler` | `"sampler"` |
| `Width` | `"width"` |
| `Height` | `"height"` |
| `Model.ModelType` | `"model_type"` |
| `Model.Quantization` | `"quantization"` |
| `ClipSkip` | `"clip_skip"` |
### Por que impure
Los errores de `json.Unmarshal` son errores de parsing del input externo, no de I/O, pero se modelan como `(T, error)` para forzar manejo explicito en el caller. Marcado `impure` con `error_type: error_go_core` por convencion del registry.