refactor: replace any with concrete types where shapes are known (check-safety 2e)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Alex Dunmow 2026-07-10 02:44:26 +08:00
parent 3064ef1b40
commit 057677ab7f

View File

@ -2,38 +2,41 @@ package main
import (
"context"
"encoding/json"
"fmt"
"html"
"strings"
)
type smartBlockGeneration struct {
ID string `json:"id"`
Template string `json:"template"`
}
type smartBlockMeta struct {
Generations []smartBlockGeneration `json:"generations"`
CurrentGenerationID string `json:"currentGenerationId"`
}
// SmartBlockFunc renders a Smart Block by substituting {{field}} placeholders with content values.
func SmartBlockFunc(ctx context.Context, content map[string]any) string {
// Get metadata
meta, ok := content["_meta"].(map[string]any)
if !ok {
rawMeta, err := json.Marshal(content["_meta"])
if err != nil {
return renderEmptyState()
}
var meta smartBlockMeta
if err := json.Unmarshal(rawMeta, &meta); err != nil {
return renderEmptyState()
}
generations, ok := meta["generations"].([]any)
if !ok || len(generations) == 0 {
if len(meta.Generations) == 0 || meta.CurrentGenerationID == "" {
return renderEmptyState()
}
currentID, _ := meta["currentGenerationId"].(string)
if currentID == "" {
return renderEmptyState()
}
// Find the current generation
var template string
for _, gen := range generations {
genMap, ok := gen.(map[string]any)
if !ok {
continue
}
if genMap["id"] == currentID {
template, _ = genMap["template"].(string)
for _, gen := range meta.Generations {
if gen.ID == meta.CurrentGenerationID {
template = gen.Template
break
}
}