Skip to content

Commit 9fc1641

Browse files
⅄ trunk → 25-05-22-synhash
2 parents 8ebfdeb + dbeea4d commit 9fc1641

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

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

Lines changed: 37 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
@@ -391,12 +416,18 @@ instance FromHttpApiData (HQ.HashQualified Name) where
391416
& HQ.parseText
392417
& maybe (Left "Invalid Hash Qualified Name. Expected one of the following forms: name@hash, name, @hash") Right
393418

419+
instance ToHttpApiData (HQ.HashQualified Name) where
420+
toQueryParam = HQ.toTextWith Name.toText
421+
394422
instance FromHttpApiData (HQ'.HashQualified Name) where
395423
parseQueryParam txt =
396424
Text.replace "@" "#" txt
397425
& HQ'.parseText
398426
& maybe (Left "Invalid Hash Qualified Name. Expected one of the following forms: name@hash, name") Right
399427

428+
instance ToHttpApiData (HQ'.HashQualified Name) where
429+
toQueryParam = HQ'.toTextWith Name.toText
430+
400431
instance ToParamSchema (HQ.HashQualified n) where
401432
toParamSchema _ =
402433
mempty
@@ -423,6 +454,9 @@ deriving via Text instance Sqlite.FromField ProjectName
423454
instance FromHttpApiData ProjectName where
424455
parseQueryParam = mapLeft tShow . tryInto @ProjectName
425456

457+
instance ToHttpApiData ProjectName where
458+
toQueryParam name = into @Text name
459+
426460
instance ToParamSchema ProjectName where
427461
toParamSchema _ =
428462
mempty
@@ -446,6 +480,9 @@ deriving via Text instance Sqlite.FromField ProjectBranchName
446480
instance FromHttpApiData ProjectBranchName where
447481
parseQueryParam = mapLeft tShow . tryInto @ProjectBranchName
448482

483+
instance ToHttpApiData ProjectBranchName where
484+
toQueryParam name = into @Text name
485+
449486
instance ToSchema ProjectBranchName
450487

451488
instance ToParamSchema ProjectBranchName where

0 commit comments

Comments
 (0)