1313from .cypher import CypherItem
1414
1515
16- class Variable : # pylint: disable=R0903
16+ class Variable :
1717 """
1818Represent a node or edge variable within a query.
1919 """
@@ -45,18 +45,44 @@ def __repr__ (
4545 return str (_repr )
4646
4747
48+ def toJSON ( # pylint: disable=C0103
49+ self ,
50+ ) -> dict :
51+ """
52+ JSON-serializable representation
53+ """
54+ return {
55+ "name" : self .name ,
56+ "item" : self .item ,
57+ "predicates" : [ p .toJSON () for p in self .predicates ],
58+ }
59+
60+
4861class Predicate : # pylint: disable=R0903
4962 """
5063Represent a simple predicate.
5164 """
5265 def __init__ (
5366 self ,
5467 var : Variable ,
68+ elem : str ,
5569 ) -> None :
5670 """
5771Constructor.
5872 """
5973 self .var : Variable = var
74+ self .elem : str = elem
75+
76+
77+ def toJSON ( # pylint: disable=C0103
78+ self ,
79+ ) -> dict :
80+ """
81+ JSON-serializable representation
82+ """
83+ return {
84+ "elem" : self .elem
85+ }
6086
6187
6288class PredicateMap (Predicate ): # pylint: disable=R0903
@@ -66,13 +92,14 @@ class PredicateMap (Predicate): # pylint: disable=R0903
6692 def __init__ (
6793 self ,
6894 var : Variable ,
95+ elem : str ,
6996 key : str ,
7097 val : str ,
7198 ) -> None :
7299 """
73100Constructor.
74101 """
75- super ().__init__ (var )
102+ super ().__init__ (var , elem )
76103 self .key : str = key
77104 self .val : str = val
78105
@@ -92,19 +119,34 @@ def __repr__ (
92119 return str (_repr )
93120
94121
122+ def toJSON ( # pylint: disable=C0103
123+ self ,
124+ ) -> dict :
125+ """
126+ JSON-serializable representation
127+ """
128+ j = super ().toJSON ()
129+ j ["kind" ] = "map"
130+ j ["key" ] = self .key
131+ j ["val" ] = self .val
132+
133+ return j
134+
135+
95136class PredicateLabel (Predicate ): # pylint: disable=R0903
96137 """
97138Represent a predicate that specifies a label.
98139 """
99140 def __init__ (
100141 self ,
101142 var : Variable ,
143+ elem : str ,
102144 label : str ,
103145 ) -> None :
104146 """
105147Constructor.
106148 """
107- super ().__init__ (var )
149+ super ().__init__ (var , elem )
108150 self .label : str = label
109151
110152
@@ -122,19 +164,33 @@ def __repr__ (
122164 return str (_repr )
123165
124166
167+ def toJSON ( # pylint: disable=C0103
168+ self ,
169+ ) -> dict :
170+ """
171+ JSON-serializable representation
172+ """
173+ j = super ().toJSON ()
174+ j ["kind" ] = "label"
175+ j ["label" ] = self .label
176+
177+ return j
178+
179+
125180class PredicateDirection (Predicate ): # pylint: disable=R0903
126181 """
127182Represent a predicate that specifies a direction.
128183 """
129184 def __init__ (
130185 self ,
131186 var : Variable ,
187+ elem : str ,
132188 direction : str ,
133189 ) -> None :
134190 """
135191Constructor.
136192 """
137- super ().__init__ (var )
193+ super ().__init__ (var , elem )
138194 self .direction : str = direction
139195
140196
@@ -152,6 +208,19 @@ def __repr__ (
152208 return str (_repr )
153209
154210
211+ def toJSON ( # pylint: disable=C0103
212+ self ,
213+ ) -> dict :
214+ """
215+ JSON-serializable representation
216+ """
217+ j = super ().toJSON ()
218+ j ["kind" ] = "direction"
219+ j ["direction" ] = self .direction
220+
221+ return j
222+
223+
155224class Path : # pylint: disable=R0903
156225 """
157226Represent a path pattern, with multiple node/edge/regex elements.
@@ -162,7 +231,7 @@ def __init__ (
162231 """
163232Constructor.
164233 """
165- self .elem : list = []
234+ self .elem : typing . List [ Variable ] = []
166235
167236
168237 def __repr__ (
@@ -320,10 +389,10 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915
320389 child_type = self .items [j ].ast_typestr
321390
322391 if child_type == "node pattern" :
323- path .elem .append (self .parse_items (i = j , debug = debug ))
392+ path .elem .append (self .parse_items (i = j , debug = debug )) # type: ignore
324393
325394 elif child_type == "rel pattern" :
326- path .elem .append (self .parse_items (i = j , debug = debug ))
395+ path .elem .append (self .parse_items (i = j , debug = debug )) # type: ignore
327396
328397 return path
329398
@@ -343,6 +412,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915
343412 if child_type == "label" :
344413 node_var .predicates .append (PredicateLabel (
345414 node_var ,
415+ "node" ,
346416 self .items [j ].literal .strip (":" ),
347417 ))
348418
@@ -351,6 +421,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915
351421
352422 node_var .predicates .append (PredicateMap (
353423 node_var ,
424+ "node" ,
354425 self .items [key_i ].literal , # pylint: disable=W0621
355426 self .items [val_i ].literal .strip ("'" ), # pylint: disable=W0621
356427 ))
@@ -372,6 +443,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915
372443 if child_type == "label" :
373444 edge_var .predicates .append (PredicateLabel (
374445 edge_var ,
446+ "edge" ,
375447 self .items [j ].literal .strip (":" ),
376448 ))
377449
@@ -380,13 +452,15 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915
380452
381453 edge_var .predicates .append (PredicateMap (
382454 edge_var ,
455+ "edge" ,
383456 self .items [key_i ].literal , # pylint: disable=W0621
384457 self .items [val_i ].literal .strip ("'" ), # pylint: disable=W0621
385458 ))
386459
387460 elif child_type == "rel type" :
388461 edge_var .predicates .append (PredicateDirection (
389462 edge_var ,
463+ "edge" ,
390464 self .items [j ].literal .strip (":" ),
391465 ))
392466
0 commit comments