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) { 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 } documents, err := convertPisForDatabase(&pis) 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 }