From 5ee737f3f822d90fe865311f519f68176a048682 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 20 Jul 2023 11:07:39 +0100 Subject: [PATCH] Further work on the unmarshaling process --- src/go.mod | 2 +- src/go.sum | 2 ++ src/vstp/handler.go | 10 +++++----- src/vstp/parser.go | 32 ++++++++++++++++++++++++-------- src/vstp/subscribe.go | 1 + 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/go.mod b/src/go.mod index ea3d762..4dcd78e 100644 --- a/src/go.mod +++ b/src/go.mod @@ -3,7 +3,7 @@ module git.fjla.uk/owlboard/mq-client go 1.19 require ( - git.fjla.uk/owlboard/go-types v0.0.0-20230719192727-845a41f8572b + git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc github.com/go-stomp/stomp/v3 v3.0.5 go.mongodb.org/mongo-driver v1.12.0 go.uber.org/zap v1.24.0 diff --git a/src/go.sum b/src/go.sum index 85e3e6e..d9289ce 100644 --- a/src/go.sum +++ b/src/go.sum @@ -10,6 +10,8 @@ git.fjla.uk/owlboard/go-types v0.0.0-20230719185315-e492e8ad6ae7 h1:g+ovnwGpx24K git.fjla.uk/owlboard/go-types v0.0.0-20230719185315-e492e8ad6ae7/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= git.fjla.uk/owlboard/go-types v0.0.0-20230719192727-845a41f8572b h1:QKHrP3kjvypPO/YVrHdTNV68w6I7MNYyEy5zcD2Jk/A= git.fjla.uk/owlboard/go-types v0.0.0-20230719192727-845a41f8572b/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= +git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc h1:7s+svu/FOU/qwLLThtwvOV2CqID8sZmVyd15UldgBR8= +git.fjla.uk/owlboard/go-types v0.0.0-20230720095555-ad24002a6acc/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/src/vstp/handler.go b/src/vstp/handler.go index 209be37..b2960c3 100644 --- a/src/vstp/handler.go +++ b/src/vstp/handler.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "time" - "git.fjla.uk/owlboard/go-types/pkg/database" + //"git.fjla.uk/owlboard/go-types/pkg/database" "git.fjla.uk/owlboard/mq-client/log" "github.com/go-stomp/stomp/v3" ) @@ -16,13 +16,13 @@ var count uint64 = 0 func handle(msg *stomp.Message) { count++ log.Msg.Info("Message count: " + fmt.Sprint(count)) - schedule := unmarshalData(msg.Body) - saveToFile(schedule) + schedule := unmarshalData(string(msg.Body)) + saveToFile(schedule, "transformed") } -func saveToFile(msg database.Service) { +func saveToFile(msg any, suffix string) { timestamp := time.Now().Format("2006-01-02T15:04:05") - path := fmt.Sprintf("message-logs/%s-msg.json", timestamp) + path := fmt.Sprintf("message-logs/%s-%s.json", timestamp, suffix) prettyJSON, err := json.MarshalIndent(msg, "", " ") if err != nil { diff --git a/src/vstp/parser.go b/src/vstp/parser.go index 80c6ea9..e7fe2a0 100644 --- a/src/vstp/parser.go +++ b/src/vstp/parser.go @@ -2,6 +2,7 @@ package vstp import ( "encoding/json" + "fmt" "strconv" "time" @@ -12,14 +13,22 @@ import ( ) // Unmarshals the JSON data and runs it through the formatData() function and returns the data in a DB ready Struct -func unmarshalData(jsonData []byte) database.Service { - var schedule upstreamApi.Schedule - err := json.Unmarshal(jsonData, &schedule) +func unmarshalData(jsonData string) database.Service { + log.Msg.Debug("Unmarshalling message body") + fmt.Println(jsonData) + log.Msg.Debug("Converting to byte array") + jsonDataBytes := []byte(jsonData) + fmt.Println(jsonDataBytes) + var schedule upstreamApi.VstpMsg + err := json.Unmarshal(jsonDataBytes, &schedule) if err != nil { log.Msg.Error("Unable to unmarshal message body: " + err.Error()) //return err } - return formatData(schedule) + log.Msg.Debug("Unmarshalling Complete") + fmt.Println(schedule) + saveToFile(schedule, "unmarshalled") + return formatData(schedule.CIFMsg) } // Transforms the upstreamApi.Schedule type into a database.Service type @@ -98,10 +107,10 @@ func parseStops(inputStops []upstreamApi.ScheduleLocation) []database.Stop { for _, loc := range inputStops { stop := database.Stop{ - PublicDeparture: &loc.PublicDepartureTime, - WttDeparture: &loc.ScheduledDepartureTime, - PublicArrival: &loc.PublicArrivalTime, - WttArrival: &loc.ScheduledArrivalTime, + PublicDeparture: getStringPointer(loc.PublicDepartureTime), + WttDeparture: getStringPointer(loc.ScheduledDepartureTime), + PublicArrival: getStringPointer(loc.PublicArrivalTime), + WttArrival: getStringPointer(loc.ScheduledArrivalTime), IsPublic: loc.PublicDepartureTime != "" || loc.PublicArrivalTime != "", Tiploc: loc.Location.Tiploc.TiplocID, } @@ -111,3 +120,10 @@ func parseStops(inputStops []upstreamApi.ScheduleLocation) []database.Stop { return stops } + +func getStringPointer(s string) *string { + if s == "" { + return nil + } + return &s +} diff --git a/src/vstp/subscribe.go b/src/vstp/subscribe.go index 2d4e729..e53fcbd 100644 --- a/src/vstp/subscribe.go +++ b/src/vstp/subscribe.go @@ -22,6 +22,7 @@ func Subscribe() { log.Msg.Error("STOMP Message Error: " + msg.Err.Error()) } else { log.Msg.Debug("STOMP Message Received") + saveToFile(string(msg.Body), "msgBody") handle(msg) } }