timetable-mgr/pis/data.go

53 lines
1.0 KiB
Go
Raw Normal View History

2024-10-21 21:04:37 +01:00
package pis
import (
"fmt"
2024-10-22 20:50:08 +01:00
"sort"
"strings"
2024-10-21 21:04:37 +01:00
"gopkg.in/yaml.v3"
)
// Process the YAML data to a struct
2024-10-22 20:50:08 +01:00
func processYaml(yamlStr string) (*[]PisData, error) {
var pis []PisData
2024-10-21 21:04:37 +01:00
2024-10-22 20:50:08 +01:00
// Unmarshal the YAML data into a slice of PisData
2024-10-21 21:04:37 +01:00
err := yaml.Unmarshal([]byte(yamlStr), &pis)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal YAML: %v", err)
}
2024-10-22 20:50:08 +01:00
// Perform deduplication on the entire pis slice
2024-10-21 21:04:37 +01:00
err = deduplicateCodes(&pis)
if err != nil {
return nil, err
}
return &pis, nil
}
// Deduplicate data in place and return error if failed
2024-10-22 20:50:08 +01:00
func deduplicateCodes(pis *[]PisData) error {
uniqueStops := make(map[string]bool)
var dedupedPis []PisData
for _, data := range *pis {
stopsKey := stopsToString(data.Stops)
// If stopsKey does not exist, add to map
if _, exists := uniqueStops[stopsKey]; !exists {
uniqueStops[stopsKey] = true
dedupedPis = append(dedupedPis, data)
}
}
*pis = dedupedPis
return nil
}
func stopsToString(stops []string) string {
sort.Strings(stops)
return strings.Join(stops, ",")
2024-10-21 21:04:37 +01:00
}