refactor: Update parseWaypoints function to return error in schedule_parser_test.go

This commit is contained in:
windyboy
2024-08-06 22:28:46 +08:00
parent afafd51bad
commit bbaea62788
5 changed files with 293 additions and 117 deletions
+220 -56
View File
@@ -3,6 +3,7 @@ package parsers
import (
"caatsm/internal/domain"
"caatsm/pkg/utils"
"errors"
"strings"
)
@@ -87,7 +88,7 @@ func ParseWithDef(line string, parserDef *LineParser) *domain.ScheduleLine {
}
}
if len(words) > parserDef.WaypointStart {
flightSchedule.Waypoints = parseWaypoints(words[parserDef.WaypointStart:])
flightSchedule.Waypoints, _ = parseWaypoints(words[parserDef.WaypointStart:])
} else {
log.Warn("No waypoints found")
flightSchedule.Comments = "No waypoints found"
@@ -96,81 +97,244 @@ func ParseWithDef(line string, parserDef *LineParser) *domain.ScheduleLine {
return flightSchedule
}
func ParseLine(line string) *domain.ScheduleLine {
// 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
// }
// if data := extract(word, parserMap[name]); data != nil {
// updateFlightSchedule(flightSchedule, name, data)
// parsed[name] = true
// maxParsed = i
// break
// }
// }
// }
// 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 {
// realWaypoints = points[i:]
// break
// }
// }
// if len(realWaypoints) == 0 {
// log.Warn("No waypoints found")
// return nil
// }
// 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)
// }
// }
// // log.Debugf("Found %d waypoints", len(waypoints))
// return waypoints
// }
// ParseLine processes a single line of schedule data and returns a ScheduleLine object.
func ParseLine(line string) (*domain.ScheduleLine, error) {
log := utils.GetSugaredLogger()
cleanLine := strings.TrimSpace(line)
words := strings.Split(cleanLine, " ")
var flightSchedule = &domain.ScheduleLine{
Reference: line,
}
var data map[string]string
flightSchedule := &domain.ScheduleLine{Reference: line}
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,
parseStrategy := []string{Task, Date, FlightNumber, Register}
_, maxParsed, err := parseFields(words, parseStrategy, flightSchedule)
if err != nil {
return nil, err
}
// 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
}
}
}
// Check if there are any waypoints after the parsed fields
if maxParsed+1 < len(words) {
flightSchedule.Waypoints = parseWaypoints(words[maxParsed+1:])
waypoints, err := parseWaypoints(words[maxParsed+1:])
if err != nil {
return nil, err
}
flightSchedule.Waypoints = waypoints
} else {
log.Warn("No waypoints found")
flightSchedule.Comments = "No waypoints found"
}
return flightSchedule
return flightSchedule, nil
}
func parseWaypoints(points []string) []domain.WayPoint {
// parseFields processes the fields based on the given strategy and updates the flight schedule.
func parseFields(words []string, parseStrategy []string, flightSchedule *domain.ScheduleLine) (map[string]bool, int, error) {
parsed := make(map[string]bool)
var maxParsed int
for i, word := range words {
for _, name := range parseStrategy {
if parsed[name] {
continue
}
if data := extract(word, parserMap[name]); data != nil {
updateFlightSchedule(flightSchedule, name, data)
parsed[name] = true
maxParsed = i
break
}
}
}
return parsed, maxParsed, nil
}
// updateFlightSchedule updates the flight schedule based on the parsed data.
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]
}
}
// parseWaypoints processes a slice of waypoint strings and returns a slice of WayPoint objects.
func parseWaypoints(points []string) ([]domain.WayPoint, error) {
log := utils.GetSugaredLogger()
if len(points) == 0 {
log.Warn("No waypoints found")
return nil
return nil, errors.New("no waypoints")
}
//find first waypoint
@@ -183,7 +347,7 @@ func parseWaypoints(points []string) []domain.WayPoint {
}
if len(realWaypoints) == 0 {
log.Warn("No waypoints found")
return nil
return nil, errors.New("no waypoints")
}
var waypoints []domain.WayPoint
for i, point := range realWaypoints {
@@ -202,7 +366,7 @@ func parseWaypoints(points []string) []domain.WayPoint {
}
}
// log.Debugf("Found %d waypoints", len(waypoints))
return waypoints
return waypoints, nil
}
/**