timetable-mgr/pis/data.go

100 lines
2.2 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
2024-10-23 11:42:47 +01:00
"git.fjla.uk/owlboard/go-types/pkg/database"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
2024-10-21 21:04:37 +01:00
"gopkg.in/yaml.v3"
)
// Process the YAML data to a struct
2024-10-23 11:42:47 +01:00
func processYaml(yamlStr string) (*[]database.PIS, error) {
2024-10-24 20:39:24 +01:00
// Define 'container' struct
var pisData struct {
Pis []PisData `yaml:"pis"`
}
2024-10-21 21:04:37 +01:00
2024-10-24 20:39:24 +01:00
// Unmarshal the YAML data into the structure
err := yaml.Unmarshal([]byte(yamlStr), &pisData)
2024-10-21 21:04:37 +01:00
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-24 20:39:24 +01:00
err = deduplicateCodes(&pisData.Pis)
2024-10-21 21:04:37 +01:00
if err != nil {
return nil, err
}
2024-10-24 20:39:24 +01:00
documents, err := convertPisForDatabase(&pisData.Pis)
if err != nil {
return nil, err
}
2024-10-23 11:42:47 +01:00
return documents, nil
2024-10-21 21:04:37 +01:00
}
// 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
}
2024-10-23 11:42:47 +01:00
// Join slice elements to a string for comparison
2024-10-22 20:50:08 +01:00
func stopsToString(stops []string) string {
sort.Strings(stops)
return strings.Join(stops, ",")
2024-10-21 21:04:37 +01:00
}
2024-10-23 11:42:47 +01:00
// Convert PisData to database.PIS
func convertPisForDatabase(in *[]PisData) (*[]database.PIS, error) {
var out []database.PIS
for _, code := range *in {
var document database.PIS
document.Code = code.Code
document.Operator = code.Operator
document.Stops = code.Stops
document.Tiplocs = GetTiplocsFromCrs(code.Stops)
out = append(out, document)
}
return &out, nil
}
// Return a list of TIPLOCS from a list of CRS using Database Lookups
func GetTiplocsFromCrs(crsList []string) (TiplocList []string) {
for _, crs := range crsList {
alpha := strings.ToUpper(crs)
tiploc, err := dbAccess.GetTiplocFromCrs(alpha)
if err != nil {
log.Warn("Unable to find matching TIPLOC for 3ALPHA", zap.String("3ALPHA", alpha), zap.Error(err))
return
}
TiplocList = append(TiplocList, strings.ToUpper(tiploc))
}
return
}