Centralize report name resolution

This commit is contained in:
2026-06-15 12:33:55 +00:00
parent e8f1aa5caf
commit 40b42f4bf3
9 changed files with 297 additions and 111 deletions

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"reflect"
"strings"
"gitea.maximumdirect.net/eric/weatherreporter/internal/briefing"
"gitea.maximumdirect.net/eric/weatherreporter/internal/module"
@@ -18,7 +17,7 @@ func (cfg Config) ReportModuleOverrides() map[report.ID][]module.ConfigItem {
if !reportCfg.deterministicModulesSet {
continue
}
id, err := reportIDForConfigKey(key)
id, err := report.IDForConfigKey(key)
if err != nil {
continue
}
@@ -42,9 +41,9 @@ func normalizeReportModules(cfg *Config) error {
reportRegistry := report.DefaultRegistry()
seenReports := map[report.ID]string{}
for key, reportCfg := range cfg.Reports {
reportID, err := reportIDForConfigKey(key)
reportID, err := report.IDForConfigKey(key)
if err != nil {
return err
return fmt.Errorf("reports.%s: %w", key, err)
}
if previous, ok := seenReports[reportID]; ok {
return fmt.Errorf("reports.%s duplicates report override %q", key, previous)
@@ -105,23 +104,3 @@ func decodeKnownOptions(raw any, optionType reflect.Type) (any, error) {
}
return target.Elem().Interface(), nil
}
func reportIDForConfigKey(key string) (report.ID, error) {
normalized := strings.ReplaceAll(strings.TrimSpace(strings.ToLower(key)), "-", "_")
switch normalized {
case "daily", "daily_today":
return report.DailyToday, nil
case "tomorrow":
return report.Tomorrow, nil
case "hourly":
return report.Hourly, nil
case "three_day", "three_day_outlook":
return report.ThreeDay, nil
case "weekend", "weekend_outlook":
return report.Weekend, nil
case "storm", "storm_report":
return report.Storm, nil
default:
return "", fmt.Errorf("reports.%s is not a known report", key)
}
}