35
35
import com .google .api .tools .framework .model .Interface ;
36
36
import com .google .api .tools .framework .model .Method ;
37
37
import com .google .api .tools .framework .model .Model ;
38
+ import com .google .api .tools .framework .model .ProtoElement ;
38
39
import com .google .api .tools .framework .model .ProtoFile ;
39
40
import com .google .api .tools .framework .model .SimpleLocation ;
40
41
import com .google .api .tools .framework .model .SymbolTable ;
47
48
import com .google .common .collect .Iterables ;
48
49
import com .google .protobuf .Api ;
49
50
import com .google .protobuf .DescriptorProtos ;
50
- import java .util .Collection ;
51
51
import java .util .Comparator ;
52
52
import java .util .HashMap ;
53
53
import java .util .HashSet ;
54
54
import java .util .LinkedHashMap ;
55
55
import java .util .List ;
56
56
import java .util .Map ;
57
- import java .util .Map .Entry ;
58
57
import java .util .Objects ;
59
58
import java .util .Optional ;
60
59
import java .util .Set ;
60
+ import java .util .function .Function ;
61
61
import java .util .stream .Collectors ;
62
62
import javax .annotation .Nullable ;
63
63
import org .apache .commons .lang3 .StringUtils ;
@@ -279,16 +279,18 @@ public static GapicProductConfig create(
279
279
getInterfacesFromProtoFile (diagCollector , sourceProtos , symbolTable );
280
280
281
281
ImmutableList <GapicInterfaceInput > interfaceInputs ;
282
- if (!configProto .equals (ConfigProto .getDefaultInstance ())) {
282
+ if (protoParser .isProtoAnnotationsEnabled ()) {
283
+ interfaceInputs =
284
+ createInterfaceInputsWithAnnotationsAndGapicConfig (
285
+ diagCollector , configProto .getInterfacesList (), protoInterfaces , language );
286
+ } else {
283
287
interfaceInputs =
284
- createInterfaceInputsWithGapicConfig (
288
+ createInterfaceInputsWithGapicConfigOnly (
285
289
diagCollector ,
286
290
configProto .getInterfacesList (),
287
291
protoInterfaces ,
288
292
symbolTable ,
289
293
language );
290
- } else {
291
- interfaceInputs = createInterfaceInputsWithoutGapicConfig (protoInterfaces .values ());
292
294
}
293
295
if (interfaceInputs == null ) {
294
296
return null ;
@@ -475,7 +477,22 @@ private static GapicProductConfig createDummyInstance(
475
477
}
476
478
477
479
/** Return the list of information about clients to be generated. */
478
- private static ImmutableList <GapicInterfaceInput > createInterfaceInputsWithGapicConfig (
480
+ private static ImmutableList <GapicInterfaceInput >
481
+ createInterfaceInputsWithAnnotationsAndGapicConfig (
482
+ DiagCollector diagCollector ,
483
+ List <InterfaceConfigProto > interfaceConfigProtosList ,
484
+ ImmutableMap <String , Interface > protoInterfaces ,
485
+ TargetLanguage language ) {
486
+ return createGapicInterfaceInputList (
487
+ diagCollector ,
488
+ language ,
489
+ protoInterfaces .values (),
490
+ interfaceConfigProtosList
491
+ .stream ()
492
+ .collect (Collectors .toMap (InterfaceConfigProto ::getName , Function .identity ())));
493
+ }
494
+
495
+ private static ImmutableList <GapicInterfaceInput > createInterfaceInputsWithGapicConfigOnly (
479
496
DiagCollector diagCollector ,
480
497
List <InterfaceConfigProto > interfaceConfigProtosList ,
481
498
ImmutableMap <String , Interface > protoInterfaces ,
@@ -491,23 +508,48 @@ private static ImmutableList<GapicInterfaceInput> createInterfaceInputsWithGapic
491
508
// Parse GAPIC config for interfaceConfigProtos.
492
509
for (InterfaceConfigProto interfaceConfigProto : interfaceConfigProtosList ) {
493
510
Interface apiInterface = symbolTable .lookupInterface (interfaceConfigProto .getName ());
494
- if (apiInterface == null || !apiInterface .isReachable ()) {
511
+ if (apiInterface == null ) {
512
+ List <String > interfaces =
513
+ symbolTable
514
+ .getInterfaces ()
515
+ .stream ()
516
+ .map (ProtoElement ::getFullName )
517
+ .collect (Collectors .toList ());
518
+ String interfacesString = String .join ("," , interfaces );
519
+ diagCollector .addDiag (
520
+ Diag .error (
521
+ SimpleLocation .TOPLEVEL ,
522
+ "interface not found: %s. Interfaces: [%s]" ,
523
+ interfaceConfigProto .getName (),
524
+ interfacesString ));
525
+ continue ;
526
+ }
527
+ if (!apiInterface .isReachable ()) {
495
528
diagCollector .addDiag (
496
529
Diag .error (
497
530
SimpleLocation .TOPLEVEL ,
498
- "interface not found : %s" ,
531
+ "interface not reachable : %s. " ,
499
532
interfaceConfigProto .getName ()));
500
533
continue ;
501
534
}
502
535
interfaceConfigProtos .put (interfaceConfigProto .getName (), interfaceConfigProto );
503
536
interfaceMap .put (interfaceConfigProto .getName (), apiInterface );
504
537
}
505
538
539
+ return createGapicInterfaceInputList (
540
+ diagCollector , language , interfaceMap .values (), interfaceConfigProtos );
541
+ }
542
+
543
+ private static ImmutableList <GapicInterfaceInput > createGapicInterfaceInputList (
544
+ DiagCollector diagCollector ,
545
+ TargetLanguage language ,
546
+ Iterable <Interface > interfaceList ,
547
+ Map <String , InterfaceConfigProto > interfaceConfigProtos ) {
548
+
506
549
// Store info about each Interface in a GapicInterfaceInput object.
507
550
ImmutableList .Builder <GapicInterfaceInput > interfaceInputs = ImmutableList .builder ();
508
- for (Entry <String , Interface > interfaceEntry : interfaceMap .entrySet ()) {
509
- String serviceFullName = interfaceEntry .getKey ();
510
- Interface apiInterface = interfaceEntry .getValue ();
551
+ for (Interface apiInterface : interfaceList ) {
552
+ String serviceFullName = apiInterface .getFullName ();
511
553
GapicInterfaceInput .Builder interfaceInput =
512
554
GapicInterfaceInput .newBuilder ().setInterface (apiInterface );
513
555
@@ -552,22 +594,6 @@ private static ImmutableMap<String, Interface> getInterfacesFromProtoFile(
552
594
return protoInterfaces .build ();
553
595
}
554
596
555
- /** Return the list of information about clients to be generated. */
556
- private static ImmutableList <GapicInterfaceInput > createInterfaceInputsWithoutGapicConfig (
557
- Collection <Interface > protoInterfaces ) {
558
- // Store info about each Interface in a GapicInterfaceInput object.
559
- ImmutableList .Builder <GapicInterfaceInput > interfaceInputs = ImmutableList .builder ();
560
- for (Interface apiInterface : protoInterfaces ) {
561
- GapicInterfaceInput .Builder interfaceInput =
562
- GapicInterfaceInput .newBuilder ()
563
- .setInterface (apiInterface )
564
- .setInterfaceConfigProto (InterfaceConfigProto .getDefaultInstance ())
565
- .setMethodsToGenerate (findMethodsToGenerateWithoutConfigYaml (apiInterface ));
566
- interfaceInputs .add (interfaceInput .build ());
567
- }
568
- return interfaceInputs .build ();
569
- }
570
-
571
597
/** Find the methods that should be generated on the surface when no GAPIC config was given. */
572
598
private static ImmutableMap <Method , MethodConfigProto > findMethodsToGenerateWithoutConfigYaml (
573
599
Interface apiInterface ) {
0 commit comments