timetable-mgr/cif/parse.go

67 lines
1.8 KiB
Go
Raw Normal View History

2024-04-03 22:25:27 +01:00
package cif
import (
"encoding/json"
"errors"
"io"
2024-04-03 22:25:27 +01:00
"git.fjla.uk/owlboard/go-types/pkg/upstreamApi"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
2024-04-10 22:59:13 +01:00
// Accepts the CIF data as a stream and outputs parsed data
func parseCifDataStream(dataStream io.ReadCloser) (*parsedData, error) {
log.Msg.Debug("STREAM-Starting CIF Datastream parsing")
if dataStream == nil {
return nil, errors.New("unable to parse nil pointer")
}
var parsed parsedData
parsed.assoc = make([]upstreamApi.JsonAssociationV1, 0)
parsed.sched = make([]upstreamApi.JsonScheduleV1, 0)
// Create JSON Decoder
decoder := json.NewDecoder(dataStream)
// Iterate over JSON Objects using stream decoder
for decoder.More() {
2024-04-03 22:25:27 +01:00
var obj map[string]json.RawMessage
if err := decoder.Decode(&obj); err != nil {
log.Msg.Error("Error decoding JSON String")
return nil, err
2024-04-03 22:25:27 +01:00
}
// Handle parsed data
2024-04-03 22:25:27 +01:00
for key, value := range obj {
switch key {
case "JsonTimetableV1":
var timetable upstreamApi.JsonTimetableV1
if err := json.Unmarshal(value, &timetable); err != nil {
log.Msg.Error("Error decoding JSONTimetableV1 object", zap.Error(err))
2024-04-03 22:25:27 +01:00
continue
}
parsed.header = timetable
2024-04-10 23:05:19 +01:00
case "TiplocV1":
// This data is not used and is sourced from CORPUS
continue
2024-04-03 22:25:27 +01:00
case "JsonAssociationV1":
// Association data is not currently used
// but may be used in the future
continue
2024-04-03 22:25:27 +01:00
case "JsonScheduleV1":
var schedule upstreamApi.JsonScheduleV1
if err := json.Unmarshal(value, &schedule); err != nil {
log.Msg.Error("Error decoding JSONScheduleV1 object", zap.Error(err))
2024-04-03 22:25:27 +01:00
continue
}
parsed.sched = append(parsed.sched, schedule)
2024-04-10 23:05:19 +01:00
default:
log.Msg.Warn("Unknown CIF Data type", zap.String("key", key))
2024-04-03 22:25:27 +01:00
}
}
}
log.Msg.Debug("CIF Parsing completed")
2024-04-03 22:25:27 +01:00
return &parsed, nil
}