aboutsummaryrefslogtreecommitdiffstats
path: root/src/Data/JLD.hs
blob: 40abae1237baede5610a9c0fb9c1a327d4e3754e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module Data.JLD (
    module Data.JLD.Mime,
    module Data.JLD.Error,
    module Data.JLD.Options,
    JLDExpansionParams (..),
    JLDExpansionState (..),
    JLDFlatteningParams (..),
    expand,
    flatten,
) where

import Data.JLD.Prelude

import Data.JLD.Control.Monad.RES (evalREST, runREST)
import Data.JLD.Error
import Data.JLD.Expansion (JLDEParams (..))
import Data.JLD.Expansion qualified as E (expand)
import Data.JLD.Expansion.Context (buildActiveContext)
import Data.JLD.Expansion.Global (JLDExpansionEnv (..), JLDExpansionState (..))
import Data.JLD.Flattening qualified as F (flatten)
import Data.JLD.Mime
import Data.JLD.Model.ActiveContext (ActiveContext (..), newActiveContext)
import Data.JLD.Model.Keyword (Keyword (..))
import Data.JLD.Model.URI (uriToIri)
import Data.JLD.Options
import Data.JLD.Util (flattenSingletonArray, valueToArray)

import Data.Aeson (Value (..))
import Data.Aeson.KeyMap qualified as KM (lookup, size)
import Text.URI (URI)

data JLDExpansionParams e m = JLDExpansionParams
    { jldExpansionParamsDocumentLoader :: DocumentLoader e m
    , jldExpansionParamsProcessingMode :: JLDVersion
    , jldExpansionParamsMaxRemoteContexts :: Int
    , jldExpansionParamsExpandContext :: Maybe Value
    , jldExpansionParamsFrameExpansion :: Bool
    , jldExpansionParamsState :: JLDExpansionState
    }
    deriving (Show)

expand :: Monad m => Value -> URI -> (JLDExpansionParams () m -> JLDExpansionParams e m) -> m (Either (JLDError e) Value, JLDExpansionState)
expand document baseUrl paramsFn = do
    let JLDExpansionParams{..} =
            paramsFn
                JLDExpansionParams
                    { jldExpansionParamsDocumentLoader = DocumentLoader <. const <. pure <| Left ()
                    , jldExpansionParamsProcessingMode = JLD1_1
                    , jldExpansionParamsMaxRemoteContexts = 20
                    , jldExpansionParamsExpandContext = Nothing
                    , jldExpansionParamsFrameExpansion = False
                    , jldExpansionParamsState =
                        JLDExpansionState
                            { jldExpansionStateContextCache = mempty
                            , jldExpansionStateDocumentCache = mempty
                            }
                    }

        env =
            JLDExpansionEnv
                { jldExpansionEnvDocumentLoader = jldExpansionParamsDocumentLoader
                , jldExpansionEnvProcessingMode = jldExpansionParamsProcessingMode
                , jldExpansionEnvMaxRemoteContexts = jldExpansionParamsMaxRemoteContexts
                }

        activeContext = newActiveContext \ac -> ac{activeContextBaseUrl = Just baseUrl, activeContextBaseIri = uriToIri baseUrl}
        expansionParams p = p{jldeParamsFrameExpansion = jldExpansionParamsFrameExpansion}

    -- 6.
    let maybeExpandContext =
            jldExpansionParamsExpandContext <&> flattenSingletonArray .> \case
                Array expandedContext -> Array expandedContext
                (Object expandedContext) | Just ctx <- KM.lookup (show KeywordContext) expandedContext -> ctx
                expandedContext -> Array <| pure expandedContext

    activeContext' <- case maybeExpandContext of
        Just expandContext ->
            buildActiveContext activeContext expandContext (Just baseUrl) id
                |> evalREST env jldExpansionParamsState
                |> fmap (fromRight activeContext)
        Nothing -> pure activeContext

    -- 8.
    (result, state') <-
        E.expand activeContext' document baseUrl expansionParams
            |> runREST env jldExpansionParamsState

    let result' = case result of
            -- 8.1.
            Right (Object expanded)
                | KM.size expanded == 1
                , Just expanded' <- KM.lookup (show KeywordGraph) expanded ->
                    Right <. Array <| valueToArray expanded'
            -- 8.2.
            Right Null -> Right <| Array mempty
            -- 8.3.
            Right expanded -> Right <. Array <| valueToArray expanded
            --
            Left err -> Left err

    pure (result', state')

data JLDFlatteningParams e m = JLDFlatteningParams
    { jldFlatteningParamsDocumentLoader :: DocumentLoader e m
    , jldFlatteningParamsProcessingMode :: JLDVersion
    , jldFlatteningParamsMaxRemoteContexts :: Int
    , jldFlatteningParamsExpandContext :: Maybe Value
    , jldFlatteningParamsFrameExpansion :: Bool
    , jldFlatteningParamsState :: JLDExpansionState
    , jldFlatteningParamsCompactArrays :: Bool
    }
    deriving (Show)

flatten :: Monad m => Value -> URI -> (JLDFlatteningParams () m -> JLDFlatteningParams e m) -> m (Either (JLDError e) Value, JLDExpansionState)
flatten document baseUrl paramsFn = do
    let JLDFlatteningParams{..} =
            paramsFn
                JLDFlatteningParams
                    { jldFlatteningParamsDocumentLoader = DocumentLoader <. const <. pure <| Left ()
                    , jldFlatteningParamsProcessingMode = JLD1_1
                    , jldFlatteningParamsMaxRemoteContexts = 20
                    , jldFlatteningParamsExpandContext = Nothing
                    , jldFlatteningParamsFrameExpansion = False
                    , jldFlatteningParamsState =
                        JLDExpansionState
                            { jldExpansionStateContextCache = mempty
                            , jldExpansionStateDocumentCache = mempty
                            }
                    , jldFlatteningParamsCompactArrays = True
                    }
        expansionParams =
            JLDExpansionParams
                { jldExpansionParamsDocumentLoader = jldFlatteningParamsDocumentLoader
                , jldExpansionParamsProcessingMode = jldFlatteningParamsProcessingMode
                , jldExpansionParamsMaxRemoteContexts = jldFlatteningParamsMaxRemoteContexts
                , jldExpansionParamsExpandContext = jldFlatteningParamsExpandContext
                , jldExpansionParamsFrameExpansion = jldFlatteningParamsFrameExpansion
                , jldExpansionParamsState = jldFlatteningParamsState
                }
    (result, state') <- expand document baseUrl (const expansionParams)
    case result of
        Left err -> pure (Left err, state')
        Right expanded -> fmap (,state') <. runExceptT <| F.flatten expanded