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

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

76 lines
1.8 KiB
Go

package main
import "strconv"
// getString extracts a string value from content map. Returns "" if missing or wrong type.
func getString(content map[string]any, key string) string {
if v, ok := content[key].(string); ok {
return v
}
return ""
}
// getInt extracts an int value from content map (handles float64 from JSON).
func getInt(content map[string]any, key string, defaultVal int) int {
if v, ok := content[key].(float64); ok {
return int(v)
}
if v, ok := content[key].(int); ok {
return v
}
if v, ok := content[key].(string); ok {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return defaultVal
}
// getSlice extracts a slice of maps from content. Returns nil if missing.
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
}
// getStringSlice extracts a slice of strings from content. Non-string entries are coerced via fmt.Sprintf.
// Returns nil if missing.
func getStringSlice(content map[string]any, key string) []string {
if v, ok := content[key].([]any); ok {
result := make([]string, 0, len(v))
for _, item := range v {
if s, ok := item.(string); ok {
result = append(result, s)
}
}
return result
}
if v, ok := content[key].([]string); ok {
return v
}
return nil
}
// getBoolish accepts "true"/"false" strings and booleans. Returns defaultVal otherwise.
func getBoolish(content map[string]any, key string, defaultVal bool) bool {
if v, ok := content[key].(bool); ok {
return v
}
if v, ok := content[key].(string); ok {
switch v {
case "true":
return true
case "false":
return false
}
}
return defaultVal
}