Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions parser/cometCalendarParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ func ParseCometCalendar(inDir string, outDir string) {

var allEvents []schema.Event

// Unmarshal decodes the scraped JSON data in calendarFile and passes it to
// a slice (list) called allEvents by address
err = json.Unmarshal(calendarFile, &allEvents)
if err != nil {
panic(err)
}

multiBuildingMap := make(map[string]map[string]map[string][]schema.Event)

// Some events have only the building name, not the abbreviation
buildingAbbreviations, validAbbreviations, err := getLocationAbbreviations(inDir)
if err != nil {
Expand Down Expand Up @@ -104,6 +107,7 @@ func ParseCometCalendar(inDir string, outDir string) {
room = "Other"
}

//
if _, exists := multiBuildingMap[date]; !exists {
multiBuildingMap[date] = make(map[string]map[string][]schema.Event)
}
Expand All @@ -115,8 +119,13 @@ func ParseCometCalendar(inDir string, outDir string) {
multiBuildingMap[date][building][room] = append(multiBuildingMap[date][building][room], event)
}

// Nested slices (date -> building -> room -> events) using schema.
var result []schema.MultiBuildingEvents[schema.Event]

// NOTE: In Go, any iterating through a map is done in a random order.
// This applies to the nested for loop below
// If you require any sorting of this data, put the keys in a slice, sort the slice
// and loop over that instead.
for date, buildings := range multiBuildingMap {
var singleBuildings []schema.SingleBuildingEvents[schema.Event]
for building, rooms := range buildings {
Expand All @@ -140,6 +149,33 @@ func ParseCometCalendar(inDir string, outDir string) {
})
}

// sort result (a nested array of slices) using slices.Sortfunc
slices.SortFunc(result, func(a, b schema.MultiBuildingEvents[schema.Event]) int {
// sorts by date, ascending
return strings.Compare(a.Date, b.Date)
})

for i := range result {
slices.SortFunc(result[i].Buildings, func(a, b schema.SingleBuildingEvents[schema.Event]) int {
// sorts alphabetically
return strings.Compare(a.Building, b.Building)
})

for j := range result[i].Buildings {
slices.SortFunc(result[i].Buildings[j].Rooms, func(c, d schema.RoomEvents[schema.Event]) int {
// sorts alphabetically
return strings.Compare(c.Room, d.Room)
})

for k := range result[i].Buildings[j].Rooms {
slices.SortFunc(result[i].Buildings[j].Rooms[k].Events, func(e, f schema.Event) int {
// sorts schema.Event.Id as a Hex rather than a String
return strings.Compare(e.Id.Hex(), f.Id.Hex())
})
}
}
}

log.Print("Parsed Comet Calendar!")

utils.WriteJSON(fmt.Sprintf("%s/cometCalendar.json", outDir), result)
Expand Down
34 changes: 34 additions & 0 deletions parser/mazevoParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"slices"
"strings"

"github.com/UTDNebula/api-tools/utils"
Expand Down Expand Up @@ -44,6 +45,7 @@ func ParseMazevo(inDir string, outDir string) {
panic(err)
}

// Unmarshal decodes the scraped JSON data
var rawData SourceData
err = json.Unmarshal(fileBytes, &rawData)
if err != nil {
Expand Down Expand Up @@ -93,8 +95,13 @@ func ParseMazevo(inDir string, outDir string) {
}
}

// Nested slices (date -> building -> room -> events) using schema.
var result []schema.MultiBuildingEvents[schema.MazevoEvent]

// NOTE: In Go, any iterating through a map is done in a random order.
// This applies to the nested for loop below
// If you require any sorting of this data, put the keys in a slice, sort the slice
// and loop over that instead.
for date, buildings := range multiBuildingMap {
var buildingList []schema.SingleBuildingEvents[schema.MazevoEvent]
for building, rooms := range buildings {
Expand All @@ -116,6 +123,33 @@ func ParseMazevo(inDir string, outDir string) {
})
}

// sort result (a nested array of slices) using slices.Sortfunc
slices.SortFunc(result, func(a, b schema.MultiBuildingEvents[schema.MazevoEvent]) int {
// sorts by date, ascending
return strings.Compare(a.Date, b.Date)
})

for i := range result {
slices.SortFunc(result[i].Buildings, func(a, b schema.SingleBuildingEvents[schema.MazevoEvent]) int {
// sorts alphabetically
return strings.Compare(a.Building, b.Building)
})

for j := range result[i].Buildings {
slices.SortFunc(result[i].Buildings[j].Rooms, func(c, d schema.RoomEvents[schema.MazevoEvent]) int {
// sorts alphabetically
return strings.Compare(c.Room, d.Room)
})

for k := range result[i].Buildings[j].Rooms {
slices.SortFunc(result[i].Buildings[j].Rooms[k].Events, func(e, f schema.MazevoEvent) int {
// sorts alphabetically
return strings.Compare(*e.EventName, *f.EventName)
})
}
}
}

log.Print("Parsed Mazevo!")

err = utils.WriteJSON(fmt.Sprintf("%s/mazevo.json", outDir), result)
Expand Down
Loading