timetable-extension #1

Open
fred.boniface wants to merge 160 commits from timetable-extension into main
4 changed files with 24 additions and 21 deletions
Showing only changes of commit 3883031e04 - Show all commits

View File

@ -10,10 +10,13 @@ import (
)
// Accepts CORPUS data as a byte array and formats it ready for database insertion
func parseCorpusData(jsonData *[]byte) ([]database.CorpusEntry, error) {
log.Msg.Info("Unmarshalling CORPUS Data")
func parseCorpusData(jsonData *[]byte) (*[]database.CorpusEntry, error) {
log.Msg.Debug("Starting CORPUS Data parsing")
// Initialise data structure
var dataMap map[string]interface{}
// Create JSON
err := json.Unmarshal(*jsonData, &dataMap)
if err != nil {
log.Msg.Error("Unable to unmarshal CORPUS data", zap.Error(err))
@ -51,11 +54,11 @@ func parseCorpusData(jsonData *[]byte) ([]database.CorpusEntry, error) {
corpusEntries = append(corpusEntries, corpusEntry)
}
return corpusEntries, nil
return &corpusEntries, nil
}
// Removes empty fields from CORPUS entries
func pruneCorpusEntries(corpusEntries []database.CorpusEntry) []database.CorpusEntry {
func pruneCorpusEntries(corpusEntries []database.CorpusEntry) *[]database.CorpusEntry {
for i := range corpusEntries {
if corpusEntries[i].CRS == " " {
corpusEntries[i].CRS = ""
@ -74,5 +77,5 @@ func pruneCorpusEntries(corpusEntries []database.CorpusEntry) []database.CorpusE
}
}
return corpusEntries
return &corpusEntries
}

View File

@ -3,10 +3,10 @@ package corpus
import "git.fjla.uk/owlboard/go-types/pkg/database"
// Removes non-station entities from the CORPUS Data, ready for insertion to the database (stations collection)
func createStationEntries(corpusData []database.CorpusEntry) []database.StationEntry {
func createStationEntries(corpusData *[]database.CorpusEntry) *[]database.StationEntry {
var stationEntries []database.StationEntry
for _, entry := range corpusData {
for _, entry := range *corpusData {
if entry.CRS != "" {
stationEntry := database.StationEntry{
CRS: entry.CRS,
@ -19,5 +19,5 @@ func createStationEntries(corpusData []database.CorpusEntry) []database.StationE
}
}
return stationEntries
return &stationEntries
}

View File

@ -22,7 +22,7 @@ func RunCorpusUpdate(cfg *helpers.Configuration) error {
return err
}
corpusData := pruneCorpusEntries(unsortedCorpusData)
corpusData := pruneCorpusEntries(*unsortedCorpusData)
stationData := createStationEntries(corpusData)
if err := dbAccess.DropCollection(dbAccess.CorpusCollection); err != nil {

View File

@ -7,12 +7,12 @@ import (
)
// Puts an array of Corpus Documents into the database
func PutManyCorpus(corpusData []database.CorpusEntry) error {
func PutManyCorpus(corpusData *[]database.CorpusEntry) error {
collection := MongoClient.Database(databaseName).Collection(CorpusCollection)
documents := convertCorpusToInterfaceSlice(corpusData)
_, err := collection.InsertMany(context.Background(), documents)
_, err := collection.InsertMany(context.Background(), *documents)
if err != nil {
return err
}
@ -22,12 +22,12 @@ func PutManyCorpus(corpusData []database.CorpusEntry) error {
}
// Puts an array of Stations documents into the database
func PutManyStations(stationsData []database.StationEntry) error {
func PutManyStations(stationsData *[]database.StationEntry) error {
collection := MongoClient.Database(databaseName).Collection(StationsCollection)
documents := convertStationsToInterfaceSlice(stationsData)
_, err := collection.InsertMany(context.Background(), documents)
_, err := collection.InsertMany(context.Background(), *documents)
if err != nil {
return err
}
@ -37,19 +37,19 @@ func PutManyStations(stationsData []database.StationEntry) error {
}
// Converts []database.CorpusEntry types into interface slices required to put them into the database
func convertCorpusToInterfaceSlice(corpusData []database.CorpusEntry) []interface{} {
interfaceSlice := make([]interface{}, len(corpusData))
for i, doc := range corpusData {
func convertCorpusToInterfaceSlice(corpusData *[]database.CorpusEntry) *[]interface{} {
interfaceSlice := make([]interface{}, len(*corpusData))
for i, doc := range *corpusData {
interfaceSlice[i] = doc
}
return interfaceSlice
return &interfaceSlice
}
// Converts []database.StationEntry types into interface slices required to put them into the database
func convertStationsToInterfaceSlice(stationsData []database.StationEntry) []interface{} {
interfaceSlice := make([]interface{}, len(stationsData))
for i, doc := range stationsData {
func convertStationsToInterfaceSlice(stationsData *[]database.StationEntry) *[]interface{} {
interfaceSlice := make([]interface{}, len(*stationsData))
for i, doc := range *stationsData {
interfaceSlice[i] = doc
}
return interfaceSlice
return &interfaceSlice
}