From 43eff5f1d037b3e45a3b7a274048527e2a95103d Mon Sep 17 00:00:00 2001 From: William Carroll Date: Fri, 31 Jul 2020 11:27:47 +0100 Subject: Prefer RecordWildCards for FromJSON instances Stylistically, I think this looks cleaner. --- src/Types.hs | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) (limited to 'src/Types.hs') diff --git a/src/Types.hs b/src/Types.hs index 273d4aecca70..485111f38bac 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -250,13 +250,10 @@ tripPKFields (TripPK{ tripPKUsername instance FromJSON TripPK where parseJSON = withObject "TripPK" $ \x -> do - username <- x .: "username" - destination <- x .: "destination" - startDate <- x .: "startDate" - pure TripPK{ tripPKUsername = username - , tripPKDestination = destination - , tripPKStartDate = startDate - } + tripPKUsername <- x .: "username" + tripPKDestination <- x .: "destination" + tripPKStartDate <- x .: "startDate" + pure TripPK{..} -- | Return the tuple representation of a Trip record for SQL. tripFields :: Trip -> (Username, Destination, Date, Date, Comment) @@ -284,17 +281,12 @@ instance ToJSON Trip where instance FromJSON Trip where parseJSON = withObject "Trip" $ \x -> do - username <- x .: "username" - destination <- x .: "destination" - startDate <- x .: "startDate" - endDate <- x .: "endDate" - comment <- x .: "comment" - pure Trip{ tripUsername = username - , tripDestination = destination - , tripStartDate = startDate - , tripEndDate = endDate - , tripComment = comment - } + tripUsername <- x .: "username" + tripDestination <- x .: "destination" + tripStartDate <- x .: "startDate" + tripEndDate <- x .: "endDate" + tripComment <- x .: "comment" + pure Trip{..} -- | Users and Accounts both refer to the same underlying entities; however, -- Users model the user-facing Account details, hiding sensitive details like @@ -328,11 +320,9 @@ data AccountCredentials = AccountCredentials instance FromJSON AccountCredentials where parseJSON = withObject "AccountCredentials" $ \x -> do - username <- x.: "username" - password <- x.: "password" - pure AccountCredentials{ accountCredentialsUsername = username - , accountCredentialsPassword = password - } + accountCredentialsUsername <- x.: "username" + accountCredentialsPassword <- x.: "password" + pure AccountCredentials{..} -- | Hash password `x`. @@ -355,11 +345,11 @@ data CreateAccountRequest = CreateAccountRequest instance FromJSON CreateAccountRequest where parseJSON = withObject "CreateAccountRequest" $ \x -> do - username <- x .: "username" - password <- x .: "password" - email <- x .: "email" - role <- x .: "role" - pure $ CreateAccountRequest username password email role + createAccountRequestUsername <- x .: "username" + createAccountRequestPassword <- x .: "password" + createAccountRequestEmail <- x .: "email" + createAccountRequestRole <- x .: "role" + pure $ CreateAccountRequest{..} createAccountRequestFields :: CreateAccountRequest -> (Username, ClearTextPassword, Email, Role) createAccountRequestFields request = -- cgit 1.4.1