@@ -62,36 +62,6 @@ class TypeScriptJmesPathVisitor implements ExpressionVisitor<Void> {
6262 scopeCount = 0 ;
6363 }
6464
65- private String serializeObject (Map <String , Object > obj ) {
66- return "{" + obj .entrySet ().stream ()
67- .map (entry -> "\" " + entry .getKey () + "\" :" + serializeValue (entry .getValue ()))
68- .collect (Collectors .joining ("," ))
69- + "}" ;
70- }
71-
72- private String serializeArray (List <Object > array ) {
73- return "[" + array .stream ()
74- .map (this ::serializeValue )
75- .collect (Collectors .joining ("," ))
76- + "]" ;
77- }
78-
79- @ SuppressWarnings ("unchecked" )
80- private String serializeValue (Object value ) {
81- if (value == null ) {
82- return "null" ;
83- } else if (value instanceof String ) {
84- return "\" " + value + "\" " ;
85- } else if (value instanceof Number || value instanceof Boolean ) {
86- return value .toString ();
87- } else if (value instanceof Map ) {
88- return serializeObject ((Map <String , Object >) value );
89- } else if (value instanceof ArrayList ) {
90- return serializeArray ((List <Object >) value );
91- }
92- throw new CodegenException ("Unsupported literal type: " + value .getClass ());
93- }
94-
9565 public void run () {
9666 writer .openBlock ("let returnComparator = () => {" , "}" , () -> {
9767 executionContext = accessor ;
@@ -101,44 +71,6 @@ public void run() {
10171 executionContext = "returnComparator()" ;
10272 }
10373
104- private String makeNewScope (String prefix ) {
105- scopeCount += 1 ;
106- return prefix + scopeCount ;
107- }
108-
109- void writeBooleanExpectation (String expectedValue , String returnValue ) {
110- writer .openBlock ("if ($L == $L) {" , "}" , executionContext , expectedValue , () -> {
111- writer .write ("return $L;" , returnValue );
112- });
113- }
114-
115- void writeAnyStringEqualsExpectation (String expectedValue , String returnValue ) {
116- String element = makeNewScope ("anyStringEq_" );
117- writer .openBlock ("for (let $L of $L) {" , "}" , element , executionContext , () -> {
118- writer .openBlock ("if ($L == $S) {" , "}" , element , expectedValue , () -> {
119- writer .write ("return $L;" , returnValue );
120- });
121- });
122- }
123-
124- void writeAllStringEqualsExpectation (String expectedValue , String returnValue ) {
125- String element = makeNewScope ("element_" );
126- String result = makeNewScope ("allStringEq_" );
127- writer .write ("let $L = ($L.length > 0);" , result , executionContext );
128- writer .openBlock ("for (let $L of $L) {" , "}" , element , executionContext , () -> {
129- writer .write ("$L = $L && ($L == $S)" , result , result , element , expectedValue );
130- });
131- writer .openBlock ("if ($L) {" , "}" , result , () -> {
132- writer .write ("return $L;" , returnValue );
133- });
134- }
135-
136- void writeStringExpectation (String expectedValue , String returnValue ) {
137- writer .openBlock ("if ($L === $S) {" , "}" , executionContext , expectedValue , () -> {
138- writer .write ("return $L;" , returnValue );
139- });
140- }
141-
14274 @ Override
14375 public Void visitComparator (ComparatorExpression expression ) {
14476
@@ -374,4 +306,72 @@ public Void visitSubexpression(Subexpression expression) {
374306 expression .getRight ().accept (this );
375307 return null ;
376308 }
309+
310+ void writeBooleanExpectation (String expectedValue , String returnValue ) {
311+ writer .openBlock ("if ($L == $L) {" , "}" , executionContext , expectedValue , () -> {
312+ writer .write ("return $L;" , returnValue );
313+ });
314+ }
315+
316+ void writeAnyStringEqualsExpectation (String expectedValue , String returnValue ) {
317+ String element = makeNewScope ("anyStringEq_" );
318+ writer .openBlock ("for (let $L of $L) {" , "}" , element , executionContext , () -> {
319+ writer .openBlock ("if ($L == $S) {" , "}" , element , expectedValue , () -> {
320+ writer .write ("return $L;" , returnValue );
321+ });
322+ });
323+ }
324+
325+ void writeAllStringEqualsExpectation (String expectedValue , String returnValue ) {
326+ String element = makeNewScope ("element_" );
327+ String result = makeNewScope ("allStringEq_" );
328+ writer .write ("let $L = ($L.length > 0);" , result , executionContext );
329+ writer .openBlock ("for (let $L of $L) {" , "}" , element , executionContext , () -> {
330+ writer .write ("$L = $L && ($L == $S)" , result , result , element , expectedValue );
331+ });
332+ writer .openBlock ("if ($L) {" , "}" , result , () -> {
333+ writer .write ("return $L;" , returnValue );
334+ });
335+ }
336+
337+ void writeStringExpectation (String expectedValue , String returnValue ) {
338+ writer .openBlock ("if ($L === $S) {" , "}" , executionContext , expectedValue , () -> {
339+ writer .write ("return $L;" , returnValue );
340+ });
341+ }
342+
343+ private String makeNewScope (String prefix ) {
344+ scopeCount += 1 ;
345+ return prefix + scopeCount ;
346+ }
347+
348+ private String serializeObject (Map <String , Object > obj ) {
349+ return "{" + obj .entrySet ().stream ()
350+ .map (entry -> "\" " + entry .getKey () + "\" :" + serializeValue (entry .getValue ()))
351+ .collect (Collectors .joining ("," ))
352+ + "}" ;
353+ }
354+
355+ private String serializeArray (List <Object > array ) {
356+ return "[" + array .stream ()
357+ .map (this ::serializeValue )
358+ .collect (Collectors .joining ("," ))
359+ + "]" ;
360+ }
361+
362+ @ SuppressWarnings ("unchecked" )
363+ private String serializeValue (Object value ) {
364+ if (value == null ) {
365+ return "null" ;
366+ } else if (value instanceof String ) {
367+ return "\" " + value + "\" " ;
368+ } else if (value instanceof Number || value instanceof Boolean ) {
369+ return value .toString ();
370+ } else if (value instanceof Map ) {
371+ return serializeObject ((Map <String , Object >) value );
372+ } else if (value instanceof ArrayList ) {
373+ return serializeArray ((List <Object >) value );
374+ }
375+ throw new CodegenException ("Unsupported literal type: " + value .getClass ());
376+ }
377377}
0 commit comments