package cif import ( "bytes" "encoding/json" "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" "git.fjla.uk/owlboard/timetable-mgr/log" "go.uber.org/zap" ) // Unmarshalls data into the correct types for processing func parseCifData(data []byte) (*parsedData, error) { // Split the data into lines lines := bytes.Split(data, []byte("\n")) // Initialise variable for the parsed data var parsed parsedData parsed.assoc = make([]upstreamApi.JsonAssociationV1, 0) parsed.sched = make([]upstreamApi.JsonScheduleV1, 0) for _, line := range lines { // Skip empty lines to avoid logging errors when there is no error if len(bytes.TrimSpace(line)) == 0 { continue } // Map each line for processing var obj map[string]json.RawMessage if err := json.Unmarshal(line, &obj); err != nil { log.Msg.Error("Error decoding line", zap.String("line", string(line)), zap.Error(err)) continue } // Loop through the mapped data and unmarshal to the correct type for key, value := range obj { switch key { case "JsonTimetableV1": var timetable upstreamApi.JsonTimetableV1 if err := json.Unmarshal(value, &timetable); err != nil { log.Msg.Error("Unable to parse JSON Timetable", zap.Error(err), zap.String("line", string(value))) continue } parsed.header = timetable case "JsonAssociationV1": var association upstreamApi.JsonAssociationV1 if err := json.Unmarshal(value, &association); err != nil { log.Msg.Error("Error decoding JSON Association", zap.Error(err)) continue } parsed.assoc = append(parsed.assoc, association) case "JsonScheduleV1": var schedule upstreamApi.JsonScheduleV1 if err := json.Unmarshal(value, &schedule); err != nil { log.Msg.Error("Error decoding JSON Schedule", zap.Error(err)) continue } parsed.sched = append(parsed.sched, schedule) } } } return &parsed, nil }