diff --git a/internal/parsers/schedule_parser.go b/internal/parsers/schedule_parser.go index 1b42c98..f217f84 100644 --- a/internal/parsers/schedule_parser.go +++ b/internal/parsers/schedule_parser.go @@ -20,6 +20,7 @@ const ( Date = "date" Task = "task" // Index = "idx" + AllDigitsPattern = `^(?P\d+)$` IndexPattern = `^(?P\(?L?[0-9]+\)?:?\.?)$` DatePattern = `^(?P\d{2}\w{3})$` TaskPattern = `^(?P[A-Z]\/[A-Z])$` @@ -29,6 +30,8 @@ const ( ) var ( + AllDigitsExpression = regexp.MustCompile(AllDigitsPattern) + IndexExpression = regexp.MustCompile(IndexPattern) TaskExpression = regexp.MustCompile(TaskPattern) DateExpression = regexp.MustCompile(DatePattern) @@ -139,6 +142,21 @@ var ( 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 } var waypoints []domain.WayPoint - for _, point := range realWaypoints { + for i, point := range realWaypoints { // 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) waypoints = append(waypoints, *waypoint) } else { diff --git a/internal/parsers/schedule_parser_test.go b/internal/parsers/schedule_parser_test.go index 00e5303..aeb55ec 100644 --- a/internal/parsers/schedule_parser_test.go +++ b/internal/parsers/schedule_parser_test.go @@ -1,6 +1,8 @@ package parsers import ( + "strings" + . "github.com/onsi/ginkgo/v2" . "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() { message := "1845(11JUN)TSN/2100" waypoint := ExtractWaypoint(message) @@ -139,6 +141,42 @@ var _ = Describe("Schedule Parser", func() { waypoints := ExtractWaypoint(message) 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() { diff --git a/pkg/utils/log.go b/pkg/utils/log.go index a3d96cb..626c2ee 100644 --- a/pkg/utils/log.go +++ b/pkg/utils/log.go @@ -40,7 +40,7 @@ var ( func load() { if log == nil { env := getEnv() - fmt.Printf("Environment: %s\n", env) + // fmt.Printf("Environment: %s\n", env) configFile := getConfigFile(env) config, err := loadConfig(configFile)