53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package pis
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Process the YAML data to a struct
|
|
func processYaml(yamlStr string) (*[]PisData, error) {
|
|
var pis []PisData
|
|
|
|
// Unmarshal the YAML data into a slice of PisData
|
|
err := yaml.Unmarshal([]byte(yamlStr), &pis)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal YAML: %v", err)
|
|
}
|
|
|
|
// Perform deduplication on the entire pis slice
|
|
err = deduplicateCodes(&pis)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &pis, nil
|
|
}
|
|
|
|
// Deduplicate data in place and return error if failed
|
|
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, ",")
|
|
}
|