themes-editorial/helpers.go
Alex Dunmow 1d9a4c8ce6 initial: theme plugin editorial
Bootstrapped during the 2026-06-06 BlockNinja consolidation. Was previously
an unversioned directory inside ~/src/blockninja-themes/editorial.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 14:11:28 +08:00

52 lines
1.3 KiB
Go

package main
// getString extracts a string value from a content map.
func getString(content map[string]any, key string) string {
if v, ok := content[key].(string); ok {
return v
}
return ""
}
// getStringWithDefault returns the string at key or the default when absent/empty.
func getStringWithDefault(content map[string]any, key, def string) string {
if v := getString(content, key); v != "" {
return v
}
return def
}
// getBool extracts a bool value from a content map. Accepts true/false and
// string forms "true"/"false"/"yes"/"no"/"1"/"0" (admin select dropdowns
// commonly store booleans as strings).
func getBool(content map[string]any, key string, def bool) bool {
if v, ok := content[key].(bool); ok {
return v
}
if v, ok := content[key].(string); ok {
switch v {
case "true", "yes", "1":
return true
case "false", "no", "0":
return false
}
}
return def
}
// getSlice extracts a slice of map values from content[key]. JSON decoding
// gives []any; we narrow to []map[string]any so block code can index into
// individual items by name.
func getSlice(content map[string]any, key string) []map[string]any {
if v, ok := content[key].([]any); ok {
result := make([]map[string]any, 0, len(v))
for _, item := range v {
if m, ok := item.(map[string]any); ok {
result = append(result, m)
}
}
return result
}
return nil
}