Skip to content

Commit 9069862

Browse files
committed
Refactor Category: from ADT to Text.
1 parent c33bd10 commit 9069862

7 files changed

Lines changed: 14 additions & 72 deletions

File tree

integration/test/Test/Apps.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ testCreateApp = do
103103
bindResponse (getApp owner2 tid2 appId) $ \resp -> resp.status `shouldMatchInt` 200
104104
bindResponse (getApp regularMember2 tid appId) $ \resp -> resp.status `shouldMatchInt` 200
105105

106-
-- Category must be any of the values for the Category enum
106+
-- Category can be any string, clients are responsible for sanitizing this.
107107
void $ bindResponse (createApp owner tid new {category = "notinenum"}) $ \resp -> do
108-
resp.status `shouldMatchInt` 400
108+
resp.status `shouldMatchInt` 200
109109

110110
let foundUserType :: (HasCallStack) => Value -> String -> [String] -> App ()
111111
foundUserType searcher exactMatchTerm aTypes =

libs/wire-api/src/Wire/API/User.hs

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ module Wire.API.User
5252
AppInfo (..),
5353
PutApp (..),
5454
Category (..),
55-
categoryTextMapping,
56-
categoryMap,
57-
categoryFromText,
58-
categoryToText,
5955
CreatedApp (..),
6056
RefreshAppCookieResponse (..),
6157

