/** * If rerouteToGrpcInterface is set, then looks up that interface and returns it, otherwise * returns the value of defaultInterface. */ public static Interface getTargetInterface( Interface defaultInterface, String rerouteToGrpcInterface) { Interface targetInterface = defaultInterface; if (!Strings.isNullOrEmpty(rerouteToGrpcInterface)) { targetInterface = defaultInterface.getModel().getSymbolTable().lookupInterface(rerouteToGrpcInterface); if (targetInterface == null) { throw new IllegalArgumentException( "reroute_to_grpc_interface not found: " + rerouteToGrpcInterface); } } return targetInterface; }
@Override public ProtoInterfaceModel getInterface(String interfaceName) { return new ProtoInterfaceModel(protoModel.getSymbolTable().lookupInterface(interfaceName)); }
/** Helper to extract the types from the underlying model. */ private List<TypeRef> getTypes(Model model) { List<TypeRef> types = new ArrayList<>(); for (Type type : model.getServiceConfig().getTypesList()) { types.add(model.getSymbolTable().lookupType(type.getName())); } return types; }
/** Gets the interfaces for the apis in the service config. */ public static List<Interface> getInterfaces(Model model) { return model .getServiceConfig() .getApisList() .stream() .map(api -> model.getSymbolTable().lookupInterface(api.getName())) .collect(ImmutableList.toImmutableList()); } }
private Interface resolveInterface(String name, LocationContext location) { Interface iface = getModel().getSymbolTable().resolveInterface("", name); if (iface == null) { error(location, "The API '%s' cannot be resolved.", name); } return iface; } }
private Interface resolveInterface(String name, LocationContext location) { Interface iface = getModel().getSymbolTable().resolveInterface("", name); if (iface == null) { error(location, "The API '%s' cannot be resolved.", name); } return iface; } }
private boolean packageHasEnums(Model model) { for (TypeRef type : model.getSymbolTable().getDeclaredTypes()) { if (type.isEnum() && type.getEnumType().isReachable()) { return true; } } return false; }
/** * Return the list of API versions for all reachable API methods. */ public static List<String> getReachableRestVersions(Model model) { Set<String> versions = Sets.newLinkedHashSet(); for (Interface iface : model.getSymbolTable().getInterfaces()) { for (Method method : iface.getReachableMethods()) { if (method.hasAttribute(HttpAttribute.KEY)) { for (HttpAttribute binding : method.getAttribute(HttpAttribute.KEY).getAllBindings()) { versions.add(binding.getRestMethod().getVersionWithDefault()); } } } } List<String> versionsList = Lists.newArrayList(versions); Collections.sort(versionsList, Collections.reverseOrder(new VersionComparator())); return versionsList; } }
/** * Return the list of API versions for all reachable API methods. */ public static List<String> getReachableRestVersions(Model model) { Set<String> versions = Sets.newLinkedHashSet(); for (Interface iface : model.getSymbolTable().getInterfaces()) { for (Method method : iface.getReachableMethods()) { if (method.hasAttribute(HttpAttribute.KEY)) { for (HttpAttribute binding : method.getAttribute(HttpAttribute.KEY).getAllBindings()) { versions.add(binding.getRestMethod().getVersionWithDefault()); } } } } List<String> versionsList = Lists.newArrayList(versions); Collections.sort(versionsList, Collections.reverseOrder(new VersionComparator())); return versionsList; } }
@Override public List<MethodModel> getMethods() { ImmutableList.Builder<MethodModel> methods = ImmutableList.builder(); for (Method method : protoInterface.getMethods()) { methods.add(new ProtoMethodModel(method)); } SymbolTable symbolTable = protoInterface.getModel().getSymbolTable(); for (Mixin mixin : protoInterface.getConfig().getMixinsList()) { Interface mixinInterface = symbolTable.lookupInterface(mixin.getName()); for (Method method : mixinInterface.getMethods()) { methods.add(new ProtoMethodModel(method)); } } return methods.build(); }
private static Map<Interface, Interface> createGrpcRerouteMap( Model model, GapicProductConfig productConfig) { HashMap<Interface, Interface> grpcRerouteMap = new HashMap<>(); for (Interface apiInterface : ProtoModels.getInterfaces(model)) { InterfaceConfig interfaceConfig = productConfig.getInterfaceConfig(apiInterface); if (interfaceConfig == null) { continue; } if (!apiInterface.isReachable()) { continue; } for (MethodConfig methodConfig : interfaceConfig.getMethodConfigs()) { String reroute = methodConfig.getRerouteToGrpcInterface(); if (!Strings.isNullOrEmpty(reroute)) { Interface targetInterface = model.getSymbolTable().lookupInterface(reroute); grpcRerouteMap.put(targetInterface, apiInterface); } } } return grpcRerouteMap; }
private Interface getInterface(String interfaceName) { Interface endpointInterface = model.getSymbolTable().lookupInterface(interfaceName); Assert.assertNotNull(endpointInterface); return endpointInterface; }
/** * Returns the body type iff the body associates with exactly one message type. * Otherwise, returns null. */ @Nullable private TypeRef getSingleBodyType(Method method) { HttpAttribute httpConfig = method.getAttribute(HttpAttribute.KEY); if (httpConfig == null) { return null; } if (!httpConfig.bodyCapturesUnboundFields() && httpConfig.getBodySelectors().size() == 1) { TypeRef type = httpConfig.getBodySelectors().get(0).getType(); if (type.isMessage()) { // Get the TypeRef of the declared MessageType that is referenced in the body // selector field. // TODO(user): If it is common use pattern, consider moving it to where it belongs. return model.getSymbolTable().lookupType(type.getMessageType().getFullName()); } } return null; }
/** * Returns the body type iff the body associates with exactly one message type. * Otherwise, returns null. */ @Nullable private TypeRef getSingleBodyType(Method method) { HttpAttribute httpConfig = method.getAttribute(HttpAttribute.KEY); if (httpConfig == null) { return null; } if (!httpConfig.bodyCapturesUnboundFields() && httpConfig.getBodySelectors().size() == 1) { TypeRef type = httpConfig.getBodySelectors().get(0).getType(); if (type.isMessage()) { // Get the TypeRef of the declared MessageType that is referenced in the body // selector field. // TODO(user): If it is common use pattern, consider moving it to where it belongs. return model.getSymbolTable().lookupType(type.getMessageType().getFullName()); } } return null; }
/** Returns a base package name for an API's client. */ @Nullable public static String getPackageName(Model model) { if (model.getServiceConfig().getApisCount() > 0) { Api api = model.getServiceConfig().getApis(0); Interface apiInterface = model.getSymbolTable().lookupInterface(api.getName()); if (apiInterface != null) { return apiInterface.getFile().getFullName(); } } return null; } }
@BeforeClass public static void startUp() { Mockito.when(simpleMethod.getModel()).thenReturn(model); Mockito.when(lroAnnotatedMethod.getModel()).thenReturn(model); Mockito.when(model.getSymbolTable()).thenReturn(symbolTable); Mockito.when(protoParser.getProtoPackage(ArgumentMatchers.any(Method.class))) .thenReturn(PROTO_PACKAGE_NAME); Mockito.when(protoParser.getLongRunningOperation(lroAnnotatedMethod)) .thenReturn( OperationData.newBuilder() .setMetadataType(ANNOTATIONS_METADATA_TYPE) .setResponseType(ANNOTATIONS_RETURN_TYPE_NAME) .build()); Mockito.when(symbolTable.lookupType(PROTO_PACKAGE_NAME + "." + GAPIC_CONFIG_METADATA_TYPE)) .thenReturn(gapicConfigMetadataType); Mockito.when(symbolTable.lookupType(PROTO_PACKAGE_NAME + "." + GAPIC_CONFIG_RETURN_TYPE_NAME)) .thenReturn(gapicConfigReturnType); Mockito.when(symbolTable.lookupType(PROTO_PACKAGE_NAME + "." + ANNOTATIONS_METADATA_TYPE)) .thenReturn(annotationsMetadataType); Mockito.when(symbolTable.lookupType(PROTO_PACKAGE_NAME + "." + ANNOTATIONS_RETURN_TYPE_NAME)) .thenReturn(annotationsReturnType); }
@BeforeClass public static void setupClass() { List<String> protoFiles = Lists.newArrayList("myproto.proto"); List<String> yamlFiles = Lists.newArrayList("myproto.yaml"); testDataLocator = TestDataLocator.create(SampleInitCodeTest.class); testConfig = new TestConfig(testDataLocator, tempDir.getRoot().getPath(), protoFiles); model = testConfig.createModel(yamlFiles); StandardSetup.registerStandardProcessors(model); StandardSetup.registerStandardConfigAspects(model); model.establishStage(Merged.KEY); apiInterface = model.getSymbolTable().getInterfaces().asList().get(0); method = apiInterface.getMethods().get(0); }
private void addApiImports(GapicInterfaceContext context) { for (TypeRef type : context.getInterface().getModel().getSymbolTable().getDeclaredTypes()) { if (type.isEnum() && type.getEnumType().isReachable()) { context.getImportTypeTable().getAndSaveNicknameFor(type); break; } } for (MethodModel method : context.getSupportedMethods()) { addMethodImports(context.asDynamicMethodContext(method)); } }
private void addApiImports(GapicInterfaceContext context) { for (TypeRef type : context.getInterface().getModel().getSymbolTable().getDeclaredTypes()) { if (type.isEnum() && type.getEnumType().isReachable()) { context.getImportTypeTable().getAndSaveNicknameFor(type); break; } } for (MethodModel method : context.getSupportedMethods()) { addMethodImports(context.asDynamicMethodContext(method)); } }
@BeforeClass public static void setupClass() { TestDataLocator locator = TestDataLocator.create(GoGapicSurfaceTransformerTest.class); model = CodegenTestUtil.readModel( locator, tempDir, new String[] {"myproto.proto", "singleservice.proto"}, new String[] {"myproto.yaml"}); for (Interface apiInterface : model.getSymbolTable().getInterfaces()) { if (apiInterface.getSimpleName().equals("Gopher")) { GoGapicSurfaceTransformerTest.apiInterface = apiInterface; break; } } ConfigProto configProto = CodegenTestUtil.readConfig( model.getDiagReporter().getDiagCollector(), locator, new String[] {"myproto_gapic.yaml"}); productConfig = GapicProductConfig.create(model, configProto, TargetLanguage.GO); if (model.getDiagReporter().getDiagCollector().hasErrors()) { throw new IllegalStateException( model.getDiagReporter().getDiagCollector().getDiags().toString()); } }