@@ -15,13 +15,11 @@ import Data.HashMap.Strict (HashMap)
15
15
import Data.HashMap.Strict qualified as HM
16
16
import Data.Hashable
17
17
import Data.Maybe hiding (mapMaybe )
18
+ import Data.Sequence
18
19
import Data.Text (Text , intercalate )
19
20
import Data.Typeable (Typeable )
20
- import Deque.Strict
21
- import DequePatterns
22
- import DequeUtils
23
- import GHC.Exts (fromList )
24
21
import GHC.Generics (Generic )
22
+ import SequenceUtils
25
23
import Prelude hiding (any , foldl , foldl' , head )
26
24
27
25
data JsonPathElement
@@ -33,63 +31,63 @@ instance Hashable JsonPathElement
33
31
34
32
instance NFData JsonPathElement
35
33
36
- type JsonPath = Deque JsonPathElement
34
+ type JsonPath = Seq JsonPathElement
37
35
38
36
data JsonSchemaTree
39
- = PathNode JsonPathElement (Deque JsonSchemaTree )
37
+ = PathNode JsonPathElement (Seq JsonSchemaTree )
40
38
| PathEnd
41
39
deriving (Eq , Show , Typeable )
42
40
43
- type JsonSchema = Deque JsonSchemaTree
41
+ type JsonSchema = Seq JsonSchemaTree
44
42
45
43
hasSameRoot :: JsonPath -> JsonSchemaTree -> Bool
46
44
hasSameRoot path tree = case (tree, path) of
47
- (PathNode el _, h :| | _) | el == h -> True
45
+ (PathNode el _, h :< | _) | el == h -> True
48
46
otherwise -> False
49
47
50
48
toSchemaTree :: JsonPath -> JsonSchemaTree
51
49
toSchemaTree =
52
50
\ case
53
- D_ -> PathEnd
54
- root :| | path -> PathNode root $ pure $ toSchemaTree path
51
+ Empty -> PathEnd
52
+ root :< | path -> PathNode root $ pure $ toSchemaTree path
55
53
56
54
(#+) :: JsonSchema -> JsonPath -> JsonSchema
57
55
(#+) schema path =
58
56
let append path tree = case (tree, path) of
59
- (t, D_ ) -> t
60
- (PathNode el D_ , h :| | tail )
57
+ (t, Empty ) -> t
58
+ (PathNode el Empty , h :< | tail )
61
59
| el == h ->
62
60
PathNode el $ pure $ toSchemaTree tail
63
- (PathNode el branches, h :| | tail )
61
+ (PathNode el branches, h :< | tail )
64
62
| el == h ->
65
63
PathNode el $ uniq $ branches #+ tail
66
64
(PathEnd , path) -> toSchemaTree path
67
65
(t, _) -> t
68
66
in if any (hasSameRoot path) schema
69
67
then append path <$> schema
70
- else toSchemaTree path `snoc` schema
68
+ else schema |> toSchemaTree path
71
69
72
- toSchema :: Deque JsonPath -> JsonSchema
70
+ toSchema :: Seq JsonPath -> JsonSchema
73
71
toSchema = foldl' (#+) empty
74
72
75
73
data JsonValueTree
76
- = ValueRoot JsonPathElement (Deque JsonValueTree )
74
+ = ValueRoot JsonPathElement (Seq JsonValueTree )
77
75
| SingleValue JsonPathElement Value
78
- | ValueArray (Deque Value )
79
- | TreeArray (Deque ( Deque JsonValueTree ))
76
+ | ValueArray (Seq Value )
77
+ | TreeArray (Seq ( Seq JsonValueTree ))
80
78
deriving (Eq , Show , Typeable )
81
79
82
- type JsonTree = Deque JsonValueTree
80
+ type JsonTree = Seq JsonValueTree
83
81
84
82
extract :: JsonSchema -> Value -> JsonTree
85
83
extract schema value =
86
84
let extractTree v schemaTree =
87
85
case schemaTree of
88
86
PathEnd -> Nothing
89
- (PathNode el (PathEnd :|| D_ )) ->
87
+ (PathNode el (PathEnd :<| Empty )) ->
90
88
case el of
91
89
Key k -> SingleValue el <$> v ^? (key $ fromText k)
92
- Iterator -> ValueArray <$> (maybeNeq $ fromList $ v ^.. values)
90
+ Iterator -> ValueArray <$> (maybeNes $ fromList $ v ^.. values)
93
91
(PathNode el@ (Key k) children) ->
94
92
let keyValue = (v ^? (key $ fromText k))
95
93
childrenExtractors = flip extractTree <$> children
@@ -99,20 +97,20 @@ extract schema value =
99
97
let nodeValues = fromList $ v ^.. values
100
98
childrenExtractors = flip extractTree <$> children
101
99
nodeTrees = (\ val -> mapMaybe id $ ($ val) <$> childrenExtractors) <$> nodeValues
102
- in TreeArray <$> maybeNeq nodeTrees
100
+ in TreeArray <$> maybeNes nodeTrees
103
101
in mapMaybe id $ (extractTree value <$> schema)
104
102
105
- genMaps :: Bool -> JsonPath -> JsonValueTree -> Deque (HashMap Text Value )
103
+ genMaps :: Bool -> JsonPath -> JsonValueTree -> Seq (HashMap Text Value )
106
104
genMaps flat jp jvt =
107
105
case (flat, jvt) of
108
- (_, ValueRoot jpe trees) -> xfold $ genMaps flat (jpe `snoc` jp ) <$> trees
109
- (_, SingleValue jpe value) -> pure $ HM. singleton (jsonPathText $ jpe `snoc` jp ) value
110
- (False , ValueArray values) -> HM. singleton (jsonPathText (Iterator `snoc` jp )) <$> values
106
+ (_, ValueRoot jpe trees) -> xfold $ genMaps flat (jp |> jpe ) <$> trees
107
+ (_, SingleValue jpe value) -> pure $ HM. singleton (jsonPathText $ jp |> jpe ) value
108
+ (False , ValueArray values) -> HM. singleton (jsonPathText (jp |> Iterator )) <$> values
111
109
(True , ValueArray values) -> HM. singleton (jsonPathText jp) <$> values
112
- (False , TreeArray trees) -> trees >>= (xfold . (genMaps flat (Iterator `snoc` jp ) <$> ))
110
+ (False , TreeArray trees) -> trees >>= (xfold . (genMaps flat (jp |> Iterator ) <$> ))
113
111
(True , TreeArray trees) -> trees >>= (xfold . (genMaps flat jp <$> ))
114
112
115
- generateTuples :: Bool -> JsonTree -> Deque (HashMap Text Value )
113
+ generateTuples :: Bool -> JsonTree -> Seq (HashMap Text Value )
116
114
generateTuples flat jTree = xfold $ genMaps flat empty <$> jTree
117
115
118
116
jsonPathText :: JsonPath -> Text
@@ -131,13 +129,13 @@ dropIterators jpath =
131
129
Key k -> Just $ Key k
132
130
Iterator -> Nothing
133
131
134
- xseq :: (a -> a -> a ) -> Deque a -> Deque a -> Deque a
135
- xseq _ va D_ = va
136
- xseq _ D_ vb = vb
132
+ xseq :: (a -> a -> a ) -> Seq a -> Seq a -> Seq a
133
+ xseq _ va Empty = va
134
+ xseq _ Empty vb = vb
137
135
xseq f va vb = do
138
136
a <- va
139
137
b <- vb
140
138
return $ f a b
141
139
142
- xfold :: Deque ( Deque (HashMap Text Value )) -> Deque (HashMap Text Value )
140
+ xfold :: Seq ( Seq (HashMap Text Value )) -> Seq (HashMap Text Value )
143
141
xfold = foldl' (xseq HM. union) empty
0 commit comments