Skip to content

Commit f8fbb07

Browse files
committed
Add go-style error handling
1 parent 2395774 commit f8fbb07

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

parser/cometCalendarParser.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ func ParseCometCalendar(inDir string, outDir string) {
152152
}
153153

154154
multiBuildingMap := make(map[string]map[string]map[string][]schema.Event)
155-
buildingAbbreviations, validAbbreviations := getLocationAbbreviations(inDir)
155+
// Some events have only the building name, not the abbreviation
156+
buildingAbbreviations, validAbbreviations, err := getLocationAbbreviations(inDir)
157+
if err != nil {
158+
panic(err)
159+
}
156160

157161
for _, event := range allEvents {
158162

@@ -248,7 +252,7 @@ func ParseCometCalendar(inDir string, outDir string) {
248252
}
249253

250254
// getAbbreviations dynamically retrieves the all of the locations abbreviations
251-
func getLocationAbbreviations(inDir string) (map[string]string, []string) {
255+
func getLocationAbbreviations(inDir string) (map[string]string, []string, error) {
252256
// Get the locations from the map scraper
253257
var mapFile []byte
254258

@@ -264,20 +268,21 @@ func getLocationAbbreviations(inDir string) (map[string]string, []string) {
264268
// If fail to get the locations again, it's not because location is unscraped
265269
mapFile, err = os.ReadFile(inDir + "/mapLocations.json")
266270
if err != nil {
267-
panic(err)
271+
return nil, nil, err
268272
}
269273
} else {
270-
panic(err)
274+
return nil, nil, err
271275
}
272276
}
277+
273278
var locations []schema.MapBuilding
274279
if err = json.Unmarshal(mapFile, &locations); err != nil {
275-
panic(err)
280+
return nil, nil, err
276281
}
277282

278283
// Process the abbreviations
279-
buildingsAbbreviations := make(map[string]string, 0)
280-
validAbbreviations := make([]string, 0)
284+
buildingsAbbreviations := make(map[string]string, 0) // Maps building names to their abbreviations
285+
validAbbreviations := make([]string, 0) // Valid building abreviations for checking
281286

282287
for _, location := range locations {
283288
// Trim the following acronym in the name
@@ -292,5 +297,5 @@ func getLocationAbbreviations(inDir string) (map[string]string, []string) {
292297
validAbbreviations = append(validAbbreviations, abbreviation)
293298
}
294299

295-
return buildingsAbbreviations, validAbbreviations
300+
return buildingsAbbreviations, validAbbreviations, nil
296301
}

scrapers/cometCalendar.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ func callAndUnmarshal(client *http.Client, page int, data *APICalendarResponse)
131131
// getTime parses the start and end time of the event
132132
func getTime(event RawEvent) (time.Time, time.Time) {
133133
instance := convert[map[string]any](
134-
convert[map[string]any](convert[[]any](event.Event["event_instances"])[0])["event_instance"])
134+
convert[map[string]any](
135+
convert[[]any](event.Event["event_instances"])[0])["event_instance"])
135136

136137
// Converts RFC3339 timestamp string to time.Time
137138
startTime, err := time.Parse(time.RFC3339, convert[string](instance["start"]))
@@ -163,28 +164,28 @@ func getEventLocation(event RawEvent) string {
163164

164165
// getFilters parses the types, topics, and target audiences
165166
func getFilters(event RawEvent) ([]string, []string, []string) {
166-
eventTypes := []string{}
167-
targetAudiences := []string{}
168-
eventTopics := []string{}
167+
types := []string{}
168+
audiences := []string{}
169+
topics := []string{}
169170

170171
filters := convert[map[string]any](event.Event["filters"])
171172

172173
rawTypes := convert[[]any](filters["event_types"])
173174
for _, rawType := range rawTypes {
174-
eventTypes = append(eventTypes, convert[string](convert[map[string]any](rawType)["name"]))
175+
types = append(types, convert[string](convert[map[string]any](rawType)["name"]))
175176
}
176177

177178
rawAudiences := convert[[]any](filters["event_target_audience"])
178179
for _, audience := range rawAudiences {
179-
targetAudiences = append(targetAudiences, convert[string](convert[map[string]any](audience)["name"]))
180+
audiences = append(audiences, convert[string](convert[map[string]any](audience)["name"]))
180181
}
181182

182183
rawTopics := convert[[]any](filters["event_topic"])
183184
for _, topic := range rawTopics {
184-
eventTopics = append(eventTopics, convert[string](convert[map[string]any](topic)["name"]))
185+
topics = append(topics, convert[string](convert[map[string]any](topic)["name"]))
185186
}
186187

187-
return eventTypes, targetAudiences, eventTopics
188+
return types, audiences, topics
188189
}
189190

190191
// getDepartmentsAndTags parses the departments, and tags

0 commit comments

Comments
 (0)