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:
parent
3064ef1b40
commit
057677ab7f
39
block.go
39
block.go
@ -2,38 +2,41 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
"html"
|
||||||
"strings"
|
"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.
|
// SmartBlockFunc renders a Smart Block by substituting {{field}} placeholders with content values.
|
||||||
func SmartBlockFunc(ctx context.Context, content map[string]any) string {
|
func SmartBlockFunc(ctx context.Context, content map[string]any) string {
|
||||||
// Get metadata
|
rawMeta, err := json.Marshal(content["_meta"])
|
||||||
meta, ok := content["_meta"].(map[string]any)
|
if err != nil {
|
||||||
if !ok {
|
return renderEmptyState()
|
||||||
|
}
|
||||||
|
var meta smartBlockMeta
|
||||||
|
if err := json.Unmarshal(rawMeta, &meta); err != nil {
|
||||||
return renderEmptyState()
|
return renderEmptyState()
|
||||||
}
|
}
|
||||||
|
|
||||||
generations, ok := meta["generations"].([]any)
|
if len(meta.Generations) == 0 || meta.CurrentGenerationID == "" {
|
||||||
if !ok || len(generations) == 0 {
|
|
||||||
return renderEmptyState()
|
return renderEmptyState()
|
||||||
}
|
}
|
||||||
|
|
||||||
currentID, _ := meta["currentGenerationId"].(string)
|
|
||||||
if currentID == "" {
|
|
||||||
return renderEmptyState()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the current generation
|
|
||||||
var template string
|
var template string
|
||||||
for _, gen := range generations {
|
for _, gen := range meta.Generations {
|
||||||
genMap, ok := gen.(map[string]any)
|
if gen.ID == meta.CurrentGenerationID {
|
||||||
if !ok {
|
template = gen.Template
|
||||||
continue
|
|
||||||
}
|
|
||||||
if genMap["id"] == currentID {
|
|
||||||
template, _ = genMap["template"].(string)
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user