Add weather report parsing for METAR, SPECI, and TAF

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>
This commit is contained in:
windyboy
2025-12-24 16:38:03 +08:00
co-authored by Claude Sonnet 4.5
parent c1808c0523
commit d0fd461e38
24 changed files with 1873 additions and 11 deletions
+69
View File
@@ -0,0 +1,69 @@
package parser
import (
"caatsm/internal/adapter/dto"
"caatsm/internal/domain/weather"
"caatsm/internal/port"
"fmt"
"time"
"github.com/google/uuid"
)
// CompositeParser combines multiple parsers (weather and aviation)
type CompositeParser struct {
aviationParser Parser
weatherParser port.WeatherParser
}
// NewCompositeParser creates a new composite parser
func NewCompositeParser(aviation Parser, weather port.WeatherParser) *CompositeParser {
return &CompositeParser{
aviationParser: aviation,
weatherParser: weather,
}
}
// Parse attempts to parse using multiple parsers
func (p *CompositeParser) Parse(rawText string) (*dto.ParsedTelegram, error) {
// 1. Try weather parser first
if p.weatherParser != nil && p.weatherParser.CanParse(rawText) {
wMsg, err := p.weatherParser.Parse(rawText)
if err == nil {
return p.weatherToTelegram(wMsg, rawText), nil
}
// If parsing fails, continue to aviation parser as fallback
}
// 2. Try aviation parser (existing logic)
return p.aviationParser.Parse(rawText)
}
// weatherToTelegram converts a WeatherMessage to ParsedTelegram
func (p *CompositeParser) weatherToTelegram(wMsg weather.WeatherMessage, raw string) *dto.ParsedTelegram {
parsed := dto.NewParsedTelegram()
parsed.Content = raw
parsed.Body = raw
parsed.Category = string(wMsg.Type())
parsed.BodyData = wMsg
parsed.Parsed = true
parsed.Status = dto.MessageStatusParsed
parsed.Uuid = uuid.New().String()
parsed.ReceivedAt = time.Now()
parsed.ParsedAt = time.Now()
// Extract basic information from weather message
switch msg := wMsg.(type) {
case *weather.Metar:
issueTime := msg.IssueTime()
parsed.MessageID = fmt.Sprintf("%s-%s", msg.Station(), issueTime.Format("20060102150405"))
parsed.DateTime = issueTime.Format("060102150405")
case *weather.Taf:
issueTime := msg.IssueTime()
parsed.MessageID = fmt.Sprintf("%s-%s", msg.Station(), issueTime.Format("20060102150405"))
parsed.DateTime = issueTime.Format("060102150405")
}
return parsed
}