aboutsummaryrefslogtreecommitdiffstats
path: root/src/Data/JLD/Model
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/JLD/Model')
-rw-r--r--src/Data/JLD/Model/NodeMap.hs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/Data/JLD/Model/NodeMap.hs b/src/Data/JLD/Model/NodeMap.hs
new file mode 100644
index 0000000..48db17e
--- /dev/null
+++ b/src/Data/JLD/Model/NodeMap.hs
@@ -0,0 +1,45 @@
1module Data.JLD.Model.NodeMap (NodeMap, lookup, lookup2, lookup3, insert, modifyArray, hasKey2, hasKey3, memberArray) where
2
3import Data.JLD.Prelude hiding (modify)
4
5import Data.Aeson (Array, Value (..))
6import Data.JLD.Util (valueToArray)
7import Data.Map.Strict qualified as M (alter, insert, lookup, member)
8
9type PropertyKey = Maybe Text
10type PropertyMap = Map PropertyKey Value
11
12type SubjectKey = Maybe Text
13type SubjectMap = Map SubjectKey PropertyMap
14
15type GraphKey = Text
16type NodeMap = Map GraphKey SubjectMap
17
18lookup :: GraphKey -> NodeMap -> Maybe SubjectMap
19lookup = M.lookup
20
21lookup2 :: GraphKey -> SubjectKey -> NodeMap -> Maybe PropertyMap
22lookup2 graphName subject nodeMap = M.lookup graphName nodeMap >>= M.lookup subject
23
24lookup3 :: GraphKey -> SubjectKey -> PropertyKey -> NodeMap -> Maybe Value
25lookup3 graphName subject property nodeMap =
26 M.lookup graphName nodeMap >>= M.lookup subject >>= M.lookup property
27
28modifyArray :: GraphKey -> SubjectKey -> PropertyKey -> (Array -> Array) -> NodeMap -> NodeMap
29modifyArray graphName subject property fn =
30 M.alter (Just <. M.alter (Just <. M.alter (Just <. Array <. fn <. maybe mempty valueToArray) property <. fromMaybe mempty) subject <. fromMaybe mempty) graphName
31
32insert :: GraphKey -> SubjectKey -> PropertyKey -> Value -> NodeMap -> NodeMap
33insert graphName subject property value =
34 M.alter (Just <. M.alter (Just <. M.insert property value <. fromMaybe mempty) subject <. fromMaybe mempty) graphName
35
36hasKey2 :: GraphKey -> SubjectKey -> NodeMap -> Bool
37hasKey2 graphName subject nodeMap = maybe False (M.member subject) <| M.lookup graphName nodeMap
38
39hasKey3 :: GraphKey -> SubjectKey -> PropertyKey -> NodeMap -> Bool
40hasKey3 graphName subject property nodeMap = maybe False (M.member property) <| M.lookup subject =<< M.lookup graphName nodeMap
41
42memberArray :: GraphKey -> SubjectKey -> PropertyKey -> Value -> NodeMap -> Bool
43memberArray graphName subject property value nodeMap = case lookup3 graphName subject property nodeMap of
44 Just (Array a) -> value `elem` a
45 _ -> False