Share day-style generated text validation
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
package generatedtext
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Tomorrow struct {
|
||||
Summary string `json:"summary"`
|
||||
ForecastDiscussion []string `json:"forecast_discussion"`
|
||||
@@ -13,36 +8,21 @@ type Tomorrow struct {
|
||||
}
|
||||
|
||||
func ValidateTomorrow(data []byte) (Tomorrow, []byte, error) {
|
||||
value, err := decodeGeneratedText[Tomorrow](data, "tomorrow")
|
||||
if err != nil {
|
||||
return Tomorrow{}, nil, err
|
||||
}
|
||||
|
||||
value.Summary = strings.TrimSpace(value.Summary)
|
||||
value.PrecipitationTiming = strings.TrimSpace(value.PrecipitationTiming)
|
||||
value.Confidence = strings.TrimSpace(value.Confidence)
|
||||
value.ForecastDiscussion = trimNonEmpty(value.ForecastDiscussion)
|
||||
if value.Summary == "" {
|
||||
return Tomorrow{}, nil, fmt.Errorf("tomorrow generated text summary is required")
|
||||
}
|
||||
if len(value.ForecastDiscussion) == 0 {
|
||||
return Tomorrow{}, nil, fmt.Errorf("tomorrow generated text forecast discussion is required")
|
||||
}
|
||||
|
||||
normalized, err := normalizeGeneratedText(value, "tomorrow")
|
||||
if err != nil {
|
||||
return Tomorrow{}, nil, err
|
||||
}
|
||||
return value, normalized, nil
|
||||
return validateDayStyleGeneratedText[Tomorrow, *Tomorrow](data, "tomorrow")
|
||||
}
|
||||
|
||||
func trimNonEmpty(values []string) []string {
|
||||
out := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed != "" {
|
||||
out = append(out, trimmed)
|
||||
}
|
||||
func (t *Tomorrow) dayStyleFields() dayStyleFields {
|
||||
return dayStyleFields{
|
||||
Summary: t.Summary,
|
||||
ForecastDiscussion: t.ForecastDiscussion,
|
||||
PrecipitationTiming: t.PrecipitationTiming,
|
||||
Confidence: t.Confidence,
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (t *Tomorrow) setDayStyleFields(fields dayStyleFields) {
|
||||
t.Summary = fields.Summary
|
||||
t.ForecastDiscussion = fields.ForecastDiscussion
|
||||
t.PrecipitationTiming = fields.PrecipitationTiming
|
||||
t.Confidence = fields.Confidence
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user