Skip to content

Commit 8d287d6

Browse files
authored
Merge pull request #5576 from unisonweb/cp/json-caching
2 parents f1fbd23 + 5df30e1 commit 8d287d6

File tree

3 files changed

+104
-9
lines changed

3 files changed

+104
-9
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
module Unison.Server.Doc where
1010

1111
import Control.Monad
12-
import Data.Aeson (ToJSON)
12+
import Data.Aeson (ToJSON, FromJSON)
1313
import Data.Foldable
1414
import Data.Functor
1515
import Data.Map qualified as Map
@@ -91,21 +91,21 @@ data DocG specialForm
9191
| Column [(DocG specialForm)]
9292
| Group (DocG specialForm)
9393
deriving stock (Eq, Ord, Show, Generic, Functor, Foldable, Traversable)
94-
deriving anyclass (ToJSON)
94+
deriving anyclass (ToJSON, FromJSON)
9595

9696
deriving instance (ToSchema specialForm) => ToSchema (DocG specialForm)
9797

9898
type UnisonHash = Text
9999

100100
data Ref a = Term a | Type a
101101
deriving stock (Eq, Ord, Show, Generic, Functor, Foldable, Traversable)
102-
deriving anyclass (ToJSON)
102+
deriving anyclass (ToJSON, FromJSON)
103103

104104
instance (ToSchema a) => ToSchema (Ref a)
105105

106106
data MediaSource = MediaSource {mediaSourceUrl :: Text, mediaSourceMimeType :: Maybe Text}
107107
deriving stock (Eq, Ord, Show, Generic)
108-
deriving anyclass (ToJSON, ToSchema)
108+
deriving anyclass (ToJSON, FromJSON, ToSchema)
109109

110110
data RenderedSpecialForm
111111
= Source [SrcRefs]
@@ -125,7 +125,7 @@ data RenderedSpecialForm
125125
| Svg Text
126126
| RenderError (RenderError SyntaxText)
127127
deriving stock (Eq, Ord, Show, Generic)
128-
deriving anyclass (ToJSON, ToSchema)
128+
deriving anyclass (ToJSON, FromJSON, ToSchema)
129129

130130
data EvaluatedSpecialForm v
131131
= ESource [(EvaluatedSrc v)]
@@ -151,7 +151,7 @@ data EvaluatedSpecialForm v
151151
-- `Src folded unfolded`
152152
data Src = Src SyntaxText SyntaxText
153153
deriving stock (Eq, Ord, Show, Generic)
154-
deriving anyclass (ToJSON, ToSchema)
154+
deriving anyclass (ToJSON, FromJSON, ToSchema)
155155

156156
-- | Evaluate the doc, then render it.
157157
evalAndRenderDoc ::
@@ -448,7 +448,7 @@ evalDoc terms typeOf eval types tm =
448448
data RenderError trm
449449
= InvalidTerm trm
450450
deriving stock (Eq, Ord, Show, Generic)
451-
deriving anyclass (ToJSON)
451+
deriving anyclass (ToJSON, FromJSON)
452452

453453
deriving anyclass instance (ToSchema trm) => ToSchema (RenderError trm)
454454

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,21 @@ instance ToCapture (Capture "namespace" Path.Path) where
329329
"E.g. base.List"
330330

331331
instance ToJSON Path.Path where
332-
toJSON p = Aeson.String (tShow p)
332+
toJSON p = Aeson.String (Path.toText p)
333+
334+
instance FromJSON Path.Path where
335+
parseJSON = Aeson.withText "Path" \txt -> case Path.parsePath (Text.unpack txt) of
336+
Left s -> fail (Text.unpack s)
337+
Right p -> pure p
333338

334339
instance ToJSON Path.Absolute where
335-
toJSON p = Aeson.String (tShow p)
340+
toJSON p = Aeson.String (Path.absToText p)
341+
342+
instance FromJSON Path.Absolute where
343+
parseJSON = Aeson.withText "Path" \txt -> case Path.parsePath' (Text.unpack txt) of
344+
Left s -> fail (Text.unpack s)
345+
Right (Path.AbsolutePath' p) -> pure p
346+
Right (Path.RelativePath' _) -> fail "Expected an absolute path but received a relative path."
336347

337348
instance ToSchema Path.Path where
338349
declareNamedSchema _ = declareNamedSchema (Proxy @Text)
@@ -428,6 +439,8 @@ instance ToSchema ProjectName
428439

429440
deriving via Text instance ToJSON ProjectName
430441

442+
deriving via Text instance FromJSON ProjectName
443+
431444
deriving via Text instance Sqlite.FromField ProjectBranchName
432445

433446
instance FromHttpApiData ProjectBranchName where
@@ -449,6 +462,8 @@ instance ToCapture (Capture "branch-name" ProjectBranchName) where
449462

450463
deriving via Text instance ToJSON ProjectBranchName
451464

465+
deriving via Text instance FromJSON ProjectBranchName
466+
452467
-- CBOR encodings
453468

454469
deriving via Text instance Serialise Hash32

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ instance ToJSON NamespaceDetails where
9898
"readme" .= readme
9999
]
100100

101+
instance FromJSON NamespaceDetails where
102+
parseJSON = Aeson.withObject "NamespaceDetails" \obj -> do
103+
fqn <- obj .: "fqn"
104+
hash <- obj .: "hash"
105+
readme <- obj .: "readme"
106+
pure $ NamespaceDetails {..}
107+
101108
deriving instance ToSchema NamespaceDetails
102109

103110
-- | A hash qualified name, unlike HashQualified, the hash is required
@@ -168,6 +175,15 @@ instance ToJSON TypeDefinition where
168175
"typeDocs" .= typeDocs
169176
]
170177

