package cif import ( "encoding/json" "errors" "io" "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" "git.fjla.uk/owlboard/timetable-mgr/log" "go.uber.org/zap" ) // Accepts the CIF data as a stream and outputs parsed data func parseCifDataStream(dataStream io.ReadCloser) (*parsedData, error) { defer dataStream.Close() log.Msg.Debug("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() { var obj map[string]json.RawMessage if err := decoder.Decode(&obj); err != nil { log.Msg.Error("Error decoding JSON String") return nil, err } // Handle parsed data 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)) continue } parsed.header = timetable case "TiplocV1": // This data is not used and is sourced from CORPUS continue case "JsonAssociationV1": // Association data is not currently used // but may be used in the future continue case "JsonScheduleV1": var schedule upstreamApi.JsonScheduleV1 if err := json.Unmarshal(value, &schedule); err != nil { log.Msg.Error("Error decoding JSONScheduleV1 object", zap.Error(err)) continue } parsed.sched = append(parsed.sched, schedule) case "EOF": log.Msg.Info("Reached EOF") default: log.Msg.Warn("Unknown CIF Data type", zap.String("key", key)) } } } log.Msg.Debug("CIF Parsing completed") return &parsed, nil }