refactor: Improve parsing of flight schedule data

This commit is contained in:
windyboy
2024-08-02 11:15:23 +08:00
parent d91e61ab5d
commit a734f5f25c
3 changed files with 84 additions and 4 deletions
+26 -2
View File
@@ -20,6 +20,7 @@ const (
Date = "date" Date = "date"
Task = "task" Task = "task"
// Index = "idx" // Index = "idx"
AllDigitsPattern = `^(?P<dep_time>\d+)$`
IndexPattern = `^(?P<idx>\(?L?[0-9]+\)?:?\.?)$` IndexPattern = `^(?P<idx>\(?L?[0-9]+\)?:?\.?)$`
DatePattern = `^(?P<date>\d{2}\w{3})$` DatePattern = `^(?P<date>\d{2}\w{3})$`
TaskPattern = `^(?P<task>[A-Z]\/[A-Z])$` TaskPattern = `^(?P<task>[A-Z]\/[A-Z])$`
@@ -29,6 +30,8 @@ const (
) )
var ( var (
AllDigitsExpression = regexp.MustCompile(AllDigitsPattern)
IndexExpression = regexp.MustCompile(IndexPattern) IndexExpression = regexp.MustCompile(IndexPattern)
TaskExpression = regexp.MustCompile(TaskPattern) TaskExpression = regexp.MustCompile(TaskPattern)
DateExpression = regexp.MustCompile(DatePattern) DateExpression = regexp.MustCompile(DatePattern)
@@ -139,6 +142,21 @@ var (
2: Register, 2: Register,
}, },
}, },
{
/**
* 杨子快运(Y8)
*01 Y87969 B2119 XMN 1540 HGH
*13 Y87444 B2578 ICN 0235 TSN
*/
Airlines: []string{"Y8"},
MinLen: 6,
WaypointStart: 3,
Fields: map[int]string{
0: Index,
1: FlightNumber,
2: Register,
},
},
} }
) )
@@ -322,9 +340,15 @@ func parseWaypoints(points []string) []domain.WayPoint {
return nil return nil
} }
var waypoints []domain.WayPoint var waypoints []domain.WayPoint
for _, point := range realWaypoints { for i, point := range realWaypoints {
// log.Debugf("Parsing waypoint: %s", point) // log.Debugf("Parsing waypoint: %s", point)
if waypoint := ExtractWaypoint(point); waypoint != nil { // check the next point for departure time
if digits := extract(point, AllDigitsExpression); i > 0 && digits != nil {
// if the previous point was a waypoint, update the departure time
if l := len(waypoints); l > 0 {
waypoints[l-1].DepartureTime = digits[DepartureTime]
}
} else if waypoint := ExtractWaypoint(point); waypoint != nil {
// log.Debugf("Waypoint: %v", waypoint) // log.Debugf("Waypoint: %v", waypoint)
waypoints = append(waypoints, *waypoint) waypoints = append(waypoints, *waypoint)
} else { } else {
+57 -1
View File
@@ -1,6 +1,8 @@
package parsers package parsers
import ( import (
"strings"
. "github.com/onsi/ginkgo/v2" . "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@@ -124,7 +126,7 @@ var _ = Describe("Schedule Parser", func() {
}) })
}) })
Describe("FindWaypoints", func() { Describe("FindWaypoint", func() {
It("should return the correct waypoints based on the message", func() { It("should return the correct waypoints based on the message", func() {
message := "1845(11JUN)TSN/2100" message := "1845(11JUN)TSN/2100"
waypoint := ExtractWaypoint(message) waypoint := ExtractWaypoint(message)
@@ -139,6 +141,42 @@ var _ = Describe("Schedule Parser", func() {
waypoints := ExtractWaypoint(message) waypoints := ExtractWaypoint(message)
Expect(waypoints).To(BeNil()) Expect(waypoints).To(BeNil())
}) })
It("TSN/0645", func() {
message := "TSN/0645"
waypoint := ExtractWaypoint(message)
Expect(waypoint).NotTo(BeNil())
Expect(waypoint.Airport).To(Equal("TSN"))
Expect(waypoint.DepartureTime).To(Equal("0645"))
})
})
Describe("Waypoints", func() {
Context("XIY/0415 TSN/0645 CGQ", func() {
It("should return 3 waypoints", func() {
points := strings.Split("XIY/0415 TSN/0645 CGQ", " ")
waypoints := parseWaypoints(points)
Expect(waypoints).NotTo(BeNil())
Expect(waypoints).To(HaveLen(3))
Expect(waypoints[0].Airport).To(Equal("XIY"))
Expect(waypoints[0].DepartureTime).To(Equal("0415"))
Expect(waypoints[1].Airport).To(Equal("TSN"))
Expect(waypoints[1].DepartureTime).To(Equal("0645"))
Expect(waypoints[2].Airport).To(Equal("CGQ"))
})
})
Context("ICN 0235 TSN", func() {
It("should return 2 waypoints", func() {
points := strings.Split("ICN 0235 TSN", " ")
waypoints := parseWaypoints(points)
Expect(waypoints).NotTo(BeNil())
Expect(waypoints).To(HaveLen(2))
Expect(waypoints[0].Airport).To(Equal("ICN"))
Expect(waypoints[0].DepartureTime).To(Equal("0235"))
Expect(waypoints[1].Airport).To(Equal("TSN"))
})
})
}) })
}) })
@@ -289,6 +327,24 @@ var _ = Describe("Parse Line with PreDef", func() {
}) })
}) })
Context("Y8", func() {
It("13 Y87444 B2578 ICN 0235 TSN", func() {
lineText := "13 Y87444 B2578 ICN 0235 TSN"
def := FindDef("Y8")
Expect(def).NotTo(BeNil())
schedule := ParseWithDef(lineText, def)
Expect(schedule).NotTo(BeNil())
Expect(schedule.Index).To(Equal("13"))
Expect(len(schedule.FlightNumber)).To(Equal(1))
Expect(schedule.FlightNumber[0]).To(Equal("Y87444"))
Expect(schedule.AircraftReg).To(Equal("B2578"))
Expect(len(schedule.Waypoints)).To(Equal(2))
Expect(schedule.Waypoints[0].Airport).To(Equal("ICN"))
Expect(schedule.Waypoints[0].DepartureTime).To(Equal("0235"))
Expect(schedule.Waypoints[1].Airport).To(Equal("TSN"))
})
})
}) })
// var _ = Describe("Parse Line without PreDef", func() { // var _ = Describe("Parse Line without PreDef", func() {
+1 -1
View File
@@ -40,7 +40,7 @@ var (
func load() { func load() {
if log == nil { if log == nil {
env := getEnv() env := getEnv()
fmt.Printf("Environment: %s\n", env) // fmt.Printf("Environment: %s\n", env)
configFile := getConfigFile(env) configFile := getConfigFile(env)
config, err := loadConfig(configFile) config, err := loadConfig(configFile)