Skip to content

Refactor JSON parsing to replace data types with helper functions - #1789

Open
AngelsandDevsLOL wants to merge 1 commit into
Courseography:masterfrom
AngelsandDevsLOL:refactor-timetable-json-parsing
Open

Refactor JSON parsing to replace data types with helper functions#1789
AngelsandDevsLOL wants to merge 1 commit into
Courseography:masterfrom
AngelsandDevsLOL:refactor-timetable-json-parsing

Conversation

@AngelsandDevsLOL

@AngelsandDevsLOL AngelsandDevsLOL commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Proposed Changes

This PR refactors app/WebParsing/UtsgJsonParser.hs to remove the DB and DBList datatypes in favour of two helper functions, parseMeetingInfo and parseCourse.

The DB and DBList datatypes exist to carry FromJSON instances for parsing the timetable API response. They aren't used anywhere else in the codebase and are constructed at a single call site only to be unwrapped again immediately by a helper function. Replacing them with helper functions gives the same behaviour and makes the parsing logic more readable.

Type of Change

(Write an X or a brief description next to the type or types that best describe your changes.)

Type Applies?
🚨 Breaking change (fix or feature that would cause existing functionality to change)
New feature (non-breaking change that adds functionality)
🐛 Bug fix (non-breaking change that fixes an issue)
🎨 User interface change (change to user interface; provide screenshots)
♻️ Refactoring (internal change to codebase, without changing functionality) X
🚦 Test update (change that only adds or modifies tests)
📦 Dependency update (change that updates a dependency)
🔧 Internal (change that only affects developers or continuous integration)

Checklist

(Complete each of the following items for your pull request. Indicate that you have completed an item by changing the [ ] into a [x] in the raw text, or by clicking on the checkbox in the rendered description on GitHub.)

Before opening your pull request:

  • I have performed a self-review of my changes.
    • Check that all changed files included in this pull request are intentional changes.
    • Check that all changes are relevant to the purpose of this pull request, as described above.
  • I have added tests for my changes, if applicable.
    • This is required for all bug fixes and new features.
  • I have updated the project documentation, if applicable.
    • This is required for new features.
  • If this is my first contribution, I have added myself to the list of contributors.
  • I have updated the project Changelog (this is required for all changes).

After opening your pull request:

  • I have verified that the CircleCI checks have passed.
  • I have requested a review from a project maintainer.

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 0

Coverage remained the same at 58.192%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4249
Covered Lines: 2526
Line Coverage: 59.45%
Relevant Branches: 994
Covered Branches: 525
Branch Coverage: 52.82%
Branches in Coverage %: Yes
Coverage Strength: 156.1 hits per line

💛 - Coveralls

@david-yz-liu david-yz-liu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AngelsandDevsLOL nice work! I left a few minor comments, but please also add new tests for the insertCourses function (this file currently isn't being tested!).

-- | Helper function to flatten the list of DB Objects
flattenDBList :: DBList -> [MeetTime]
flattenDBList (DBList meetings) = concatMap (\(DB meetTimes) -> meetTimes) meetings
forM_ (parseMeetingInfo respBody) (mapM_ insertMeeting)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since parseMeetingInfo returns a Maybe [MeetTime], you can simplify to use just a bit of pattern-matching here and display an error message when the parsing fails. (You also don't need the do, which is for multi-step monadic computations.)

case parseMeetingInfo respBody of
  Nothing -> -- print an error message
  Just meetTimes -> ...

json <- decode respBody
flip parseMaybe json $ \obj -> do
maybePayload <- obj .:? "payload"
case maybePayload of

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need a case here. Instead use .: above, and if it fails then Nothing will be returned, which I think is okay.

dbList <- mapM parseJSON courses
return $ DBList dbList
Nothing -> return $ DBList []
courses :: [Value] <- pageableCourse .: "courses"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this type annotation necessary? I would think that GHC can infer the type [Value] based on how courses is being used below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary, because concat is defined as concat :: Foldable f => f [a] -> [a], where courses should be [Value], but the [] is not inferred. The same thing happens with mapM, where it takes mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b), where courses should match t a but doesn't know what t (in this case []) is.

If it isn't included, then courses gets type Any Value, and GHC tells us that courses should be courses :: t0 Value, but t0 is ambiguous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants