refactor parser packages

This commit is contained in:
windyboy
2025-12-24 17:23:41 +08:00
parent dbde6524b3
commit b82b707a25
14 changed files with 227 additions and 176 deletions
@@ -1,4 +1,4 @@
package parser package aviation
import ( import (
"caatsm/internal/adapter/dto" "caatsm/internal/adapter/dto"
@@ -14,35 +14,6 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
const (
SSR = "ssr"
DepartureCode = "dep"
DepartureTime = "dep_time"
ArrivalCode = "arr"
ArrivalTime = "arr_time"
DestinationCode = "dest"
OtherInfo = "other"
ReferenceData = "reference_data"
CategorySurveillance = "surve"
Indicator = "indicator"
Other = "other"
AircraftID = "aircraft"
Surveillance = "surve"
Speed = "speed"
Level = "level"
Route = "route"
EstimatedTime = "estt"
AlternateAirport = "alter"
PBN = "pbn"
NavigationEquipment = "nav"
EstimatedElapsedTime = "eet"
SELCALCode = "sel"
PerformanceCategory = "per"
RerouteInformation = "rif"
Remarks = "remark"
)
var ( var (
otherPatterns = []*regexp.Regexp{navPattern, otherPatterns = []*regexp.Regexp{navPattern,
remarkPattern, remarkPattern,
@@ -1,4 +1,4 @@
package parser package aviation
import ( import (
"testing" "testing"
@@ -134,4 +134,3 @@ func BenchmarkParseMixed(b *testing.B) {
_, _ = Parse(msg) _, _ = Parse(msg)
} }
} }
@@ -1,4 +1,4 @@
package parser package aviation
import ( import (
"caatsm/internal/domain" "caatsm/internal/domain"
@@ -1,4 +1,4 @@
package parser package aviation
import "regexp" import "regexp"
@@ -15,25 +15,39 @@ const (
CategoryDelay = "DLA" CategoryDelay = "DLA"
CategoryFlightPlan = "FPL" CategoryFlightPlan = "FPL"
CANCELLED = "CNL"
AirportCode = "airport"
Date = "date"
Task = "task"
Index = "idx"
FlightNumber = "number" FlightNumber = "number"
Register = "reg" Register = "reg"
SSR = "ssr"
DepartureCode = "dep"
DepartureTime = "dep_time"
ArrivalCode = "arr"
ArrivalTime = "arr_time"
DestinationCode = "dest"
OtherInfo = "other"
ReferenceData = "reference_data"
CategorySurveillance = "surve"
Indicator = "indicator"
Other = "other"
AircraftID = "aircraft"
Surveillance = "surve"
Speed = "speed"
Level = "level"
Route = "route"
EstimatedTime = "estt"
AlternateAirport = "alter"
PBN = "pbn"
NavigationEquipment = "nav"
EstimatedElapsedTime = "eet"
SELCALCode = "sel"
PerformanceCategory = "per"
RerouteInformation = "rif"
Remarks = "remark"
) )
// Regular expression patterns // Regular expression patterns
const ( const (
AllDigitsPattern = `^(?P<dep_time>\d+)$`
IndexPattern = `^(?P<idx>\(?L?[0-9]+\)?:?\.?)$`
DatePattern = `^(?P<date>\d{2}\w{3})$`
TaskPattern = `(?P<task>[A-Z]\/[A-Z])$`
WaypointPattern = `^(SI:)?(?P<arr_time>\d{4}(\(\d{2}[A-Z]{3}\))?)?\/?(?P<airport>[A-Z]{3})\/?(?P<dep_time>\d{4}(\(\d{2}[A-Z]{3}\))?)?$`
FlightNumberPattern = `^(?P<number>[0-9A-Z][0-9A-Z]\d{3,5}(\/\d+)*)$`
RegisterPattern = `^(?P<reg>B\d{4})$`
ArrPatternString = `^\((?P<category>[A-Z]{3})-(?P<number>[A-Z0-9]+)(\/?(?P<ssr>[A-Z0-9]+))?-(?P<dep>[A-Z]{4})-(?P<arr>[A-Z]{4})(?P<arr_time>\d{4})\)$` ArrPatternString = `^\((?P<category>[A-Z]{3})-(?P<number>[A-Z0-9]+)(\/?(?P<ssr>[A-Z0-9]+))?-(?P<dep>[A-Z]{4})-(?P<arr>[A-Z]{4})(?P<arr_time>\d{4})\)$`
DepPatternString = `^\((?P<category>[A-Z]{3})-(?P<number>[A-Z0-9]+)(\/(?P<ssr>[A-Z0-9]+))?-(?P<dep>[A-Z]{4})(?P<dep_time>\d{4})-(?P<arr>[A-Z]{4})\)$` DepPatternString = `^\((?P<category>[A-Z]{3})-(?P<number>[A-Z0-9]+)(\/(?P<ssr>[A-Z0-9]+))?-(?P<dep>[A-Z]{4})(?P<dep_time>\d{4})-(?P<arr>[A-Z]{4})\)$`
FplPatternString = `\((?P<category>[A-Z]{3})-(?P<number>[A-Z]+\d+)-(?P<indicator>[A-Z]{2})\n-(?P<aircraft>[A-Z]+\d+\/?[A-Z]?)\n?-(?P<surve>.*)\n?-(?P<dep>[A-Z]{4})(?P<dep_time>\d{4})\n?-(?P<speed>[A-Z]+\d+)(?P<level>[A-Z0-9]+)\s+(?P<route>(.|\n)+)\n-(?P<dest>[A-Z]{4})(?P<estt>\d{4})\s?(?P<alter>(\s[A-Z]{4})+)\n?-([A-Z]{3}\/(?:[A-Z]{4}\d{4}\s?)+)?(?P<other>(?m)[A-Z]{3}\/(.|\n)*)\)$` FplPatternString = `\((?P<category>[A-Z]{3})-(?P<number>[A-Z]+\d+)-(?P<indicator>[A-Z]{2})\n-(?P<aircraft>[A-Z]+\d+\/?[A-Z]?)\n?-(?P<surve>.*)\n?-(?P<dep>[A-Z]{4})(?P<dep_time>\d{4})\n?-(?P<speed>[A-Z]+\d+)(?P<level>[A-Z0-9]+)\s+(?P<route>(.|\n)+)\n-(?P<dest>[A-Z]{4})(?P<estt>\d{4})\s?(?P<alter>(\s[A-Z]{4})+)\n?-([A-Z]{3}\/(?:[A-Z]{4}\d{4}\s?)+)?(?P<other>(?m)[A-Z]{3}\/(.|\n)*)\)$`
@@ -43,19 +57,12 @@ const (
// Compiled regular expressions // Compiled regular expressions
var ( var (
AllDigitsExpression = regexp.MustCompile(AllDigitsPattern) ArrPatternExpression = regexp.MustCompile(ArrPatternString)
IndexExpression = regexp.MustCompile(IndexPattern) DepPatternExpression = regexp.MustCompile(DepPatternString)
TaskExpression = regexp.MustCompile(TaskPattern) FplPatternExpression = regexp.MustCompile(FplPatternString)
DateExpression = regexp.MustCompile(DatePattern) CnlPatternExpression = regexp.MustCompile(CnlPatternString)
WaypointExpression = regexp.MustCompile(WaypointPattern) DlaPatternExpression = regexp.MustCompile(DlaPatternString)
FlightNumberExpression = regexp.MustCompile(FlightNumberPattern) BodyTypePattern = regexp.MustCompile(`^\(([A-Z]{3})(.*\n?)+\)$`)
RegisterExpression = regexp.MustCompile(RegisterPattern)
ArrPatternExpression = regexp.MustCompile(ArrPatternString)
DepPatternExpression = regexp.MustCompile(DepPatternString)
FplPatternExpression = regexp.MustCompile(FplPatternString)
CnlPatternExpression = regexp.MustCompile(CnlPatternString)
DlaPatternExpression = regexp.MustCompile(DlaPatternString)
BodyTypePattern = regexp.MustCompile(`^\(([A-Z]{3})(.*\n?)+\)$`)
categoryRegex = regexp.MustCompile(`\((?P<category>[A-Z]+)-`) categoryRegex = regexp.MustCompile(`\((?P<category>[A-Z]+)-`)
emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`) emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`)
@@ -69,5 +76,4 @@ var (
eetPattern = regexp.MustCompile(`(?s)(-?EET\/(?P<eet>(?:[A-Z]{4}\d{4}\s*)+))`) eetPattern = regexp.MustCompile(`(?s)(-?EET\/(?P<eet>(?:[A-Z]{4}\d{4}\s*)+))`)
performancePattern = regexp.MustCompile(`(?s)-?PER\/(?P<per>\w)`) performancePattern = regexp.MustCompile(`(?s)-?PER\/(?P<per>\w)`)
reroutePattern = regexp.MustCompile(`(?m)RIF\/(?P<rif>.*)[A-Z]{3}\/`) reroutePattern = regexp.MustCompile(`(?m)RIF\/(?P<rif>.*)[A-Z]{3}\/`)
cancelledPattern = regexp.MustCompile(`\bCNL\b`)
) )
@@ -0,0 +1,99 @@
package aviation
import "regexp"
// BodyConfig represents the configuration for parsing message bodies.
type BodyConfig struct {
Patterns []PatternConfig
}
// PatternConfig represents the configuration for a specific pattern.
type PatternConfig struct {
Pattern string
Comments string
Expression *regexp.Regexp
}
var (
bodyPatterns = map[string]BodyConfig{}
)
func init() {
// Initialize body patterns.
bodyPatterns = map[string]BodyConfig{
"ARR": {
Patterns: []PatternConfig{
{
Pattern: ArrPatternString,
Comments: "Pattern for ARR message",
Expression: ArrPatternExpression,
},
},
},
"DEP": {
Patterns: []PatternConfig{
{
Pattern: DepPatternString,
Comments: "Pattern for DEP message",
Expression: DepPatternExpression,
},
},
},
"FPL": {
Patterns: []PatternConfig{
{
Pattern: FplPatternString,
Comments: "Pattern for FPL message",
Expression: FplPatternExpression,
},
},
},
"CNL": {
Patterns: []PatternConfig{
{
Pattern: CnlPatternString,
Comments: "Pattern for CNL message",
Expression: CnlPatternExpression,
},
},
},
"DLA": {
Patterns: []PatternConfig{
{
Pattern: DlaPatternString,
Comments: "Pattern for DLA message",
Expression: DlaPatternExpression,
},
},
},
}
}
// FindPatterns finds the matching body configuration based on the message body.
func FindPatterns(messageBody string) *BodyConfig {
if match := BodyTypePattern.FindStringSubmatch(messageBody); len(match) > 1 {
name := match[1]
if bodyConfig, found := bodyPatterns[name]; found {
return &bodyConfig
}
}
return nil
}
// ParseBody parses the message body and returns the extracted values.
func ParseBody(messageBody string) map[string]string {
if body := FindPatterns(messageBody); body != nil {
for _, pattern := range body.Patterns {
if matches := pattern.Expression.FindStringSubmatch(messageBody); matches != nil {
result := make(map[string]string)
for i, name := range pattern.Expression.SubexpNames() {
if i != 0 && name != "" {
result[name] = matches[i]
}
}
return result
}
}
}
return nil
}
@@ -1,4 +1,4 @@
package parser package aviation
import ( import (
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
@@ -0,0 +1,16 @@
package aviation
import "caatsm/internal/adapter/dto"
// AviationParser implements the Parser interface for aviation telegrams.
type AviationParser struct{}
// NewParser creates a new aviation parser instance.
func NewParser() *AviationParser {
return &AviationParser{}
}
// Parse parses a raw message string and returns a ParsedTelegram.
func (p *AviationParser) Parse(rawText string) (*dto.ParsedTelegram, error) {
return Parse(rawText)
}
+2 -2
View File
@@ -2,6 +2,7 @@ package parser
import ( import (
"caatsm/internal/adapter/dto" "caatsm/internal/adapter/dto"
"caatsm/internal/adapter/parser/aviation"
weatherparser "caatsm/internal/adapter/parser/weather" weatherparser "caatsm/internal/adapter/parser/weather"
"caatsm/internal/port" "caatsm/internal/port"
@@ -15,7 +16,7 @@ var _ = Describe("CompositeParser", func() {
var weatherParser port.WeatherParser var weatherParser port.WeatherParser
BeforeEach(func() { BeforeEach(func() {
aviationParser = &AviationParser{} aviationParser = aviation.NewParser()
weatherParser = weatherparser.NewWeatherParser() weatherParser = weatherparser.NewWeatherParser()
composite = NewCompositeParser(aviationParser, weatherParser) composite = NewCompositeParser(aviationParser, weatherParser)
}) })
@@ -77,4 +78,3 @@ NNNN`
}) })
}) })
}) })
+4 -13
View File
@@ -1,21 +1,12 @@
package parser package parser
import ( import (
"caatsm/internal/adapter/dto" "caatsm/internal/adapter/parser/aviation"
"caatsm/internal/port" "caatsm/internal/port"
) )
// AviationParser implements the Parser interface // ProvideParser creates a composite parser instance that combines weather and aviation parsers.
type AviationParser struct{}
// Parse parses a raw message string and returns a ParsedTelegram
func (p *AviationParser) Parse(rawText string) (*dto.ParsedTelegram, error) {
return Parse(rawText)
}
// ProvideParser creates a composite parser instance that combines weather and aviation parsers
func ProvideParser(weatherParser port.WeatherParser) Parser { func ProvideParser(weatherParser port.WeatherParser) Parser {
aviation := &AviationParser{} aviationParser := aviation.NewParser()
return NewCompositeParser(aviation, weatherParser) return NewCompositeParser(aviationParser, weatherParser)
} }
@@ -0,0 +1,38 @@
package schedule
import "regexp"
// String constants
const (
AirportCode = "airport"
Date = "date"
Task = "task"
Index = "idx"
FlightNumber = "number"
Register = "reg"
ArrivalTime = "arr_time"
DepartureTime = "dep_time"
)
// Regular expression patterns
const (
AllDigitsPattern = `^(?P<dep_time>\d+)$`
IndexPattern = `^(?P<idx>\(?L?[0-9]+\)?:?\.?)$`
DatePattern = `^(?P<date>\d{2}\w{3})$`
TaskPattern = `(?P<task>[A-Z]\/[A-Z])$`
WaypointPattern = `^(SI:)?(?P<arr_time>\d{4}(\(\d{2}[A-Z]{3}\))?)?\/?(?P<airport>[A-Z]{3})\/?(?P<dep_time>\d{4}(\(\d{2}[A-Z]{3}\))?)?$`
FlightNumberPattern = `^(?P<number>[0-9A-Z][0-9A-Z]\d{3,5}(\/\d+)*)$`
RegisterPattern = `^(?P<reg>B\d{4})$`
)
// Compiled regular expressions
var (
AllDigitsExpression = regexp.MustCompile(AllDigitsPattern)
IndexExpression = regexp.MustCompile(IndexPattern)
TaskExpression = regexp.MustCompile(TaskPattern)
DateExpression = regexp.MustCompile(DatePattern)
WaypointExpression = regexp.MustCompile(WaypointPattern)
FlightNumberExpression = regexp.MustCompile(FlightNumberPattern)
RegisterExpression = regexp.MustCompile(RegisterPattern)
cancelledPattern = regexp.MustCompile(`\bCNL\b`)
)
@@ -0,0 +1,24 @@
package schedule
import (
"regexp"
"strings"
)
func extract(data string, exp *regexp.Regexp) map[string]string {
match := exp.FindStringSubmatch(data)
if len(match) > 0 {
return extractData(match, exp)
}
return nil
}
func extractData(match []string, re *regexp.Regexp) map[string]string {
data := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
data[name] = strings.TrimSpace(match[i])
}
}
return data
}
@@ -1,20 +1,6 @@
package parser package schedule
import ( import "regexp"
"regexp"
)
// BodyConfig represents the configuration for parsing message bodies.
type BodyConfig struct {
Patterns []PatternConfig
}
// PatternConfig represents the configuration for a specific pattern.
type PatternConfig struct {
Pattern string
Comments string
Expression *regexp.Regexp
}
// LineParser represents a line parser configuration. // LineParser represents a line parser configuration.
type LineParser struct { type LineParser struct {
@@ -25,61 +11,11 @@ type LineParser struct {
} }
var ( var (
bodyPatterns = map[string]BodyConfig{} parserMap = map[string]*regexp.Regexp{}
parserMap = map[string]*regexp.Regexp{} parserDef = &[]LineParser{}
parserDef = &[]LineParser{}
) )
func init() { func init() {
// Initialize body patterns.
bodyPatterns = map[string]BodyConfig{
"ARR": {
Patterns: []PatternConfig{
{
Pattern: ArrPatternString,
Comments: "Pattern for ARR message",
Expression: ArrPatternExpression,
},
},
},
"DEP": {
Patterns: []PatternConfig{
{
Pattern: DepPatternString,
Comments: "Pattern for DEP message",
Expression: DepPatternExpression,
},
},
},
"FPL": {
Patterns: []PatternConfig{
{
Pattern: FplPatternString,
Comments: "Pattern for FPL message",
Expression: FplPatternExpression,
},
},
},
"CNL": {
Patterns: []PatternConfig{
{
Pattern: CnlPatternString,
Comments: "Pattern for CNL message",
Expression: CnlPatternExpression,
},
},
},
"DLA": {
Patterns: []PatternConfig{
{
Pattern: DlaPatternString,
Comments: "Pattern for DLA message",
Expression: DlaPatternExpression,
},
},
},
}
// Initialize parser map. // Initialize parser map.
parserMap = map[string]*regexp.Regexp{ parserMap = map[string]*regexp.Regexp{
Index: IndexExpression, Index: IndexExpression,
@@ -296,32 +232,3 @@ func init() {
}, },
} }
} }
// FindPatterns finds the matching body configuration based on the message body.
func FindPatterns(messageBody string) *BodyConfig {
if match := BodyTypePattern.FindStringSubmatch(messageBody); len(match) > 1 {
name := match[1]
if bodyConfig, found := bodyPatterns[name]; found {
return &bodyConfig
}
}
return nil
}
// ParseBody parses the message body and returns the extracted values.
func ParseBody(messageBody string) map[string]string {
if body := FindPatterns(messageBody); body != nil {
for _, pattern := range body.Patterns {
if matches := pattern.Expression.FindStringSubmatch(messageBody); matches != nil {
result := make(map[string]string)
for i, name := range pattern.Expression.SubexpNames() {
if i != 0 && name != "" {
result[name] = matches[i]
}
}
return result
}
}
}
return nil
}
@@ -1,4 +1,4 @@
package parser package schedule
import ( import (
"caatsm/internal/domain" "caatsm/internal/domain"
@@ -1,4 +1,4 @@
package parser package schedule
import ( import (
"strings" "strings"