refactor: Update ARR body parsing logic
This commit is contained in:
@@ -76,7 +76,10 @@ const (
|
||||
// - ssr: the alphanumeric SSR code
|
||||
// - departure: the four-letter departure 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.
|
||||
// The pattern matches strings in the format: "(TYPE-NUMBER-SSR-DEPARTURE-DEPARTURE_TIME-ARRIVAL)".
|
||||
|
||||
@@ -3,7 +3,6 @@ package nats
|
||||
import (
|
||||
"caatsm/internal/config"
|
||||
"caatsm/internal/parsers"
|
||||
"caatsm/pkg/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -60,17 +59,19 @@ func (n *NatsHandler) Subscribe() {
|
||||
}
|
||||
}
|
||||
func (n *NatsHandler) handleMessage(msg *message.Message) error {
|
||||
log := utils.Logger
|
||||
// log := utils.Logger
|
||||
if msg.Payload == nil {
|
||||
log.Error("empty message")
|
||||
fmt.Println("empty message")
|
||||
return fmt.Errorf("empty message")
|
||||
}
|
||||
payload := string(msg.Payload)
|
||||
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
|
||||
} else {
|
||||
log.Info("message ", map[string]interface{}{"message": parsed})
|
||||
// log.Info("message ", map[string]interface{}{"message": parsed})
|
||||
fmt.Println("message ", parsed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ const (
|
||||
var (
|
||||
categoryRegex = regexp.MustCompile(`\((?P<category>[A-Z]+)-`)
|
||||
emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`)
|
||||
bodyOnly = regexp.MustCompile(`(.|\n)?((ZCZC(.|\n)*))NNNN$`)
|
||||
bodyOnly = regexp.MustCompile(`(.|\n)?(ZCZC(.|\n)*)NNNN(.|\n)?$`)
|
||||
)
|
||||
|
||||
type BodyParser struct {
|
||||
@@ -169,16 +169,16 @@ func Parse(rawText string) (*domain.ParsedMessage, error) {
|
||||
func clean(text string) string {
|
||||
cleanedText := emptyLineRemove.ReplaceAllString(text, "")
|
||||
cleanText := strings.ReplaceAll(cleanedText, "\n\n", "\n")
|
||||
if bodyOnly != nil {
|
||||
match := bodyOnly.FindStringSubmatch(cleanText)
|
||||
if len(match) > 1 {
|
||||
bodyOnly := match[2]
|
||||
if bodyOnly[len(bodyOnly)-1] == '\n' {
|
||||
return bodyOnly[:len(bodyOnly)-1]
|
||||
}
|
||||
return bodyOnly
|
||||
// if bodyOnly != nil {
|
||||
match := bodyOnly.FindStringSubmatch(cleanText)
|
||||
if len(match) > 1 {
|
||||
bodyOnly := match[2]
|
||||
if bodyOnly[len(bodyOnly)-1] == '\n' {
|
||||
return bodyOnly[:len(bodyOnly)-1]
|
||||
}
|
||||
return bodyOnly
|
||||
}
|
||||
// }
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
@@ -132,7 +132,21 @@ NNNN`
|
||||
Context("with ARR body", func() {
|
||||
parser := NewBodyParser()
|
||||
It("should parse the body (ARR-AB123-SSR1234-KJFK-KLAX1234) correctly", func() {
|
||||
body := "(ARR-AB123-SSR1234-KJFK-KLAX1234)"
|
||||
body := " (ARR-AB123-SSR1234-KJFK-KLAX1234)"
|
||||
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"))
|
||||
})
|
||||
|
||||
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())
|
||||
@@ -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"))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user