Implement comprehensive weather parsing capabilities following Clean Architecture principles with composite parser pattern for routing between aviation and weather messages. ## Features Added - Weather report parsing (METAR, SPECI, TAF) - Composite parser pattern for message routing - Lenient parsing with warnings for unrecognized tokens - Support for PROB and RMK sections in TAF - Rich domain modeling with typed weather elements ## Architecture **Domain Layer** (internal/domain/weather/): - WeatherMessage interface with Metar and Taf implementations - Weather elements: Wind, Visibility, Cloud, Temperature, Altimeter, Phenomenon - Domain errors: ErrInvalidFormat, ErrMissingStation, ErrMissingTime **Port Layer** (internal/port/weather_parser.go): - WeatherParser interface with CanParse and Parse methods **Adapter Layer** (internal/adapter/parser/weather/): - WeatherParserImpl with classification and parsing logic - Comprehensive regex patterns for weather elements - METAR/SPECI parser with element extraction - TAF parser with period handling (FM, TEMPO, BECMG, PROB) - Helper functions for time parsing and unit conversions **Composite Parser** (internal/adapter/parser/composite.go): - Routes weather reports to weather parser - Falls back to aviation parser for telegrams - Converts WeatherMessage to ParsedTelegram format ## Integration - Updated ProvideParser to create composite parser with weather parser - Added weather parser to Wire DI configuration - Updated processor_bench_test.go for weather parser integration - Documentation added in docs/weather-parser.md ## Testing - 29 comprehensive tests for weather parsing (all passing) - Tests for classification, METAR, SPECI, TAF, and composite routing - Benchmark compatibility maintained ## Fixes Applied - TAF PROB parsing: Include PROB/RMK in special section detection - Composite test: Updated to use properly formatted AFTN telegram - Linter issues: Switch statement refactor, removed unused patterns - Ineffective break statement fixed in TAF parser ## Coverage ~1,743 lines of new code with: - Complete METAR/SPECI parsing - TAF parsing with period support - Lenient error handling with warnings - Unit conversions and time utilities 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
package weather
|
|
|
|
import "strings"
|
|
|
|
// Tokenize splits the raw text into tokens by whitespace
|
|
func Tokenize(raw string) []string {
|
|
raw = strings.TrimSpace(raw)
|
|
// Remove trailing '=' if present
|
|
if strings.HasSuffix(raw, "=") {
|
|
raw = raw[:len(raw)-1]
|
|
raw = strings.TrimSpace(raw)
|
|
}
|
|
|
|
tokens := strings.Fields(raw)
|
|
return tokens
|
|
}
|
|
|
|
// Section represents a special section in the report (RMK, TEMPO, BECMG, FM)
|
|
type Section struct {
|
|
Type string // "RMK", "TEMPO", "BECMG", "FM"
|
|
StartIdx int // Starting token index
|
|
EndIdx int // Ending token index (exclusive)
|
|
Tokens []string // Tokens in this section
|
|
}
|
|
|
|
// FindSpecialSections identifies special sections in the tokenized report
|
|
func FindSpecialSections(tokens []string) []Section {
|
|
var sections []Section
|
|
var currentSection *Section
|
|
|
|
for i, token := range tokens {
|
|
upperToken := strings.ToUpper(token)
|
|
|
|
switch {
|
|
case strings.HasPrefix(upperToken, "RMK"):
|
|
if currentSection != nil {
|
|
currentSection.EndIdx = i
|
|
sections = append(sections, *currentSection)
|
|
}
|
|
currentSection = &Section{
|
|
Type: "RMK",
|
|
StartIdx: i,
|
|
Tokens: []string{token},
|
|
}
|
|
|
|
case strings.HasPrefix(upperToken, "TEMPO"):
|
|
if currentSection != nil && currentSection.Type != "RMK" {
|
|
currentSection.EndIdx = i
|
|
sections = append(sections, *currentSection)
|
|
}
|
|
currentSection = &Section{
|
|
Type: "TEMPO",
|
|
StartIdx: i,
|
|
Tokens: []string{token},
|
|
}
|
|
|
|
case strings.HasPrefix(upperToken, "BECMG"):
|
|
if currentSection != nil && currentSection.Type != "RMK" {
|
|
currentSection.EndIdx = i
|
|
sections = append(sections, *currentSection)
|
|
}
|
|
currentSection = &Section{
|
|
Type: "BECMG",
|
|
StartIdx: i,
|
|
Tokens: []string{token},
|
|
}
|
|
|
|
case strings.HasPrefix(upperToken, "FM"):
|
|
if currentSection != nil && currentSection.Type != "RMK" {
|
|
currentSection.EndIdx = i
|
|
sections = append(sections, *currentSection)
|
|
}
|
|
currentSection = &Section{
|
|
Type: "FM",
|
|
StartIdx: i,
|
|
Tokens: []string{token},
|
|
}
|
|
|
|
default:
|
|
if currentSection != nil {
|
|
currentSection.Tokens = append(currentSection.Tokens, token)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Close the last section
|
|
if currentSection != nil {
|
|
currentSection.EndIdx = len(tokens)
|
|
sections = append(sections, *currentSection)
|
|
}
|
|
|
|
return sections
|
|
}
|
|
|
|
// ExtractSectionTokens extracts tokens for a specific section type
|
|
func ExtractSectionTokens(tokens []string, sectionType string) []string {
|
|
sections := FindSpecialSections(tokens)
|
|
for _, section := range sections {
|
|
if section.Type == sectionType {
|
|
return section.Tokens
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|