From a57e924501267f5ff2aeae28fa9b3ab783229ec0 Mon Sep 17 00:00:00 2001 From: windyboy Date: Tue, 6 Aug 2024 12:44:32 +0800 Subject: [PATCH] refactor: Update aviation_parser.go and pattern.go to use BodyConfig struct instead of config.BodyConfig --- internal/config/config.go | 116 ++++++++++++++-------------- internal/parsers/aviation_parser.go | 9 +-- internal/parsers/pattern.go | 109 ++++++++++++++++++++++++-- 3 files changed, 166 insertions(+), 68 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 7193f65..28594e2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -63,10 +63,10 @@ const ( // - ssr: the alphanumeric SSR code // - departure: the four-letter departure airport code // - arrival: the four-letter arrival airport code - arrPatternString = `^\((?P[A-Z]{3})` + - `-(?P[A-Z0-9]+)(\/?(?P[A-Z0-9]+))?` + - `-(?P[A-Z]{4})` + - `-(?P[A-Z]{4})(?P\d{4})\)$` + // arrPatternString = `^\((?P[A-Z]{3})` + + // `-(?P[A-Z0-9]+)(\/?(?P[A-Z0-9]+))?` + + // `-(?P[A-Z]{4})` + + // `-(?P[A-Z]{4})(?P\d{4})\)$` // depPatternString represents the regular expression pattern used to match departure patterns. // The pattern matches strings in the format: "(TYPE-NUMBER-SSR-DEPARTURE-DEPARTURE_TIME-ARRIVAL)". @@ -77,7 +77,7 @@ const ( // - departure: the four-letter departure airport code // - departure_time: the four-digit departure time // - arrival: the four-letter arrival airport code - depPatternString = `^\((?P[A-Z]{3})-(?P[A-Z0-9]+)(\/(?P[A-Z0-9]+))?-(?P[A-Z]{4})(?P\d{4})-(?P[A-Z]{4})\)$` + // depPatternString = `^\((?P[A-Z]{3})-(?P[A-Z0-9]+)(\/(?P[A-Z0-9]+))?-(?P[A-Z]{4})(?P\d{4})-(?P[A-Z]{4})\)$` // fplPatternString represents the regular expression pattern used to match flight plan patterns. // The pattern matches strings in the format: "(CATEGORY-NUMBER-INDICATOR-AIRCRAFT-SURVE-DEPARTURE-DEPARTURE_TIME-SPEED-LEVEL-ROUTE-DESTINATION-ESTT-ALTER-OTHER)". @@ -97,65 +97,65 @@ const ( // - alter: one or more sequences of a whitespace character followed by exactly four uppercase letters // - other: any other relevant information, starting with three uppercase letters followed by a forward slash and zero or more characters (including newline) - fplPatternString = `\((?P[A-Z]{3})-(?P[A-Z]+\d+)-(?P[A-Z]{2})\n-(?P[A-Z]+\d+\/?[A-Z]?)\n?-(?P.*)\n?-(?P[A-Z]{4})(?P\d{4})\n?-(?P[A-Z]+\d+)(?P[A-Z0-9]+)\s+(?P(.|\n)+)\n-(?P[A-Z]{4})(?P\d{4})\s?(?P(\s[A-Z]{4})+)\n?-([A-Z]{3}\/(?:[A-Z]{4}\d{4}\s?)+)?(?P(?m)[A-Z]{3}\/(.|\n)*)\)$` + // fplPatternString = `\((?P[A-Z]{3})-(?P[A-Z]+\d+)-(?P[A-Z]{2})\n-(?P[A-Z]+\d+\/?[A-Z]?)\n?-(?P.*)\n?-(?P[A-Z]{4})(?P\d{4})\n?-(?P[A-Z]+\d+)(?P[A-Z0-9]+)\s+(?P(.|\n)+)\n-(?P[A-Z]{4})(?P\d{4})\s?(?P(\s[A-Z]{4})+)\n?-([A-Z]{3}\/(?:[A-Z]{4}\d{4}\s?)+)?(?P(?m)[A-Z]{3}\/(.|\n)*)\)$` - cnlPatternString = `^\((?P[A-Z]{3})-(?P\w+\d+)-?(?P[A-Z]{4})?-?(?[A-Z]{4})\)$` + // cnlPatternString = `^\((?P[A-Z]{3})-(?P\w+\d+)-?(?P[A-Z]{4})?-?(?[A-Z]{4})\)$` - dlaPatternString = `^\((?P[A-Z]{3})-(?P\w+\d+)-?(?P[A-Z]{4})(?P\d{4})?-?(?[A-Z]{4})(?\d{4})?\)$` + // dlaPatternString = `^\((?P[A-Z]{3})-(?P\w+\d+)-?(?P[A-Z]{4})(?P\d{4})?-?(?[A-Z]{4})(?\d{4})?\)$` ) // Initialize the bodyPatterns map -var bodyPatterns = map[string]BodyConfig{ - "ARR": { - Patterns: []PatternConfig{ - { - Pattern: arrPatternString, - Comments: "Pattern for ARR message", - Expression: regexp.MustCompile(arrPatternString), - }, - }, - }, - "DEP": { - Patterns: []PatternConfig{ - { - Pattern: depPatternString, - Comments: "Pattern for DEP message", - Expression: regexp.MustCompile(depPatternString), - }, - }, - }, - "FPL": { - Patterns: []PatternConfig{ - { - Pattern: fplPatternString, - Comments: "Pattern for FPL message", - Expression: regexp.MustCompile(fplPatternString), - }, - }, - }, - "CNL": { - Patterns: []PatternConfig{ - { - Pattern: cnlPatternString, - Comments: "Pattern for CNL message", - Expression: regexp.MustCompile(cnlPatternString), - }, - }, - }, - "DLA": { - Patterns: []PatternConfig{ - { - Pattern: dlaPatternString, - Comments: "Pattern for DLA message", - Expression: regexp.MustCompile(dlaPatternString), - }, - }, - }, -} +// var bodyPatterns = map[string]BodyConfig{ +// "ARR": { +// Patterns: []PatternConfig{ +// { +// Pattern: arrPatternString, +// Comments: "Pattern for ARR message", +// Expression: regexp.MustCompile(arrPatternString), +// }, +// }, +// }, +// "DEP": { +// Patterns: []PatternConfig{ +// { +// Pattern: depPatternString, +// Comments: "Pattern for DEP message", +// Expression: regexp.MustCompile(depPatternString), +// }, +// }, +// }, +// "FPL": { +// Patterns: []PatternConfig{ +// { +// Pattern: fplPatternString, +// Comments: "Pattern for FPL message", +// Expression: regexp.MustCompile(fplPatternString), +// }, +// }, +// }, +// "CNL": { +// Patterns: []PatternConfig{ +// { +// Pattern: cnlPatternString, +// Comments: "Pattern for CNL message", +// Expression: regexp.MustCompile(cnlPatternString), +// }, +// }, +// }, +// "DLA": { +// Patterns: []PatternConfig{ +// { +// Pattern: dlaPatternString, +// Comments: "Pattern for DLA message", +// Expression: regexp.MustCompile(dlaPatternString), +// }, +// }, +// }, +// } -func GetBodyPatterns() map[string]BodyConfig { - return bodyPatterns -} +// func GetBodyPatterns() map[string]BodyConfig { +// return bodyPatterns +// } func SetMyConfig(cfg *Config) { MyConfig = cfg diff --git a/internal/parsers/aviation_parser.go b/internal/parsers/aviation_parser.go index c44fc6f..e71e119 100644 --- a/internal/parsers/aviation_parser.go +++ b/internal/parsers/aviation_parser.go @@ -1,7 +1,6 @@ package parsers import ( - "caatsm/internal/config" "caatsm/internal/domain" "caatsm/pkg/utils" "fmt" @@ -71,24 +70,24 @@ var ( type BodyParser struct { body string - bodyPatterns map[string]config.BodyConfig + bodyPatterns map[string]BodyConfig mu sync.Mutex } func NewBodyParser(body string) *BodyParser { return &BodyParser{ - bodyPatterns: config.GetBodyPatterns(), + bodyPatterns: bodyPatterns, body: body, } } -func (bp *BodyParser) GetBodyPatterns() map[string]config.BodyConfig { +func (bp *BodyParser) GetBodyPatterns() map[string]BodyConfig { bp.mu.Lock() defer bp.mu.Unlock() return bp.bodyPatterns } -func (bp *BodyParser) SetBodyPatterns(patterns map[string]config.BodyConfig) { +func (bp *BodyParser) SetBodyPatterns(patterns map[string]BodyConfig) { bp.mu.Lock() defer bp.mu.Unlock() bp.bodyPatterns = patterns diff --git a/internal/parsers/pattern.go b/internal/parsers/pattern.go index 1dda906..d1f5cfc 100644 --- a/internal/parsers/pattern.go +++ b/internal/parsers/pattern.go @@ -1,20 +1,119 @@ package parsers import ( - "caatsm/internal/config" "regexp" ) +type BodyConfig struct { + Patterns []PatternConfig +} + +type PatternConfig struct { + Pattern string + Comments string + Expression *regexp.Regexp +} + +const ( + arrPatternString = `^\((?P[A-Z]{3})` + + `-(?P[A-Z0-9]+)(\/?(?P[A-Z0-9]+))?` + + `-(?P[A-Z]{4})` + + `-(?P[A-Z]{4})(?P\d{4})\)$` + + // depPatternString represents the regular expression pattern used to match departure patterns. + // The pattern matches strings in the format: "(TYPE-NUMBER-SSR-DEPARTURE-DEPARTURE_TIME-ARRIVAL)". + // The pattern captures the following named groups: + // - category: the three-letter category code + // - number: the alphanumeric flight number + // - ssr: the alphanumeric SSR code + // - departure: the four-letter departure airport code + // - departure_time: the four-digit departure time + // - arrival: the four-letter arrival airport code + depPatternString = `^\((?P[A-Z]{3})-(?P[A-Z0-9]+)(\/(?P[A-Z0-9]+))?-(?P[A-Z]{4})(?P\d{4})-(?P[A-Z]{4})\)$` + + // fplPatternString represents the regular expression pattern used to match flight plan patterns. + // The pattern matches strings in the format: "(CATEGORY-NUMBER-INDICATOR-AIRCRAFT-SURVE-DEPARTURE-DEPARTURE_TIME-SPEED-LEVEL-ROUTE-DESTINATION-ESTT-ALTER-OTHER)". + // The pattern captures the following named groups: + // - category: the three-letter category code + // - number: the alphanumeric flight number + // - indicator: the two-letter indicator + // - aircraft: the alphanumeric aircraft type, optionally followed by a slash and an uppercase letter + // - surve: any character sequence (surveillance information) + // - departure: the four-letter departure airport code + // - departure_time: the four-digit departure time + // - speed: one or more uppercase letters followed by one or more digits (e.g., N123) + // - level: one or more alphanumeric characters (e.g., FL350) + // - route: one or more characters (including newline) representing the flight route + // - destination: the four-letter destination airport code + // - estt: the four-digit estimated time of arrival + // - alter: one or more sequences of a whitespace character followed by exactly four uppercase letters + // - other: any other relevant information, starting with three uppercase letters followed by a forward slash and zero or more characters (including newline) + + fplPatternString = `\((?P[A-Z]{3})-(?P[A-Z]+\d+)-(?P[A-Z]{2})\n-(?P[A-Z]+\d+\/?[A-Z]?)\n?-(?P.*)\n?-(?P[A-Z]{4})(?P\d{4})\n?-(?P[A-Z]+\d+)(?P[A-Z0-9]+)\s+(?P(.|\n)+)\n-(?P[A-Z]{4})(?P\d{4})\s?(?P(\s[A-Z]{4})+)\n?-([A-Z]{3}\/(?:[A-Z]{4}\d{4}\s?)+)?(?P(?m)[A-Z]{3}\/(.|\n)*)\)$` + + cnlPatternString = `^\((?P[A-Z]{3})-(?P\w+\d+)-?(?P[A-Z]{4})?-?(?[A-Z]{4})\)$` + + dlaPatternString = `^\((?P[A-Z]{3})-(?P\w+\d+)-?(?P[A-Z]{4})(?P\d{4})?-?(?[A-Z]{4})(?\d{4})?\)$` +) + var ( bodyTypePattern = regexp.MustCompile(`^\(([A-Z]{3})(.*\n?)+\)$`) + + bodyPatterns = map[string]BodyConfig{ + "ARR": { + Patterns: []PatternConfig{ + { + Pattern: arrPatternString, + Comments: "Pattern for ARR message", + Expression: regexp.MustCompile(arrPatternString), + }, + }, + }, + "DEP": { + Patterns: []PatternConfig{ + { + Pattern: depPatternString, + Comments: "Pattern for DEP message", + Expression: regexp.MustCompile(depPatternString), + }, + }, + }, + "FPL": { + Patterns: []PatternConfig{ + { + Pattern: fplPatternString, + Comments: "Pattern for FPL message", + Expression: regexp.MustCompile(fplPatternString), + }, + }, + }, + "CNL": { + Patterns: []PatternConfig{ + { + Pattern: cnlPatternString, + Comments: "Pattern for CNL message", + Expression: regexp.MustCompile(cnlPatternString), + }, + }, + }, + "DLA": { + Patterns: []PatternConfig{ + { + Pattern: dlaPatternString, + Comments: "Pattern for DLA message", + Expression: regexp.MustCompile(dlaPatternString), + }, + }, + }, + } ) -func FindPatterns(messageBody string) *config.BodyConfig { +func FindPatterns(messageBody string) *BodyConfig { if match := bodyTypePattern.FindStringSubmatch(messageBody); len(match) > 1 { name := match[1] - patters := config.GetBodyPatterns() - if body, found := patters[name]; found { - return &body + patters := bodyPatterns + if bodyConfig, found := patters[name]; found { + return &bodyConfig } } return nil