@@ -184,7 +180,6 @@ import Data.Default
184180
import Data.Domain (Domain (Domain))
185181
import Data.Either.Extra (maybeToEither)
186182
import Data.Handle (Handle)
187-
import Data.HashMap.Strict qualified as HM
188183
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
189184
import Data.Id
190185
import Data.Json.Util (UTCTimeMillis, (#))
@@ -2125,68 +2120,15 @@ data PutApp = PutApp
21252120
deriving (Arbitrary) via (GenericUniform PutApp)
21262121
deriving (A.FromJSON, A.ToJSON, S.ToSchema) via Schema PutApp
21272122

2128-
data Category
2129-
= Security
2130-
| Collaboration
2131-
| Productivity
2132-
| Automation
2133-
| Files
2134-
| AI
2135-
| Developer
2136-
| Support
2137-
| Finance
2138-
| HR
2139-
| Integration
2140-
| Compliance
2141-
| Other
2123+
newtype Category = Category {fromCategory :: Text}
21422124
deriving (Eq, Ord, Show, Read, Generic)
21432125
deriving (Arbitrary) via GenericUniform Category
21442126
deriving (A.FromJSON, A.ToJSON, S.ToSchema) via (Schema Category)
21452127

2146-
categoryTextMapping :: [(Text, Category)]
2147-
categoryTextMapping =
2148-
[ ("security", Security),
2149-
("collaboration", Collaboration),
2150-
("productivity", Productivity),
2151-
("automation", Automation),
2152-
("files", Files),
2153-
("ai", AI),
2154-
("developer", Developer),
2155-
("support", Support),
2156-
("finance", Finance),
2157-
("hr", HR),
2158-
("integration", Integration),
2159-
("compliance", Compliance),
2160-
("other", Other)
2161-
]
2162-
2163-
categoryMap :: HM.HashMap Text Category
2164-
categoryMap = HM.fromList categoryTextMapping
2165-
2166-
categoryFromText :: Text -> Maybe Category
2167-
categoryFromText text' = HM.lookup text' categoryMap
2168-
2169-
categoryToText :: Category -> Text
2170-
categoryToText = \case
2171-
Security -> "security"
2172-
Collaboration -> "collaboration"
2173-
Productivity -> "productivity"
2174-
Automation -> "automation"
2175-
Files -> "files"
2176-
AI -> "ai"
2177-
Developer -> "developer"
2178-
Support -> "support"
2179-
Finance -> "finance"
2180-
HR -> "hr"
2181-
Integration -> "integration"
2182-
Compliance -> "compliance"
2183-
Other -> "other"
2184-
21852128
instance ToSchema Category where
2186-
schema =
2187-
enum @Text "Category" $
2188-
mconcat $
2189-
map (uncurry element) categoryTextMapping
2129+
schema = over doc desc (Category <$> fromCategory .= schema @Text)
2130+
where
2131+
desc = S.description ?~ "Category name (if uncertain, pick \"other\")"
21902132

21912133
instance ToSchema NewApp where
21922134
schema =

libs/wire-api/test/golden/Test/Wire/API/Golden/Generated/UserProfile_user.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ testObject_UserProfile_user_2 =
8989
profileApp =
9090
Just $
9191
AppInfo
92-
{ category = Other,
92+
{ category = Category "other",
9393
description = unsafeRange "bloob"
9494
},
9595
profileSearchable = True

libs/wire-api/test/golden/Test/Wire/API/Golden/Manual/App.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ testObject_NewApp_1 =
3434
(either undefined id $ mkName "good name")
3535
mempty
3636
defaultAccentId
37-
Other
37+
(Category "other")
3838
(unsafeRange "good description")
3939
(plainTextPassword6Unsafe "good password")
4040

@@ -44,15 +44,15 @@ testObject_CreatedApp_1 =
4444

4545
testObject_AppInfo_1 :: AppInfo
4646
testObject_AppInfo_1 =
47-
AppInfo Other (unsafeRange "good description")
47+
AppInfo (Category "other") (unsafeRange "good description")
4848

4949
testObject_PutApp_1 :: PutApp
5050
testObject_PutApp_1 =
5151
PutApp
5252
(Just (either undefined id $ mkName "good name"))
5353
(Just mempty)
5454
(Just defaultAccentId)
55-
(Just Other)
55+
(Just (Category "other"))
5656
(Just (unsafeRange "good description"))
5757

5858
testObject_PutApp_2 :: PutApp

libs/wire-api/test/golden/Test/Wire/API/Golden/Manual/ListUsersById.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ profile2 =
7878
profileApp =
7979
Just $
8080
AppInfo
81-
{ category = Other,
81+
{ category = Category "other",
8282
description = unsafeRange "bloob"
8383
},
8484
profileSearchable = True

libs/wire-subsystems/src/Wire/AppStore.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ instance PostgresMarshall (UUID, UUID, Value, Text, Text, UUID) StoredApp where
5858
( postgresMarshall app.id,
5959
postgresMarshall app.teamId,
6060
postgresMarshall app.meta,
61-
postgresMarshall (categoryToText app.category),
61+
postgresMarshall (fromCategory app.category),
6262
postgresMarshall (fromRange app.description),
6363
postgresMarshall app.creator
6464
)
@@ -69,7 +69,7 @@ instance PostgresUnmarshall (UUID, UUID, Value, Text, Text, UUID) StoredApp wher
6969
<$> postgresUnmarshall uid
7070
<*> postgresUnmarshall teamId
7171
<*> postgresUnmarshall meta
72-
<*> (postgresUnmarshall =<< maybe (Left $ "Category " <> category <> " not found") Right (categoryFromText category))
72+
<*> postgresUnmarshall (Category category)
7373
<*> (maybe (Left "description out of bounds") Right . checked @0 @300 =<< postgresUnmarshall description)
7474
<*> postgresUnmarshall creator
7575

libs/wire-subsystems/src/Wire/AppStore/Postgres.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ updateAppImpl ::
108108
StoredAppUpdate ->
109109
Sem r (Either AppStoreError ())
110110
updateAppImpl (toUUID -> teamId) (toUUID -> appId) upd = do
111-
found <- case (User.categoryToText <$> upd.category, fromRange <$> upd.description) of
111+
found <- case (User.fromCategory <$> upd.category, fromRange <$> upd.description) of
112112
(Just cat, Just desc) ->
113113
runStatement (cat, desc, appId, teamId) $
114114
[maybeStatement|

0 commit comments

Comments
 (0)