178+
instance FromJSON TypeDefinition where
179+
parseJSON = Aeson.withObject "TypeDefinition" \obj -> do
180+
typeNames <- obj .: "typeNames"
181+
bestTypeName <- obj .: "bestTypeName"
182+
defnTypeTag <- obj .: "defnTypeTag"
183+
typeDefinition <- obj .: "typeDefinition"
184+
typeDocs <- obj .: "typeDocs"
185+
pure $ TypeDefinition {..}
186+
171187
deriving instance ToSchema TypeDefinition
172188

173189
instance ToJSON TermDefinition where
@@ -181,6 +197,16 @@ instance ToJSON TermDefinition where
181197
"termDocs" .= termDocs
182198
]
183199

200+
instance FromJSON TermDefinition where
201+
parseJSON = Aeson.withObject "TermDefinition" \obj -> do
202+
termNames <- obj .: "termNames"
203+
bestTermName <- obj .: "bestTermName"
204+
defnTermTag <- obj .: "defnTermTag"
205+
termDefinition <- obj .: "termDefinition"
206+
signature <- obj .: "signature"
207+
termDocs <- obj .: "termDocs"
208+
pure $ TermDefinition {..}
209+
184210
deriving instance ToSchema TermDefinition
185211

186212
instance ToJSON DefinitionDisplayResults where
@@ -191,6 +217,13 @@ instance ToJSON DefinitionDisplayResults where
191217
"missingDefinitions" .= missingDefinitions
192218
]
193219

220+
instance FromJSON DefinitionDisplayResults where
221+
parseJSON = Aeson.withObject "DefinitionDisplayResults" \obj -> do
222+
termDefinitions <- obj .: "termDefinitions"
223+
typeDefinitions <- obj .: "typeDefinitions"
224+
missingDefinitions <- obj .: "missingDefinitions"
225+
pure $ DefinitionDisplayResults {..}
226+
194227
deriving instance ToSchema DefinitionDisplayResults
195228

196229
data TermDefinitionDiff = TermDefinitionDiff
@@ -296,6 +329,25 @@ instance ToJSON SemanticSyntaxDiff where
296329
"toAnnotation" .= toAnnotation
297330
]
298331

332+
instance FromJSON SemanticSyntaxDiff where
333+
parseJSON = Aeson.withObject "SemanticSyntaxDiff" \obj -> do
334+
diffTag :: Text <- obj .: "diffTag"
335+
case diffTag of
336+
"old" -> Old <$> obj .: "elements"
337+
"new" -> New <$> obj .: "elements"
338+
"both" -> Both <$> obj .: "elements"
339+
"segmentChange" -> do
340+
fromSegment <- obj .: "fromSegment"
341+
toSegment <- obj .: "toSegment"
342+
annotation <- obj .: "annotation"
343+
pure $ SegmentChange (fromSegment, toSegment) annotation
344+
"annotationChange" -> do
345+
segment <- obj .: "segment"
346+
fromAnnotation <- obj .: "fromAnnotation"
347+
toAnnotation <- obj .: "toAnnotation"
348+
pure $ AnnotationChange segment (fromAnnotation, toAnnotation)
349+
_ -> fail "Invalid diffTag"
350+
299351
-- | A diff of the syntax of a term or type
300352
--
301353
-- It doesn't make sense to diff builtins with ABTs, so in that case we just provide the
@@ -504,6 +556,20 @@ instance ToJSON TermDiffResponse where
504556
"newTerm" .= newTerm
505557
]
506558

559+
instance FromJSON TermDiffResponse where
560+
parseJSON = Aeson.withObject "TermDiffResponse" \obj -> do
561+
diff <- DisplayObjectDiff <$> obj .: "diff"
562+
diffKind :: Text <- obj .: "diffKind"
563+
project <- obj .: "project"
564+
oldBranch <- obj .: "oldBranchRef"
565+
newBranch <- obj .: "newBranchRef"
566+
oldTerm <- obj .: "oldTerm"
567+
newTerm <- obj .: "newTerm"
568+
case diffKind of
569+
"diff" -> pure $ TermDiffResponse {..}
570+
"mismatched" -> pure $ TermDiffResponse {..}
571+
_ -> fail "Invalid diffKind"
572+
507573
data TypeDiffResponse = TypeDiffResponse
508574
{ project :: ProjectName,
509575
oldBranch :: ProjectBranchName,
@@ -542,5 +608,19 @@ instance ToJSON TypeDiffResponse where
542608
"newType" .= newType
543609
]
544610

611+
instance FromJSON TypeDiffResponse where
612+
parseJSON = Aeson.withObject "TypeDiffResponse" \obj -> do
613+
diff <- DisplayObjectDiff <$> obj .: "diff"
614+
diffKind :: Text <- obj .: "diffKind"
615+
project <- obj .: "project"
616+
oldBranch <- obj .: "oldBranchRef"
617+
newBranch <- obj .: "newBranchRef"
618+
oldType <- obj .: "oldType"
619+
newType <- obj .: "newType"
620+
case diffKind of
621+
"diff" -> pure $ TypeDiffResponse {..}
622+
"mismatched" -> pure $ TypeDiffResponse {..}
623+
_ -> fail "Invalid diffKind"
624+
545625
-- | Servant utility for a query param that's required, providing a useful error message if it's missing.
546626
type RequiredQueryParam = Servant.QueryParam' '[Servant.Required, Servant.Strict]

0 commit comments

Comments
 (0)