refactor: Add CNL message parsing logic to BodyParser

The code changes in `aviation_parser.go` and `aviation_parser_test.go` add CNL message parsing logic to the `BodyParser` module. This allows for the correct extraction and parsing of CNL messages, including the category, aircraft ID, departure airport, and destination airport. This enhancement improves the overall functionality and accuracy of the code.
This commit is contained in:
windyboy
2024-07-24 17:15:35 +08:00
parent 1715591ef5
commit e58fa3bb36
4 changed files with 62 additions and 0 deletions
+11
View File
@@ -98,6 +98,8 @@ const (
// - other: any other relevant information, starting with three uppercase letters followed by a forward slash and zero or more characters (including newline)
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<departure>[A-Z]{4})(?P<departure_time>\d{4})\n?-(?P<speed>[A-Z]+\d+)(?P<level>[A-Z0-9]+)\s+(?P<route>(.|\n)+)\n-(?P<destination>[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)*)\)$`
cnlPatternString = `^\((?P<category>[A-Z]{3})-(?P<number>\w+\d+)-?(?P<departure>[A-Z]{4})?-?(?<destination>[A-Z]{4})\)$`
)
// Initialize the bodyPatterns map
@@ -129,6 +131,15 @@ var bodyPatterns = map[string]BodyConfig{
},
},
},
"CNL": {
Patterns: []PatternConfig{
{
Pattern: cnlPatternString,
Comments: "Pattern for CNL message",
Expression: regexp.MustCompile(cnlPatternString),
},
},
},
}
func GetBodyPatterns() map[string]BodyConfig {
+29
View File
@@ -0,0 +1,29 @@
package domain
import "fmt"
// CNL represents the structure of a cancellation (CNL) message
type CNL struct {
Category string `json:"category"` // 电报类别 (Message Category)
AircraftID string `json:"aircraft_id"` // 航空器识别标志 (Aircraft Identification)
DepartureAirport string `json:"departure_airport"` // 起飞机场 (Departure Airport)
DestinationAirport string `json:"destination_airport"` // 到达机场 (Destination Airport)
OtherInfo string `json:"other_info,omitempty"` // 其他信息 (optional) (Other Information)
}
// Validate validates the CNL struct fields
func (c *CNL) Validate() error {
if c.Category == "" {
return fmt.Errorf("telegram category is required")
}
if c.AircraftID == "" {
return fmt.Errorf("aircraft id is required")
}
if c.DepartureAirport == "" {
return fmt.Errorf("departure airport is required")
}
if c.DestinationAirport == "" {
return fmt.Errorf("destination airport is required")
}
return nil
}
+7
View File
@@ -119,6 +119,13 @@ func (bp *BodyParser) createBodyData(data map[string]string) (string, interface{
DepartureTime: data["departure_time"],
Destination: data["arrival"],
}, nil
case "CNL":
return category, &domain.CNL{
Category: data["category"],
AircraftID: data["number"],
DepartureAirport: data["departure"],
DestinationAirport: data["destination"],
}, nil
case "FPL":
otherData := parseOther(data["other"])
return category, &domain.FPL{
+15
View File
@@ -213,6 +213,21 @@ NNNN`
})
})
Context("with CNL body", func() {
// parser := NewBodyParser()
It("should parse the body correctly", func() {
body := "(CNL-YZR7979-ZSPD-ZBTJ)"
parser := NewBodyParser(body)
category, parsedBody, err := parser.Parse()
Expect(err).ToNot(HaveOccurred())
Expect(parsedBody).ToNot(BeNil())
Expect(category).To(Equal("CNL"))
Expect(parsedBody).To(BeAssignableToTypeOf(&domain.CNL{}))
cnlMessage := parsedBody.(*domain.CNL)
Expect(cnlMessage.AircraftID).To(Equal("YZR7979"))
})
})
})
Describe("Parse whole real message", func() {