feat(registry): random_hex_id_go_core + spa_handler_go_infra

Dos primitivas reutilizables para apps web del registry:
- random_hex_id_go_core: IDs hex aleatorios (apps con SQLite + IDs string)
- spa_handler_go_infra: http.Handler que sirve embed.FS con fallback
  a index.html (patron SPA para React Router/dnd-kit)

Ambas creadas via fn-constructor durante apps/kanban (issue 0053).
Tests pasan, fn index OK.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-06 15:54:01 +02:00
parent 2be91b0c4d
commit 7849e1cc8e
6 changed files with 310 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
package core
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
// RandomHexID generates a random hex string by reading numBytes bytes from
// crypto/rand. The returned string has length numBytes*2.
// Returns an error if numBytes <= 0 or if crypto/rand.Read fails.
func RandomHexID(numBytes int) (string, error) {
if numBytes <= 0 {
return "", fmt.Errorf("random_hex_id: numBytes must be > 0, got %d", numBytes)
}
b := make([]byte, numBytes)
if _, err := rand.Read(b); err != nil {
return "", fmt.Errorf("random_hex_id: failed to read from crypto/rand: %w", err)
}
return hex.EncodeToString(b), nil
}
+54
View File
@@ -0,0 +1,54 @@
---
name: random_hex_id
kind: function
lang: go
domain: core
version: "1.0.0"
purity: impure
signature: "func RandomHexID(numBytes int) (string, error)"
description: "Genera un string hex aleatorio leyendo numBytes bytes de crypto/rand. El resultado tiene longitud numBytes*2. Retorna error si numBytes<=0 o si falla crypto/rand."
tags: ["random", "id", "hex", "uuid-alternative"]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: ["crypto/rand", "encoding/hex", "fmt"]
params:
- name: numBytes
desc: "numero de bytes aleatorios a leer de crypto/rand; el string resultante mide numBytes*2 caracteres hex"
output: "string hex en minusculas con 2*numBytes caracteres, o error si numBytes<=0 o falla la lectura de crypto/rand"
tested: true
tests:
- "8 bytes retorna 16 caracteres hex validos"
- "dos llamadas devuelven valores distintos"
- "numBytes 0 retorna error"
- "numBytes negativo retorna error"
test_file_path: "functions/core/random_hex_id_test.go"
file_path: "functions/core/random_hex_id.go"
---
## Ejemplo
```go
id, err := RandomHexID(8)
if err != nil {
log.Fatal(err)
}
// id = "a3f1c9b204e87d6a" (16 chars hex, aleatorio)
// Para IDs de 32 chars (128 bits, equivalente a UUID sin guiones):
id32, _ := RandomHexID(16)
// id32 = "4e2a1b9c7d8f0e3a5c6b2d4f1a9e8c7b"
```
## Notas
Funcion impura — usa `crypto/rand` (respaldado por `/dev/urandom` o equivalente del SO). No es determinista.
Usos tipicos:
- `RandomHexID(8)` → 16 chars, 64 bits de entropia. Suficiente para IDs de rows en SQLite con tablas pequeñas (<10M filas).
- `RandomHexID(16)` → 32 chars, 128 bits. Equivalente a UUID v4 sin guiones.
- `RandomHexID(32)` → 64 chars, 256 bits. Equivalente a SHA-256 de entropia.
Comparacion con `session_create_go_infra`: esa funcion genera tokens de 32 bytes internamente pero no expone la primitiva. Esta funcion es la primitiva componible que cualquier app puede usar directamente.
+49
View File
@@ -0,0 +1,49 @@
package core
import (
"encoding/hex"
"testing"
)
func TestRandomHexID(t *testing.T) {
t.Run("8 bytes retorna 16 caracteres hex validos", func(t *testing.T) {
got, err := RandomHexID(8)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(got) != 16 {
t.Errorf("expected length 16, got %d", len(got))
}
if _, err := hex.DecodeString(got); err != nil {
t.Errorf("result is not valid hex: %v", err)
}
})
t.Run("dos llamadas devuelven valores distintos", func(t *testing.T) {
a, err := RandomHexID(8)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
b, err := RandomHexID(8)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if a == b {
t.Errorf("expected two different IDs, both are %q", a)
}
})
t.Run("numBytes 0 retorna error", func(t *testing.T) {
_, err := RandomHexID(0)
if err == nil {
t.Error("expected error for numBytes=0, got nil")
}
})
t.Run("numBytes negativo retorna error", func(t *testing.T) {
_, err := RandomHexID(-1)
if err == nil {
t.Error("expected error for numBytes=-1, got nil")
}
})
}