]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Corpus/Types.hs
[node] fix pubmed JSON serialization
[gargantext.git] / src / Gargantext / API / Node / Corpus / Types.hs
1 {-# LANGUAGE TemplateHaskell #-}
2
3 module Gargantext.API.Node.Corpus.Types where
4
5 import Control.Lens hiding (elements, Empty)
6 import Control.Monad.Fail (fail)
7 import Data.Aeson
8 import Data.Aeson.TH (deriveJSON)
9 import Data.Monoid (mempty)
10 import Data.Swagger
11 import GHC.Generics (Generic)
12 import Test.QuickCheck
13 import qualified Data.Text as T
14
15 import Gargantext.Prelude
16
17 import qualified Gargantext.API.Admin.Orchestrator.Types as Types
18 import Gargantext.Core.Utils.Prefix (unPrefix)
19 import Gargantext.Database.Action.Flow (DataOrigin(..))
20
21 data Database = Empty
22 | PubMed
23 | Arxiv
24 | HAL
25 | IsTex
26 | Isidore
27 deriving (Eq, Show, Generic, Enum, Bounded)
28
29 instance Arbitrary Database where
30 arbitrary = arbitraryBoundedEnum
31
32 deriveJSON (unPrefix "") ''Database
33 instance ToSchema Database where
34 declareNamedSchema = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions
35
36 database2origin :: Database -> DataOrigin
37 database2origin Empty = InternalOrigin Types.IsTex
38 database2origin PubMed = ExternalOrigin Types.PubMed
39 database2origin Arxiv = ExternalOrigin Types.Arxiv
40 database2origin HAL = ExternalOrigin Types.HAL
41 database2origin IsTex = ExternalOrigin Types.IsTex
42 database2origin Isidore = ExternalOrigin Types.Isidore
43
44 ------------------------------------------------------------------------
45 data Datafield = Gargantext
46 | External Database
47 | Web
48 | Files
49 deriving (Eq, Show, Generic)
50
51 instance FromJSON Datafield where
52 parseJSON (String "Gargantext") = pure Gargantext
53 parseJSON (String "Web") = pure Web
54 parseJSON (String "Files") = pure Files
55 parseJSON (Object o) = do
56 db <- o .: "External"
57 pure $ External db
58 parseJSON x = withText "Datafield" (\text ->
59 fail $ "Cannot match pattern '<db>' for string " <> T.unpack text) x
60
61 instance ToJSON Datafield where
62 toJSON (External db) = toJSON $ object [ ("External", toJSON db) ]
63 toJSON s = toJSON $ show s
64
65 instance Arbitrary Datafield where
66 arbitrary = oneof [pure Gargantext, pure Web, pure Files, External <$> arbitrary]
67
68 instance ToSchema Datafield where
69 declareNamedSchema _ = do
70 return $ NamedSchema (Just "Datafield") $ mempty
71 & type_ ?~ SwaggerObject