refactor: Update ARR body parsing logic

This commit is contained in:
windyboy
2024-07-22 14:43:35 +08:00
parent 25bb5aa404
commit 3115162454
4 changed files with 77 additions and 16 deletions
+4 -1
View File
@@ -76,7 +76,10 @@ const (
// - ssr: the alphanumeric SSR code // - ssr: the alphanumeric SSR code
// - departure: the four-letter departure airport code // - departure: the four-letter departure airport code
// - arrival: the four-letter arrival airport code // - arrival: the four-letter arrival airport code
arrPatternString = `^\((?P<category>[A-Z]{3})\-(?P<number>[A-Z0-9]+)(\-(?P<ssr>[A-Z]+[0-9]+))?\-(?P<departure>[A-Z]{4})\-(?P<arrival>[A-Z]{4})(?P<time>\d{4})\)$` arrPatternString = `^\((?P<category>[A-Z]{3})` +
`-(?P<number>[A-Z0-9]+)(\/?(?P<ssr>[A-Z0-9]+))?` +
`-(?P<departure>[A-Z]{4})` +
`-(?P<arrival>[A-Z]{4})(?P<time>\d{4})\)$`
// depPatternString represents the regular expression pattern used to match departure patterns. // 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 matches strings in the format: "(TYPE-NUMBER-SSR-DEPARTURE-DEPARTURE_TIME-ARRIVAL)".
+6 -5
View File
@@ -3,7 +3,6 @@ package nats
import ( import (
"caatsm/internal/config" "caatsm/internal/config"
"caatsm/internal/parsers" "caatsm/internal/parsers"
"caatsm/pkg/utils"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@@ -60,17 +59,19 @@ func (n *NatsHandler) Subscribe() {
} }
} }
func (n *NatsHandler) handleMessage(msg *message.Message) error { func (n *NatsHandler) handleMessage(msg *message.Message) error {
log := utils.Logger // log := utils.Logger
if msg.Payload == nil { if msg.Payload == nil {
log.Error("empty message") fmt.Println("empty message")
return fmt.Errorf("empty message") return fmt.Errorf("empty message")
} }
payload := string(msg.Payload) payload := string(msg.Payload)
if parsed, err := parsers.Parse(payload); err != nil { if parsed, err := parsers.Parse(payload); err != nil {
log.Error("error parsing message", err, map[string]interface{}{"payload": payload}) // log.Error("error parsing message", err, map[string]interface{}{"payload": payload})
fmt.Print("error parsing message", err)
return err return err
} else { } else {
log.Info("message ", map[string]interface{}{"message": parsed}) // log.Info("message ", map[string]interface{}{"message": parsed})
fmt.Println("message ", parsed)
} }
return nil return nil
} }
+3 -3
View File
@@ -18,7 +18,7 @@ const (
var ( var (
categoryRegex = regexp.MustCompile(`\((?P<category>[A-Z]+)-`) categoryRegex = regexp.MustCompile(`\((?P<category>[A-Z]+)-`)
emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`) emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`)
bodyOnly = regexp.MustCompile(`(.|\n)?((ZCZC(.|\n)*))NNNN$`) bodyOnly = regexp.MustCompile(`(.|\n)?(ZCZC(.|\n)*)NNNN(.|\n)?$`)
) )
type BodyParser struct { type BodyParser struct {
@@ -169,7 +169,7 @@ func Parse(rawText string) (*domain.ParsedMessage, error) {
func clean(text string) string { func clean(text string) string {
cleanedText := emptyLineRemove.ReplaceAllString(text, "") cleanedText := emptyLineRemove.ReplaceAllString(text, "")
cleanText := strings.ReplaceAll(cleanedText, "\n\n", "\n") cleanText := strings.ReplaceAll(cleanedText, "\n\n", "\n")
if bodyOnly != nil { // if bodyOnly != nil {
match := bodyOnly.FindStringSubmatch(cleanText) match := bodyOnly.FindStringSubmatch(cleanText)
if len(match) > 1 { if len(match) > 1 {
bodyOnly := match[2] bodyOnly := match[2]
@@ -178,7 +178,7 @@ func clean(text string) string {
} }
return bodyOnly return bodyOnly
} }
} // }
return "" return ""
} }
+57
View File
@@ -145,6 +145,20 @@ NNNN`
Expect(arrMessage.ArrivalAirport).To(Equal("KLAX")) Expect(arrMessage.ArrivalAirport).To(Equal("KLAX"))
}) })
It("should parse the body (ARR-JAE7433/A0132-RKSI-ZBTJ1604) correctly", func() {
body := " (ARR-JAE7433/A0132-RKSI-ZBTJ1604)"
parsedBody, err := parser.Parse(body)
Expect(err).ToNot(HaveOccurred())
Expect(parsedBody).ToNot(BeNil())
Expect(parsedBody).To(BeAssignableToTypeOf(&domain.ARR{}))
arrMessage := parsedBody.(*domain.ARR)
Expect(arrMessage.Category).To(Equal("ARR"))
Expect(arrMessage.AircraftID).To(Equal("AB123"))
Expect(arrMessage.SSRModeAndCode).To(Equal("SSR1234"))
Expect(arrMessage.DepartureAirport).To(Equal("KJFK"))
Expect(arrMessage.ArrivalAirport).To(Equal("KLAX"))
})
}) })
Context("with DEP body", func() { Context("with DEP body", func() {
@@ -201,4 +215,47 @@ NNNN`
}) })
}) })
}) })
Describe("Parse whole real message", func() {
Context("with a real arr message", func() {
message := `
ZCZC TMQ2526 141605
FF ZBTJZPZX
141604 ZBACZQZX
(ARR-JAE7433/A0132-RKSI-ZBTJ1604)
NNNN
`
It("should parse the whole message correctly", func() {
parsedMessage, err := Parse(message)
Expect(err).ToNot(HaveOccurred())
Expect(parsedMessage).ToNot(BeNil())
Expect(parsedMessage.MessageID).To(Equal("TMQ2526"))
Expect(parsedMessage.DateTime).To(Equal("141605"))
Expect(parsedMessage.PrimaryAddress).To(Equal("ZBTJZPZX"))
Expect(parsedMessage.SecondaryAddresses).To(Equal([]string{"141604 ZBACZQZX"}))
arrmsg := parsedMessage.BodyData.(*domain.ARR)
Expect(arrmsg.Category).To(Equal("ARR"))
Expect(arrmsg.AircraftID).To(Equal("JAE7433"))
Expect(arrmsg.SSRModeAndCode).To(Equal("A0132"))
Expect(arrmsg.DepartureAirport).To(Equal("RKSI"))
Expect(arrmsg.ArrivalAirport).To(Equal("ZBTJ"))
Expect(arrmsg.ArrivalTime).To(Equal("1604"))
})
})
})
}) })