Skip to content

Commit 4fb8ec4

Browse files
committed
More FromJSON instances
1 parent b9c74e2 commit 4fb8ec4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

unison-share-api/src/Unison/Server/Orphans.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,20 @@ instance ToJSON Name where
196196
toEncoding = toEncoding . Name.toText
197197
toJSON = toJSON . Name.toText
198198

199+
instance FromJSON Name where
200+
parseJSON = Aeson.withText "Name" \txt -> case Name.parseTextEither txt of
201+
Left err -> fail $ "Invalid Name: " <> Text.unpack err
202+
Right name -> pure name
203+
199204
instance ToJSONKey Name where
200205
toJSONKey = contramap Name.toText (toJSONKey @Text)
201206

207+
instance FromJSONKey Name where
208+
fromJSONKey =
209+
Aeson.FromJSONKeyTextParser \txt -> case Name.parseTextEither txt of
210+
Left err -> fail $ "Invalid Name: " <> Text.unpack err
211+
Right name -> pure name
212+
202213
instance ToSchema Name where
203214
declareNamedSchema _ = declareNamedSchema (Proxy @Text)
204215

@@ -208,6 +219,11 @@ instance ToJSON NameSegment where
208219
instance ToJSONKey NameSegment where
209220
toJSONKey = contramap NameSegment.toEscapedText (toJSONKey @Text)
210221

222+
instance FromJSON NameSegment where
223+
parseJSON = Aeson.withText "NameSegment" \txt -> case NameSegment.parseText txt of
224+
Left err -> fail $ "Invalid NameSegment: " <> Text.unpack err
225+
Right ns -> pure ns
226+
211227
deriving anyclass instance ToParamSchema ShortCausalHash
212228

213229
instance ToParamSchema ShortHash where
@@ -268,6 +284,12 @@ instance ToJSON ConstructorType where
268284
CT.Data -> String "Data"
269285
CT.Effect -> String "Effect"
270286

287+
instance FromJSON ConstructorType where
288+
parseJSON = Aeson.withText "ConstructorType" \txt -> case txt of
289+
"Data" -> pure CT.Data
290+
"Effect" -> pure CT.Effect
291+
_ -> fail $ "Invalid ConstructorType: " <> Text.unpack txt
292+
271293
instance FromHttpApiData Path.Relative where
272294
parseUrlPiece txt = case Path.parsePath' (Text.unpack txt) of
273295
Left s -> Left s
@@ -298,6 +320,9 @@ instance FromHttpApiData Path.Path where
298320
Right (Path.RelativePath' p) -> Right (Path.unrelative p)
299321
Right (Path.AbsolutePath' _) -> Left $ "Expected relative path, but " <> txt <> " was absolute."
300322

323+
instance ToHttpApiData Path.Path where
324+
toUrlPiece = tShow
325+
301326
instance ToCapture (Capture "hash" ShortHash) where
302327
toCapture _ =
303328
DocCapture

0 commit comments

Comments
 (0)