32
32
import com .google .api .generator .engine .ast .VariableExpr ;
33
33
import com .google .api .generator .gapic .composer .resourcename .ResourceNameTokenizer ;
34
34
import com .google .api .generator .gapic .model .Field ;
35
+ import com .google .api .generator .gapic .model .HttpBindings ;
35
36
import com .google .api .generator .gapic .model .Message ;
36
37
import com .google .api .generator .gapic .model .MethodArgument ;
37
38
import com .google .api .generator .gapic .model .ResourceName ;
@@ -65,7 +66,8 @@ public static Expr createMethodArgValue(
65
66
MethodArgument methodArg ,
66
67
Map <String , ResourceName > resourceNames ,
67
68
Map <String , Message > messageTypes ,
68
- Map <String , String > valuePatterns ) {
69
+ Map <String , String > valuePatterns ,
70
+ HttpBindings bindings ) {
69
71
if (methodArg .isResourceNameHelper ()) {
70
72
Preconditions .checkState (
71
73
methodArg .field ().hasResourceReference (),
@@ -84,7 +86,8 @@ public static Expr createMethodArgValue(
84
86
resourceName ,
85
87
methodArg .field ().resourceReference ().isChildType (),
86
88
resourceNames .values ().stream ().collect (Collectors .toList ()),
87
- methodArg .field ().name ());
89
+ methodArg .field ().name (),
90
+ bindings );
88
91
89
92
if (!methodArg .isResourceNameHelper () && methodArg .field ().hasResourceReference ()) {
90
93
defValue =
@@ -156,7 +159,7 @@ public static Expr createValue(
156
159
Message nestedMessage = messageTypes .get (field .type ().reference ().fullName ());
157
160
if (nestedMessage != null ) {
158
161
return createSimpleMessageBuilderValue (
159
- nestedMessage , resourceNames , messageTypes , nestedValuePatterns );
162
+ nestedMessage , resourceNames , messageTypes , nestedValuePatterns , null );
160
163
}
161
164
}
162
165
@@ -209,8 +212,10 @@ public static Expr createResourceHelperValue(
209
212
ResourceName resourceName ,
210
213
boolean isChildType ,
211
214
List <ResourceName > resnames ,
212
- String fieldOrMessageName ) {
213
- return createResourceHelperValue (resourceName , isChildType , resnames , fieldOrMessageName , true );
215
+ String fieldOrMessageName ,
216
+ HttpBindings bindings ) {
217
+ return createResourceHelperValue (
218
+ resourceName , isChildType , resnames , fieldOrMessageName , true , bindings );
214
219
}
215
220
216
221
private static Optional <ResourceName > findParentResource (
@@ -239,7 +244,8 @@ static Expr createResourceHelperValue(
239
244
boolean isChildType ,
240
245
List <ResourceName > resnames ,
241
246
String fieldOrMessageName ,
242
- boolean allowAnonResourceNameClass ) {
247
+ boolean allowAnonResourceNameClass ,
248
+ HttpBindings bindings ) {
243
249
244
250
if (isChildType ) {
245
251
resourceName = findParentResource (resourceName , resnames ).orElse (resourceName );
@@ -249,13 +255,15 @@ static Expr createResourceHelperValue(
249
255
List <ResourceName > unexaminedResnames = new ArrayList <>(resnames );
250
256
for (ResourceName resname : resnames ) {
251
257
unexaminedResnames .remove (resname );
252
- if (!resname .isOnlyWildcard ()) {
253
- return createResourceHelperValue (resname , false , unexaminedResnames , fieldOrMessageName );
258
+ if (!resname .isOnlyWildcard ()
259
+ && (bindings == null || resname .getMatchingPattern (bindings ) != null )) {
260
+ return createResourceHelperValue (
261
+ resname , false , unexaminedResnames , fieldOrMessageName , null );
254
262
}
255
263
}
256
264
257
265
return allowAnonResourceNameClass
258
- ? createAnonymousResourceNameClassValue (fieldOrMessageName )
266
+ ? createAnonymousResourceNameClassValue (fieldOrMessageName , bindings )
259
267
: ValueExpr .withValue (
260
268
StringObjectValue .withValue (
261
269
String .format ("%s%s" , fieldOrMessageName , fieldOrMessageName .hashCode ())));
@@ -272,15 +280,22 @@ static Expr createResourceHelperValue(
272
280
if (containsOnlyDeletedTopic ) {
273
281
ofMethodName = "ofDeletedTopic" ;
274
282
} else {
275
- for (String pattern : resourceName .patterns ()) {
276
- if (pattern .equals (ResourceNameConstants .WILDCARD_PATTERN )
277
- || pattern .equals (ResourceNameConstants .DELETED_TOPIC_LITERAL )) {
278
- continue ;
283
+ String matchingPattern = null ;
284
+ if (bindings != null ) {
285
+ matchingPattern = resourceName .getMatchingPattern (bindings );
286
+ }
287
+ if (matchingPattern == null ) {
288
+ for (String pattern : resourceName .patterns ()) {
289
+ if (pattern .equals (ResourceNameConstants .WILDCARD_PATTERN )
290
+ || pattern .equals (ResourceNameConstants .DELETED_TOPIC_LITERAL )) {
291
+ continue ;
292
+ }
293
+ matchingPattern = pattern ;
294
+ break ;
279
295
}
280
- patternPlaceholderTokens .addAll (
281
- ResourceNameTokenizer .parseTokenHierarchy (Arrays .asList (pattern )).get (0 ));
282
- break ;
283
296
}
297
+ patternPlaceholderTokens .addAll (
298
+ ResourceNameTokenizer .parseTokenHierarchy (Arrays .asList (matchingPattern )).get (0 ));
284
299
}
285
300
286
301
boolean hasOnePattern = resourceName .patterns ().size () == 1 ;
@@ -312,16 +327,20 @@ static Expr createResourceHelperValue(
312
327
}
313
328
314
329
public static Expr createSimpleMessageBuilderValue (
315
- Message message , Map <String , ResourceName > resourceNames , Map <String , Message > messageTypes ) {
330
+ Message message ,
331
+ Map <String , ResourceName > resourceNames ,
332
+ Map <String , Message > messageTypes ,
333
+ HttpBindings bindings ) {
316
334
return createSimpleMessageBuilderValue (
317
- message , resourceNames , messageTypes , Collections .emptyMap ());
335
+ message , resourceNames , messageTypes , Collections .emptyMap (), bindings );
318
336
}
319
337
320
338
public static Expr createSimpleMessageBuilderValue (
321
339
Message message ,
322
340
Map <String , ResourceName > resourceNames ,
323
341
Map <String , Message > messageTypes ,
324
- Map <String , String > valuePatterns ) {
342
+ Map <String , String > valuePatterns ,
343
+ HttpBindings bindings ) {
325
344
MethodInvocationExpr builderExpr =
326
345
MethodInvocationExpr .builder ()
327
346
.setStaticReferenceType (message .type ())
@@ -350,7 +369,8 @@ public static Expr createSimpleMessageBuilderValue(
350
369
field .resourceReference ().isChildType (),
351
370
resourceNames .values ().stream ().collect (Collectors .toList ()),
352
371
message .name (),
353
- /* allowAnonResourceNameClass = */ false );
372
+ /* allowAnonResourceNameClass = */ false ,
373
+ bindings );
354
374
defaultExpr =
355
375
MethodInvocationExpr .builder ()
356
376
.setExprReferenceExpr (defaultExpr )
@@ -507,7 +527,8 @@ public static Expr createSimplePagedResponseValue(
507
527
}
508
528
509
529
@ VisibleForTesting
510
- static AnonymousClassExpr createAnonymousResourceNameClassValue (String fieldOrMessageName ) {
530
+ static AnonymousClassExpr createAnonymousResourceNameClassValue (
531
+ String fieldOrMessageName , HttpBindings matchedBindings ) {
511
532
TypeNode stringMapType =
512
533
TypeNode .withReference (
513
534
ConcreteReference .builder ()
@@ -525,12 +546,18 @@ static AnonymousClassExpr createAnonymousResourceNameClassValue(String fieldOrMe
525
546
// fieldValuesMap.put("resource", "resource-12345");
526
547
// return fieldValuesMap;
527
548
// }
549
+
550
+ String toStringValue = String .format ("%s%s" , fieldOrMessageName , fieldOrMessageName .hashCode ());
551
+ if (matchedBindings != null ) {
552
+ Map <String , String > valuePatterns = matchedBindings .getPathParametersValuePatterns ();
553
+ toStringValue =
554
+ constructValueMatchingPattern (fieldOrMessageName , valuePatterns .get (fieldOrMessageName ));
555
+ }
556
+
528
557
VariableExpr fieldValuesMapVarExpr =
529
558
VariableExpr .withVariable (
530
559
Variable .builder ().setType (stringMapType ).setName ("fieldValuesMap" ).build ());
531
- StringObjectValue fieldOrMessageStringValue =
532
- StringObjectValue .withValue (
533
- String .format ("%s%s" , fieldOrMessageName , fieldOrMessageName .hashCode ()));
560
+ StringObjectValue fieldOrMessageStringValue = StringObjectValue .withValue (toStringValue );
534
561
535
562
List <Expr > bodyExprs =
536
563
Arrays .asList (
@@ -586,15 +613,24 @@ static AnonymousClassExpr createAnonymousResourceNameClassValue(String fieldOrMe
586
613
.build ())
587
614
.build ();
588
615
616
+ MethodDefinition toStringMethod =
617
+ MethodDefinition .builder ()
618
+ .setIsOverride (true )
619
+ .setScope (ScopeNode .PUBLIC )
620
+ .setReturnType (TypeNode .STRING )
621
+ .setName ("toString" )
622
+ .setReturnExpr (ValueExpr .withValue (StringObjectValue .withValue (toStringValue )))
623
+ .build ();
624
+
589
625
return AnonymousClassExpr .builder ()
590
626
.setType (
591
627
TypeNode .withReference (
592
628
ConcreteReference .withClazz (com .google .api .resourcenames .ResourceName .class )))
593
- .setMethods (Arrays .asList (getFieldValuesMapMethod , getFieldValueMethod ))
629
+ .setMethods (Arrays .asList (getFieldValuesMapMethod , getFieldValueMethod , toStringMethod ))
594
630
.build ();
595
631
}
596
632
597
- private static String constructValueMatchingPattern (String fieldName , String pattern ) {
633
+ public static String constructValueMatchingPattern (String fieldName , String pattern ) {
598
634
if (pattern == null || pattern .isEmpty ()) {
599
635
return fieldName + fieldName .hashCode ();
600
636
}
0 commit comments