16
16
package software .amazon .smithy .typescript .codegen ;
17
17
18
18
import java .util .ArrayList ;
19
+ import java .util .List ;
19
20
import java .util .Map ;
21
+ import java .util .stream .Collectors ;
20
22
import software .amazon .smithy .codegen .core .CodegenException ;
21
23
import software .amazon .smithy .jmespath .ExpressionVisitor ;
22
24
import software .amazon .smithy .jmespath .JmespathExpression ;
@@ -61,35 +63,17 @@ class TypeScriptJmesPathVisitor implements ExpressionVisitor<Void> {
61
63
}
62
64
63
65
private String serializeObject (Map <String , Object > obj ) {
64
- StringBuilder builder = new StringBuilder ();
65
- builder .append ("{" );
66
- boolean first = true ; // first key-value pair
67
- for (Map .Entry <String , Object > entry : obj .entrySet ()) {
68
- if (!first ) {
69
- builder .append ("," );
70
- }
71
- builder .append ("\" " ).append (entry .getKey ()).append ("\" :" );
72
- // recursively serialize value (could be primitive, obj, array)
73
- builder .append (serializeValue (entry .getValue ()));
74
- first = false ;
75
- }
76
- builder .append ("}" );
77
- return builder .toString ();
66
+ return "{" + obj .entrySet ().stream ()
67
+ .map (entry -> "\" " + entry .getKey () + "\" :" + serializeValue (entry .getValue ()))
68
+ .collect (Collectors .joining ("," ))
69
+ + "}" ;
78
70
}
79
71
80
- private String serializeArray (ArrayList <Object > array ) {
81
- StringBuilder builder = new StringBuilder ();
82
- builder .append ("[" );
83
- boolean first = true ;
84
- for (Object value : array ) {
85
- if (!first ) {
86
- builder .append ("," );
87
- }
88
- builder .append (serializeValue (value ));
89
- first = false ;
90
- }
91
- builder .append ("]" );
92
- return builder .toString ();
72
+ private String serializeArray (List <Object > array ) {
73
+ return "[" + array .stream ()
74
+ .map (this ::serializeValue )
75
+ .collect (Collectors .joining ("," ))
76
+ + "]" ;
93
77
}
94
78
95
79
@ SuppressWarnings ("unchecked" )
@@ -103,7 +87,7 @@ private String serializeValue(Object value) {
103
87
} else if (value instanceof Map ) {
104
88
return serializeObject ((Map <String , Object >) value );
105
89
} else if (value instanceof ArrayList ) {
106
- return serializeArray ((ArrayList <Object >) value );
90
+ return serializeArray ((List <Object >) value );
107
91
}
108
92
throw new CodegenException ("Unsupported literal type: " + value .getClass ());
109
93
}
@@ -245,14 +229,10 @@ public Void visitLiteral(LiteralExpression expression) {
245
229
executionContext = "\" " + expression .getValue ().toString () + "\" " ;
246
230
break ;
247
231
case OBJECT :
248
- @ SuppressWarnings ("unchecked" )
249
- Map <String , Object > objValue = (Map <String , Object >) expression .getValue ();
250
- executionContext = serializeObject (objValue );
232
+ executionContext = serializeObject (expression .expectObjectValue ());
251
233
break ;
252
234
case ARRAY :
253
- @ SuppressWarnings ("unchecked" )
254
- ArrayList <Object > arrayValue = (ArrayList <Object >) expression .getValue ();
255
- executionContext = serializeArray (arrayValue );
235
+ executionContext = serializeArray (expression .expectArrayValue ());
256
236
break ;
257
237
default :
258
238
// All other options are already valid js literials.
0 commit comments