refactor: Update aviation_parser.go to remove unused code and update constants
This commit is contained in:
@@ -47,6 +47,7 @@ var (
|
||||
pbnPattern,
|
||||
eetPattern,
|
||||
performancePattern,
|
||||
regPattern,
|
||||
reroutePattern}
|
||||
)
|
||||
|
||||
@@ -63,40 +64,36 @@ func NewBodyParser(body string) *BodyParser {
|
||||
}
|
||||
}
|
||||
|
||||
func (bp *BodyParser) GetBodyPatterns() map[string]BodyConfig {
|
||||
bp.mu.Lock()
|
||||
defer bp.mu.Unlock()
|
||||
return bp.bodyPatterns
|
||||
func (parser *BodyParser) GetBodyPatterns() map[string]BodyConfig {
|
||||
parser.mu.Lock()
|
||||
defer parser.mu.Unlock()
|
||||
return parser.bodyPatterns
|
||||
}
|
||||
|
||||
func (bp *BodyParser) SetBodyPatterns(patterns map[string]BodyConfig) {
|
||||
bp.mu.Lock()
|
||||
defer bp.mu.Unlock()
|
||||
bp.bodyPatterns = patterns
|
||||
func (parser *BodyParser) SetBodyPatterns(patterns map[string]BodyConfig) {
|
||||
parser.mu.Lock()
|
||||
defer parser.mu.Unlock()
|
||||
parser.bodyPatterns = patterns
|
||||
}
|
||||
|
||||
func (bp *BodyParser) Parse() (string, interface{}, error) {
|
||||
bp.mu.Lock()
|
||||
defer bp.mu.Unlock()
|
||||
func (parser *BodyParser) Parse() (string, interface{}, error) {
|
||||
parser.mu.Lock()
|
||||
defer parser.mu.Unlock()
|
||||
|
||||
bp.body = strings.TrimSpace(bp.body)
|
||||
category := findCategory(bp.body)
|
||||
parser.body = strings.TrimSpace(parser.body)
|
||||
category := findCategory(parser.body)
|
||||
if category == "" {
|
||||
return "", nil, fmt.Errorf("no category found in body text")
|
||||
}
|
||||
|
||||
if patternConfig, exists := bp.bodyPatterns[category]; exists && patternConfig.Patterns != nil {
|
||||
if patternConfig, exists := parser.bodyPatterns[category]; exists && patternConfig.Patterns != nil {
|
||||
for _, p := range patternConfig.Patterns {
|
||||
// if match := p.Expression.FindStringSubmatch(bp.body); match != nil {
|
||||
// data := extractData(match, p.Expression)
|
||||
// return bp.createBodyData(data)
|
||||
// }
|
||||
if data := extract(bp.body, p.Expression); data != nil {
|
||||
return bp.createBodyData(data)
|
||||
if data := extract(parser.body, p.Expression); data != nil {
|
||||
return parser.createBodyData(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", nil, fmt.Errorf("no matching pattern found for body: %s", bp.body)
|
||||
return "", nil, fmt.Errorf("no matching pattern found for body: %s", parser.body)
|
||||
}
|
||||
|
||||
func findCategory(body string) string {
|
||||
@@ -128,7 +125,7 @@ func extractData(match []string, re *regexp.Regexp) map[string]string {
|
||||
return data
|
||||
}
|
||||
|
||||
func (bp *BodyParser) createBodyData(data map[string]string) (string, interface{}, error) {
|
||||
func (parser *BodyParser) createBodyData(data map[string]string) (string, interface{}, error) {
|
||||
switch category := data["category"]; category {
|
||||
case CategoryArrival:
|
||||
return category, &domain.ARR{
|
||||
|
||||
@@ -99,6 +99,25 @@ NNNN`
|
||||
|
||||
})
|
||||
|
||||
Describe("Other Info", func() {
|
||||
Context("PBN/A1B2B3B4B5D1L1 NAV/ABAS REG/B6513 EET/ZBPE0112 SEL/KMAL PER/C RIF/FRT N640 ZBYN RMK/TCAS EQUIPPED", func() {
|
||||
It("should parse the other info correctly", func() {
|
||||
otherInfo := "PBN/A1B2B3B4B5D1L1 NAV/ABAS REG/B6513 EET/ZBPE0112 SEL/KMAL PER/C RIF/FRT N640 ZBYN RMK/TCAS EQUIPPED"
|
||||
parsed := parseOther(otherInfo)
|
||||
Expect(parsed).ToNot(BeNil())
|
||||
Expect(parsed[PBN]).To(Equal("A1B2B3B4B5D1L1"))
|
||||
Expect(parsed[NavigationEquipment]).To(Equal("ABAS"))
|
||||
Expect(parsed[Register]).To(Equal("B6513"))
|
||||
Expect(parsed[EstimatedElapsedTime]).To(Equal("ZBPE0112"))
|
||||
Expect(parsed[SELCALCode]).To(Equal("KMAL"))
|
||||
Expect(parsed[PerformanceCategory]).To(Equal("C"))
|
||||
Expect(parsed[RerouteInformation]).To(Equal("FRT N640 ZBYN"))
|
||||
Expect(parsed[Remarks]).To(Equal("TCAS EQUIPPED"))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Describe("ParseBody", func() {
|
||||
|
||||
Context("with ARR body (ARR-CES5470-ZBTJ-ZSHC1614)", func() {
|
||||
|
||||
@@ -61,9 +61,10 @@ var (
|
||||
emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`)
|
||||
bodyOnly = regexp.MustCompile(`(.|\n)?(ZCZC(.|\n)*)NNNN(.|\n)?$`)
|
||||
originator = regexp.MustCompile(`(?P<originatorDateTime>[0-9]+)\s(?P<originator>[A-Z]+)`)
|
||||
navPattern = regexp.MustCompile(`(?m)NAV\/(?P<nav>.*)$`)
|
||||
navPattern = regexp.MustCompile(`(?m)NAV\/(?P<nav>\w+)`)
|
||||
remarkPattern = regexp.MustCompile(`(?s)RMK\/(?P<remark>.*)`)
|
||||
selPattern = regexp.MustCompile(`(?m)SEL\/(?P<sel>\w+)`)
|
||||
regPattern = regexp.MustCompile(`(?m)REG\/(?P<reg>[A-Z0-9]+)`)
|
||||
pbnPattern = regexp.MustCompile(`(?m)PBN\/(?P<pbn>[A-Z0-9]+)`)
|
||||
eetPattern = regexp.MustCompile(`(?s)(-?EET\/(?P<eet>(?:[A-Z]{4}\d{4}\s*)+))`)
|
||||
performancePattern = regexp.MustCompile(`(?s)-?PER\/(?P<per>\w)`)
|
||||
|
||||
Reference in New Issue
Block a user