68 lines
2.0 KiB
Go
68 lines
2.0 KiB
Go
package briefing
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gitea.maximumdirect.net/eric/weatherreporter/internal/module"
|
|
)
|
|
|
|
type AreaForecastDiscussionModule struct {
|
|
Product string `json:"product,omitempty"`
|
|
KeyMessages []string `json:"key_messages,omitempty"`
|
|
ShortTerm string `json:"short_term,omitempty"`
|
|
LongTerm string `json:"long_term,omitempty"`
|
|
}
|
|
|
|
func buildAreaForecastDiscussionModule(ctx ModuleContext, options any) (*module.Output, error) {
|
|
discussion := ctx.Collected.Discussion
|
|
if discussion == nil {
|
|
return nil, nil
|
|
}
|
|
opts, ok := options.(module.AreaForecastDiscussionOptions)
|
|
if !ok {
|
|
return nil, fmt.Errorf("area forecast discussion options have type %T", options)
|
|
}
|
|
sections, err := areaForecastDiscussionSections(opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
value := AreaForecastDiscussionModule{}
|
|
if sections["product"] {
|
|
value.Product = discussion.Product
|
|
}
|
|
if sections["key_messages"] {
|
|
value.KeyMessages = append([]string(nil), discussion.KeyMessages...)
|
|
}
|
|
if sections["short_term"] && discussion.ShortTerm != nil {
|
|
value.ShortTerm = discussion.ShortTerm.Text
|
|
}
|
|
if sections["long_term"] && discussion.LongTerm != nil {
|
|
value.LongTerm = discussion.LongTerm.Text
|
|
}
|
|
if value.Product == "" && len(value.KeyMessages) == 0 && value.ShortTerm == "" && value.LongTerm == "" {
|
|
return nil, nil
|
|
}
|
|
return &module.Output{ID: module.AreaForecastDiscussion, StanzaName: "area_forecast_discussion", Value: value}, nil
|
|
}
|
|
|
|
func areaForecastDiscussionSections(options module.AreaForecastDiscussionOptions) (map[string]bool, error) {
|
|
if len(options.Sections) == 0 {
|
|
return map[string]bool{
|
|
"product": true,
|
|
"key_messages": true,
|
|
"short_term": true,
|
|
"long_term": true,
|
|
}, nil
|
|
}
|
|
sections := map[string]bool{}
|
|
for _, section := range options.Sections {
|
|
switch section {
|
|
case "product", "key_messages", "short_term", "long_term":
|
|
sections[section] = true
|
|
default:
|
|
return nil, fmt.Errorf("area forecast discussion section %q is not supported", section)
|
|
}
|
|
}
|
|
return sections, nil
|
|
}
|