package stations import ( "bytes" "encoding/xml" "fmt" "git.fjla.uk/owlboard/go-types/pkg/database" "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" "git.fjla.uk/owlboard/timetable-mgr/dbAccess" ) // Parses n number of XML byte arrays func parseData(data ...[]byte) ([]database.Station, error) { var stations []upstreamApi.Station for _, d := range data { parsedStations, err := parseXML(d) if err != nil { return nil, err } stations = append(stations, parsedStations...) } var output []database.Station for _, s := range stations { outputStation, err := convertApiToDatabase(s) if err != nil { return nil, err } output = append(output, outputStation) } return output, nil } // Parses XML and converts to struct func parseXML(data []byte) ([]upstreamApi.Station, error) { var stationList upstreamApi.StationList reader := bytes.NewReader(data) decoder := xml.NewDecoder(reader) err := decoder.Decode(&stationList) if err != nil { return nil, fmt.Errorf("error parsing XML: %v", err) } return stationList.Stations, nil } // Convert API type to Database type ready for insertion func convertApiToDatabase(data upstreamApi.Station) (database.Station, error) { if data.CrsCode == "" { return database.Station{}, fmt.Errorf("CRS code is required but missing") } tiploc, err := dbAccess.GetTiplocFromCrs(data.CrsCode) if err != nil { return database.Station{}, err } stanox, err := dbAccess.GetStanoxFromCrs(data.CrsCode) if err != nil { return database.Station{}, err } output := database.Station{ CRS: data.CrsCode, TIPLOC: tiploc, STANOX: stanox, NLCDESC: data.Name, Location: database.GeoJson{ Type: "Point", Coordinates: []float64{ data.Longitude, data.Latitude, }, }, Operator: data.StationOperator, } return output, nil }