Files
fn_registry/registry/indexer.go
T
egutierrez bf4b71653f feat: indexer que regenera registry.db desde los .md
Index() recorre functions/ y types/, parsea cada .md con frontmatter,
purga la BD existente e inserta todas las entradas. Devuelve conteos
y errores sin abortar ante fallos individuales.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 02:07:08 +01:00

88 lines
2.0 KiB
Go

package registry
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// IndexResult holds stats from an indexing run.
type IndexResult struct {
Functions int
Types int
Errors []string
}
// Index walks the registry root, parses all .md files, and populates the database.
// It purges existing data first to ensure a clean rebuild.
func Index(db *DB, root string) (*IndexResult, error) {
if err := db.Purge(); err != nil {
return nil, fmt.Errorf("purging database: %w", err)
}
result := &IndexResult{}
// Index functions
functionsDir := filepath.Join(root, "functions")
if _, err := os.Stat(functionsDir); err == nil {
err := filepath.Walk(functionsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() || !strings.HasSuffix(path, ".md") {
return nil
}
f, err := ParseFunctionMD(path)
if err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", path, err))
return nil
}
if err := db.InsertFunction(f); err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("insert %s: %v", f.ID, err))
return nil
}
result.Functions++
return nil
})
if err != nil {
return nil, fmt.Errorf("walking functions: %w", err)
}
}
// Index types
typesDir := filepath.Join(root, "types")
if _, err := os.Stat(typesDir); err == nil {
err := filepath.Walk(typesDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() || !strings.HasSuffix(path, ".md") {
return nil
}
t, err := ParseTypeMD(path)
if err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", path, err))
return nil
}
if err := db.InsertType(t); err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("insert %s: %v", t.ID, err))
return nil
}
result.Types++
return nil
})
if err != nil {
return nil, fmt.Errorf("walking types: %w", err)
}
}
return result, nil
}