From 60ed218f0751ec7a5982da813b7a020893dfc7af Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 22 Apr 2024 10:58:00 +0100 Subject: [PATCH] Update CIF Conversion function to new ServiceDetail subdoc. --- cif/convert.go | 28 ++++++++++++++++++++++++---- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/cif/convert.go b/cif/convert.go index 5d8c87c..d929dcc 100644 --- a/cif/convert.go +++ b/cif/convert.go @@ -2,6 +2,7 @@ package cif import ( "strconv" + "strings" "git.fjla.uk/owlboard/go-types/pkg/database" "git.fjla.uk/owlboard/go-types/pkg/upstreamApi" @@ -14,7 +15,6 @@ func ConvertServiceType(input *upstreamApi.JsonScheduleV1, vstp bool) (*database output := database.Service{ TransactionType: input.TransactionType, StpIndicator: input.CifStpIndicator, - Vstp: vstp, // Simply uses the value passed into the function Operator: input.AtocCode, TrainUid: input.CifTrainUid, Headcode: input.ScheduleSegment.SignallingId, @@ -22,9 +22,7 @@ func ConvertServiceType(input *upstreamApi.JsonScheduleV1, vstp bool) (*database PlanSpeed: parseSpeed(&input.ScheduleSegment.CifSpeed), ScheduleStartDate: ParseCifDate(&input.ScheduleStartDate, "start"), ScheduleEndDate: ParseCifDate(&input.ScheduleEndDate, "end"), - FirstClass: hasFirstClass(&input.ScheduleSegment.CifTrainClass, &input.ScheduleSegment.SignallingId), - Catering: hasCatering(&input.ScheduleSegment.CifCateringCode), - Sleeper: hasSleeper(&input.ScheduleSegment.CifSleepers), + ServiceDetail: generateServiceDetail(&input.ScheduleSegment.CifTrainClass, &input.ScheduleSegment.SignallingId, &input.ScheduleSegment.CifCateringCode, &input.ScheduleSegment.CifSleepers, &input.ScheduleSegment.CifOperatingCharacteristics, vstp), DaysRun: parseDaysRun(&input.ScheduleDaysRun), Stops: parseStops(&input.ScheduleSegment.ScheduleLocation), } @@ -85,6 +83,22 @@ func isPublic(input *upstreamApi.CifScheduleLocation) bool { return true } +// Generates a ServiceDetail struct based on the input +func generateServiceDetail( + cifTrainClass, signallingId, + cateringCode, sleepers, + operatingCharacteristics *string, + vstp bool, +) database.ServiceDetail { + return database.ServiceDetail{ + FirstClass: hasFirstClass(cifTrainClass, signallingId), + Catering: hasCatering(cateringCode), + Sleeper: hasSleeper(sleepers), + Vstp: vstp, + Guard: hasGuard(operatingCharacteristics), + } +} + // Ascertains whether the service offers first class func hasFirstClass(input, signallingId *string) bool { if input == nil || signallingId == nil { @@ -115,3 +129,9 @@ func hasSleeper(input *string) bool { } return *input != "" } + +// Ascertains whether the service requires a guard +func hasGuard(opChars *string) bool { + str := strings.ToLower(*opChars) + return strings.Contains(str, "g") +} diff --git a/go.mod b/go.mod index 33b09ff..bbc7a2a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.fjla.uk/owlboard/timetable-mgr go 1.21 require ( - git.fjla.uk/owlboard/go-types v1.0.1 + git.fjla.uk/owlboard/go-types v1.1.0 github.com/go-stomp/stomp/v3 v3.1.0 go.mongodb.org/mongo-driver v1.15.0 go.uber.org/zap v1.27.0 diff --git a/go.sum b/go.sum index d21b178..55c6f3e 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ git.fjla.uk/owlboard/go-types v1.0.0 h1:yL/F97CzAnk9Ui4T9sC4k7Fc1H4mZgbesMRIwlYE git.fjla.uk/owlboard/go-types v1.0.0/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= git.fjla.uk/owlboard/go-types v1.0.1 h1:APZS0jQUjdaNPfWijToX6LLA+g4aI8ZYPXQVS5aN1eg= git.fjla.uk/owlboard/go-types v1.0.1/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= +git.fjla.uk/owlboard/go-types v1.1.0 h1:3o8My2O3KMOtSjXApYyI3VBS03PPdk+NGt7QonoFkl0= +git.fjla.uk/owlboard/go-types v1.1.0/go.mod h1:kG+BX9UF+yJaAVnln/QSKlTdrtKRRReezMeSk1ZLMzY= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=