@@ -59,6 +59,54 @@ class TypeScriptJmesPathVisitor implements ExpressionVisitor<Void> {
59
59
scopeCount = 0 ;
60
60
}
61
61
62
+ private String serializeObject (Map <String , Object > obj ) {
63
+ StringBuilder builder = new StringBuilder ();
64
+ builder .append ("{" );
65
+ boolean first = true ; // first key-value pair
66
+ for (Map .Entry <String , Object > entry : obj .entrySet ()) {
67
+ if (!first ) {
68
+ builder .append ("," );
69
+ }
70
+ builder .append ("\" " ).append (entry .getKey ()).append ("\" :" );
71
+ // recursively serialize value (could be primitive, obj, array)
72
+ builder .append (serializeValue (entry .getValue ()));
73
+ first = false ;
74
+ }
75
+ builder .append ("}" );
76
+ return builder .toString ();
77
+ }
78
+
79
+ private String serializeArray (ArrayList <Object > array ) {
80
+ StringBuilder builder = new StringBuilder ();
81
+ builder .append ("[" );
82
+ boolean first = true ;
83
+ for (Object value : array ) {
84
+ if (!first ) {
85
+ builder .append ("," );
86
+ }
87
+ builder .append (serializeValue (value ));
88
+ first = false ;
89
+ }
90
+ builder .append ("]" );
91
+ return builder .toString ();
92
+ }
93
+
94
+ @ SuppressWarnings ("unchecked" )
95
+ private String serializeValue (Object value ) {
96
+ if (value == null ) {
97
+ return "null" ;
98
+ } else if (value instanceof String ) {
99
+ return "\" " + value + "\" " ;
100
+ } else if (value instanceof Number || value instanceof Boolean ) {
101
+ return value .toString ();
102
+ } else if (value instanceof Map ) {
103
+ return serializeObject ((Map <String , Object >) value );
104
+ } else if (value instanceof ArrayList ) {
105
+ return serializeArray ((ArrayList <Object >) value );
106
+ }
107
+ throw new CodegenException ("Unsupported literal type: " + value .getClass ());
108
+ }
109
+
62
110
public void run () {
63
111
writer .openBlock ("let returnComparator = () => {" , "}" , () -> {
64
112
executionContext = accessor ;
@@ -196,10 +244,15 @@ public Void visitLiteral(LiteralExpression expression) {
196
244
executionContext = "\" " + expression .getValue ().toString () + "\" " ;
197
245
break ;
198
246
case OBJECT :
247
+ @ SuppressWarnings ("unchecked" )
248
+ Map <String , Object > objValue = (Map <String , Object >) expression .getValue ();
249
+ executionContext = serializeObject (objValue );
250
+ break ;
199
251
case ARRAY :
200
- // TODO: resolve JMESPATH OBJECTS and ARRAY types as literals
201
- throw new CodegenException ("TypeScriptJmesPath visitor has not implemented resolution of ARRAY and"
202
- + " OBJECT literials " );
252
+ @ SuppressWarnings ("unchecked" )
253
+ ArrayList <Object > arrayValue = (ArrayList <Object >) expression .getValue ();
254
+ executionContext = serializeArray (arrayValue );
255
+ break ;
203
256
default :
204
257
// All other options are already valid js literials.
205
258
// (BOOLEAN, ANY, NULL, NUMBER, EXPRESSION)
0 commit comments