timetable-mgr/pis/data.go

100 lines
2.2 KiB
Go

package pis
import (
"fmt"
"sort"
"strings"
"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"
"gopkg.in/yaml.v3"
)
// Process the YAML data to a struct
func processYaml(yamlStr string) (*[]database.PIS, error) {
// Define 'container' struct
var pisData struct {
Pis []PisData `yaml:"pis"`
}
// Unmarshal the YAML data into the structure
err := yaml.Unmarshal([]byte(yamlStr), &pisData)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal YAML: %v", err)
}
// Perform deduplication on the entire pis slice
err = deduplicateCodes(&pisData.Pis)
if err != nil {
return nil, err
}
documents, err := convertPisForDatabase(&pisData.Pis)
if err != nil {
return nil, err
}
return documents, 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
}
// Join slice elements to a string for comparison
func stopsToString(stops []string) string {
sort.Strings(stops)
return strings.Join(stops, ",")
}
// 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
}