refactor parser packages
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package parser
|
||||
package aviation
|
||||
|
||||
import (
|
||||
"caatsm/internal/adapter/dto"
|
||||
@@ -14,35 +14,6 @@ import (
|
||||
"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 (
|
||||
otherPatterns = []*regexp.Regexp{navPattern,
|
||||
remarkPattern,
|
||||
+1
-2
@@ -1,4 +1,4 @@
|
||||
package parser
|
||||
package aviation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -134,4 +134,3 @@ func BenchmarkParseMixed(b *testing.B) {
|
||||
_, _ = Parse(msg)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package parser
|
||||
package aviation
|
||||
|
||||
import (
|
||||
"caatsm/internal/domain"
|
||||
+28
-22
@@ -1,4 +1,4 @@
|
||||
package parser
|
||||
package aviation
|
||||
|
||||
import "regexp"
|
||||
|
||||
@@ -15,25 +15,39 @@ const (
|
||||
CategoryDelay = "DLA"
|
||||
CategoryFlightPlan = "FPL"
|
||||
|
||||
CANCELLED = "CNL"
|
||||
AirportCode = "airport"
|
||||
Date = "date"
|
||||
Task = "task"
|
||||
Index = "idx"
|
||||
FlightNumber = "number"
|
||||
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
|
||||
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})\)$`
|
||||
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)*)\)$`
|
||||
@@ -43,13 +57,6 @@ const (
|
||||
|
||||
// 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)
|
||||
ArrPatternExpression = regexp.MustCompile(ArrPatternString)
|
||||
DepPatternExpression = regexp.MustCompile(DepPatternString)
|
||||
FplPatternExpression = regexp.MustCompile(FplPatternString)
|
||||
@@ -69,5 +76,4 @@ var (
|
||||
eetPattern = regexp.MustCompile(`(?s)(-?EET\/(?P<eet>(?:[A-Z]{4}\d{4}\s*)+))`)
|
||||
performancePattern = regexp.MustCompile(`(?s)-?PER\/(?P<per>\w)`)
|
||||
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
-1
@@ -1,4 +1,4 @@
|
||||
package parser
|
||||
package aviation
|
||||
|
||||
import (
|
||||
. "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,6 +2,7 @@ package parser
|
||||
|
||||
import (
|
||||
"caatsm/internal/adapter/dto"
|
||||
"caatsm/internal/adapter/parser/aviation"
|
||||
weatherparser "caatsm/internal/adapter/parser/weather"
|
||||
"caatsm/internal/port"
|
||||
|
||||
@@ -15,7 +16,7 @@ var _ = Describe("CompositeParser", func() {
|
||||
var weatherParser port.WeatherParser
|
||||
|
||||
BeforeEach(func() {
|
||||
aviationParser = &AviationParser{}
|
||||
aviationParser = aviation.NewParser()
|
||||
weatherParser = weatherparser.NewWeatherParser()
|
||||
composite = NewCompositeParser(aviationParser, weatherParser)
|
||||
})
|
||||
@@ -77,4 +78,3 @@ NNNN`
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -1,21 +1,12 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"caatsm/internal/adapter/dto"
|
||||
"caatsm/internal/adapter/parser/aviation"
|
||||
"caatsm/internal/port"
|
||||
)
|
||||
|
||||
// AviationParser implements the Parser interface
|
||||
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
|
||||
// ProvideParser creates a composite parser instance that combines weather and aviation parsers.
|
||||
func ProvideParser(weatherParser port.WeatherParser) Parser {
|
||||
aviation := &AviationParser{}
|
||||
return NewCompositeParser(aviation, weatherParser)
|
||||
aviationParser := aviation.NewParser()
|
||||
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 (
|
||||
"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
|
||||
}
|
||||
import "regexp"
|
||||
|
||||
// LineParser represents a line parser configuration.
|
||||
type LineParser struct {
|
||||
@@ -25,61 +11,11 @@ type LineParser struct {
|
||||
}
|
||||
|
||||
var (
|
||||
bodyPatterns = map[string]BodyConfig{}
|
||||
parserMap = map[string]*regexp.Regexp{}
|
||||
parserDef = &[]LineParser{}
|
||||
)
|
||||
|
||||
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.
|
||||
parserMap = map[string]*regexp.Regexp{
|
||||
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 (
|
||||
"caatsm/internal/domain"
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package parser
|
||||
package schedule
|
||||
|
||||
import (
|
||||
"strings"
|
||||
Reference in New Issue
Block a user