diff --git a/parser/cometCalendarParser.go b/parser/cometCalendarParser.go index 70ca0ec..e691d96 100644 --- a/parser/cometCalendarParser.go +++ b/parser/cometCalendarParser.go @@ -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 { @@ -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) } @@ -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 { @@ -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) diff --git a/parser/mazevoParser.go b/parser/mazevoParser.go index 06b2646..52a1403 100644 --- a/parser/mazevoParser.go +++ b/parser/mazevoParser.go @@ -6,6 +6,7 @@ import ( "log" "os" "path/filepath" + "slices" "strings" "github.com/UTDNebula/api-tools/utils" @@ -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 { @@ -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 { @@ -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)