From e4fa1380896bbba928ade3fdde09606fb5194aa7 Mon Sep 17 00:00:00 2001 From: windyboy Date: Wed, 7 Aug 2024 10:47:15 +0800 Subject: [PATCH] refactor: Update constants and remove unused code in aviation_parser.go and constants.go --- go.mod | 6 +- internal/parsers/aviation_parser.go | 29 +++-- internal/parsers/constants.go | 11 ++ internal/parsers/schedule_parser.go | 159 +++------------------------- 4 files changed, 43 insertions(+), 162 deletions(-) diff --git a/go.mod b/go.mod index 159bde9..4d8bf68 100644 --- a/go.mod +++ b/go.mod @@ -39,17 +39,17 @@ require ( github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect - github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/cast v1.7.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/vektah/gqlparser/v2 v2.5.16 // indirect github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/crypto v0.25.0 // indirect + golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.23.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/text v0.17.0 // indirect golang.org/x/tools v0.23.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/internal/parsers/aviation_parser.go b/internal/parsers/aviation_parser.go index 5a8e0b0..ad5f351 100644 --- a/internal/parsers/aviation_parser.go +++ b/internal/parsers/aviation_parser.go @@ -11,25 +11,20 @@ import ( ) const ( - StartIndicatorPrefix = "ZCZC" - EndHeaderMarker = "." - BeginPartMarker = "BEGIN PART" - Category = "category" - CategoryArrival = "ARR" // FlightNumber = "number" - SSR = "ssr" - DepartureCode = "dep" - DepartureTime = "dep_time" - ArrivalCode = "arr" - ArrivalTime = "arr_time" - CategoryDeparture = "DEP" - DestinationCode = "dest" - OtherInfo = "other" - CategoryCancellation = "CNL" - CategoryDelay = "DLA" - NewDepartureTime = "new_departure_time" - CategoryFlightPlan = "FPL" + SSR = "ssr" + DepartureCode = "dep" + DepartureTime = "dep_time" + ArrivalCode = "arr" + ArrivalTime = "arr_time" + // CategoryDeparture = "DEP" + DestinationCode = "dest" + OtherInfo = "other" + // CategoryCancellation = "CNL" + // CategoryDelay = "DLA" + // NewDepartureTime = "new_departure_time" + // CategoryFlightPlan = "FPL" ReferenceData = "reference_data" Aircraft = "aircraft" CategorySurveillance = "surve" diff --git a/internal/parsers/constants.go b/internal/parsers/constants.go index 4030e5c..6ce0063 100644 --- a/internal/parsers/constants.go +++ b/internal/parsers/constants.go @@ -4,6 +4,17 @@ import "regexp" // String constants const ( + StartIndicatorPrefix = "ZCZC" + EndHeaderMarker = "." + BeginPartMarker = "BEGIN PART" + + Category = "category" + CategoryArrival = "ARR" + CategoryDeparture = "DEP" + CategoryCancellation = "CNL" + CategoryDelay = "DLA" + CategoryFlightPlan = "FPL" + CANCELLED = "CNL" AirportCode = "airport" Date = "date" diff --git a/internal/parsers/schedule_parser.go b/internal/parsers/schedule_parser.go index bfa785f..0200902 100644 --- a/internal/parsers/schedule_parser.go +++ b/internal/parsers/schedule_parser.go @@ -97,103 +97,6 @@ func ParseWithDef(line string, parserDef *LineParser) *domain.ScheduleLine { return flightSchedule } -// func ParseLine(line string) *domain.ScheduleLine { -// log := utils.GetSugaredLogger() -// cleanLine := strings.TrimSpace(line) -// words := strings.Split(cleanLine, " ") -// var flightSchedule = &domain.ScheduleLine{ -// Reference: line, -// } -// var data map[string]string - -// if indexData := extract(words[0], IndexExpression); indexData != nil { -// flightSchedule.Index = indexData[Index] -// words = words[1:] -// } - -// // Define the parsing strategy -// parseStrategy := []string{ -// Task, -// Date, -// FlightNumber, -// Register, -// } - -// // Track parsed fields to avoid re-parsing -// parsed := make(map[string]bool) -// var maxParsed int - -// // Parse each word in the line -// for i, word := range words { -// // Check if all fields have been parsed -// for _, name := range parseStrategy { -// // Skip if already parsed -// if parsed[name] { -// continue -// } -// // Parse the word -// if data = extract(word, parserMap[name]); data != nil { -// // Update the flight schedule -// switch name { -// case Task: -// flightSchedule.Task = data[Task] -// parsed[Task] = true -// maxParsed = i -// case Date: -// flightSchedule.Date = data[Date] -// parsed[Date] = true -// maxParsed = i -// case FlightNumber: -// flightSchedule.FlightNumber = getFlightNumbers(data[FlightNumber]) -// parsed[FlightNumber] = true -// maxParsed = i -// case Register: -// flightSchedule.AircraftReg = data[Register] -// parsed[Register] = true -// maxParsed = i -// } -// break -// } -// } - -// } -// if maxParsed+1 < len(words) { -// flightSchedule.Waypoints = parseWaypoints(words[maxParsed+1:]) -// } else { -// log.Warn("No waypoints found") -// flightSchedule.Comments = "No waypoints found" -// } - -// return flightSchedule -// } -// func ParseLine(line string) *domain.ScheduleLine { -// log := utils.GetSugaredLogger() -// cleanLine := strings.TrimSpace(line) -// words := strings.Split(cleanLine, " ") -// flightSchedule := &domain.ScheduleLine{Reference: line} - -// if indexData := extract(words[0], IndexExpression); indexData != nil { -// flightSchedule.Index = indexData[Index] -// words = words[1:] -// } - -// parseStrategy := []string{Task, Date, FlightNumber, Register} -// _, maxParsed := parseFields(words, parseStrategy, flightSchedule) - -// if maxParsed+1 < len(words) { -// flightSchedule.Waypoints = parseWaypoints(words[maxParsed+1:]) -// } else { -// log.Warn("No waypoints found") -// flightSchedule.Comments = "No waypoints found" -// } -// return flightSchedule -// } - -// func parseFields(words []string, parseStrategy []string, flightSchedule *domain.ScheduleLine) (map[string]bool, int) { -// parsed := make(map[string]bool) -// var maxParsed int - -// for i, word := range words { // for _, name := range parseStrategy { // if parsed[name] { // continue @@ -209,27 +112,6 @@ func ParseWithDef(line string, parserDef *LineParser) *domain.ScheduleLine { // return parsed, maxParsed // } -// func updateFlightSchedule(flightSchedule *domain.ScheduleLine, name string, data map[string]string) { -// switch name { -// case Task: -// flightSchedule.Task = data[Task] -// case Date: -// flightSchedule.Date = data[Date] -// case FlightNumber: -// flightSchedule.FlightNumber = getFlightNumbers(data[FlightNumber]) -// case Register: -// flightSchedule.AircraftReg = data[Register] -// } -// } - -// func parseWaypoints(points []string) []domain.WayPoint { -// log := utils.GetSugaredLogger() -// if len(points) == 0 { -// log.Warn("No waypoints found") -// return nil -// } - -// //find first waypoint // var realWaypoints []string // for i, point := range points { // if extract(point, WaypointExpression) != nil { @@ -330,43 +212,36 @@ func updateFlightSchedule(flightSchedule *domain.ScheduleLine, name string, data } // parseWaypoints processes a slice of waypoint strings and returns a slice of WayPoint objects. -func parseWaypoints(points []string) ([]domain.WayPoint, error) { +func parseWaypoints(target []string) ([]domain.WayPoint, error) { log := utils.GetSugaredLogger() + points := getValidPoints(target) if len(points) == 0 { log.Warn("No waypoints found") return nil, errors.New("no waypoints") } - - //find first waypoint - var realWaypoints []string + var waypoints []domain.WayPoint for i, point := range points { - if extract(point, WaypointExpression) != nil { - realWaypoints = points[i:] - break + if digits := extract(point, AllDigitsExpression); i > 0 && digits != nil && len(waypoints) > 0 { + waypoints[len(waypoints)-1].DepartureTime = digits[DepartureTime] + } else if waypoint := ExtractWaypoint(point); waypoint != nil { + waypoints = append(waypoints, *waypoint) } } - if len(realWaypoints) == 0 { + if len(waypoints) == 0 { log.Warn("No waypoints found") return nil, errors.New("no waypoints") } - var waypoints []domain.WayPoint - for i, point := range realWaypoints { - // log.Debugf("Parsing waypoint: %s", point) - // 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 { - log.Warnf("Failed to parse waypoint: %s", point) + return waypoints, nil +} + +func getValidPoints(data []string) []string { + var points []string + for i, point := range data { + if extract(point, WaypointExpression) != nil { + return data[i:] } } - // log.Debugf("Found %d waypoints", len(waypoints)) - return waypoints, nil + return points } /**