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