@VisitsBefore void visit(Interface endpointInterface) { Interface old = interfaces.put(endpointInterface.getFullName(), endpointInterface); if (old != null) { model .report( Diag.error( endpointInterface.getLocation(), "Duplicate declaration of interface '%s'. Previous location: %s", endpointInterface.getFullName(), old.getLocation().getDisplayString())); for (Method method : endpointInterface.getMethods()) { Method oldMethod = methodByName.put(method.getSimpleName(), method); if (oldMethod != null) { endpointInterface.setMethodByNameMap(ImmutableMap.copyOf(methodByName));
private Method getMethod(Interface apiInterface, String methodName) { Method method = apiInterface.lookupMethod(methodName); if (method == null) { throw new IllegalArgumentException( String.format( "Method %s not found, available: %s", methodName, apiInterface.getMethods())); } return method; } }
@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 void merge(Interface iface) { Api api = iface.getConfig(); if (api == null) { return; } // Get user-defined api version, which is optional. String apiVersion = api.getVersion(); String packageName = iface.getFile().getFullName(); if (Strings.isNullOrEmpty(apiVersion)) { // If version is not provided by user, extract major version from package name. apiVersion = ApiVersionUtil.extractDefaultMajorVersionFromPackageName(packageName); } iface.setConfig(api.toBuilder().setVersion(apiVersion).build()); iface.putAttribute(VersionAttribute.KEY, VersionAttribute.create(apiVersion)); }
@VisitsBefore void normalize(Interface iface) { Api.Builder coreApiBuilder = Api.newBuilder().setName(iface.getFullName()); coreApiBuilder.setSourceContext( SourceContext.newBuilder().setFileName(iface.getFile().getLocation().getDisplayString())); coreApiBuilder.setSyntax(iface.getSyntax()); for (Method method : iface.getReachableMethods()) { com.google.protobuf.Method.Builder coreMethodBuilder = com.google.protobuf.Method.newBuilder() .setName(method.getSimpleName()) .setRequestTypeUrl(generateTypeUrl(method.getInputType())) .setResponseTypeUrl(generateTypeUrl(method.getOutputType())); coreMethodBuilder.setRequestStreaming(method.getRequestStreaming()); coreMethodBuilder.setResponseStreaming(method.getResponseStreaming()); coreMethodBuilder.addAllOptions( DescriptorNormalization.getMethodOptions( method.getOptionFields(), false, includeDefaults)); coreApiBuilder.addMethods(coreMethodBuilder); } coreApiBuilder.addAllOptions( DescriptorNormalization.getOptions(iface.getProto(), includeDefaults)); coreApiBuilder.setVersion(iface.getAttribute(VersionAttribute.KEY).majorVersion()); apis.add(coreApiBuilder.build()); }
/** Returns a list of RPC methods supported by the context. */ public List<Method> getSupportedMethods(Interface apiInterface) { List<Method> simples = new ArrayList<>(apiInterface.getMethods().size()); for (Method method : apiInterface.getMethods()) { if (isSupported(method)) { simples.add(method); } } return simples; }
/** Find the methods that should be generated on the surface when a GAPIC config was given. */ @Nullable private static ImmutableMap<Method, MethodConfigProto> findMethodsToGenerateWithConfigYaml( Interface apiInterface, InterfaceConfigProto interfaceConfigProto, DiagCollector diagCollector) { ImmutableMap.Builder<Method, MethodConfigProto> methodsToSurface = ImmutableMap.builder(); // Get the set of methods defined by the GAPIC config. Only these methods will be generated. for (MethodConfigProto methodConfigProto : interfaceConfigProto.getMethodsList()) { Interface targetInterface = GapicInterfaceConfig.getTargetInterface( apiInterface, methodConfigProto.getRerouteToGrpcInterface()); Method protoMethod = targetInterface.lookupMethod(methodConfigProto.getName()); if (protoMethod == null) { diagCollector.addDiag( Diag.error( SimpleLocation.TOPLEVEL, "method not found: %s", methodConfigProto.getName())); continue; } methodsToSurface.put(protoMethod, methodConfigProto); } if (diagCollector.getErrorCount() > 0) { return null; } return methodsToSurface.build(); }
private void startMergeMethod(Interface including, Method method) { // Check conditions implied by mixin configuration. Method redeclared = including.lookupMethod(method.getSimpleName()); if (redeclared == null) { error( including.getLocation(), "The API '%s' does not redeclare method '%s' as required by the mixin configuration.", including.getFullName(), method); return; } if (!method.getInputType().equals(redeclared.getInputType())) { error( redeclared.getLocation(), "The method '%s' must have request type '%s' as required by the mixin configuration.", redeclared, method.getInputMessage()); return; } if (!method.getOutputType().equals(redeclared.getOutputType())) { error( redeclared.getLocation(), "The method '%s' must have response type '%s' as required by the mixin configuration.", redeclared, method.getOutputType()); return; } // Add attribute. redeclared.addAttribute(ImplementsAttribute.KEY, ImplementsAttribute.create(method)); }
@Override public String toString() { return "api " + getFullName(); }
private ViewModel buildClientViewModel(GapicInterfaceContext context) { SurfaceNamer namer = context.getNamer(); String name = namer.getApiWrapperClassName(context.getInterfaceConfig()); context .getImportTypeTable() .getAndSaveNicknameFor( PhpPackageUtil.getFullyQualifiedName( namer.getGapicImplNamespace(), namer.getApiWrapperClassImplName(context.getInterfaceConfig()))); DynamicLangXApiSubclassView.Builder apiClass = DynamicLangXApiSubclassView.newBuilder(); apiClass.templateFileName(API_TEMPLATE_FILENAME); apiClass.protoFilename(context.getInterface().getFile().getSimpleName()); apiClass.name(name); apiClass.parentName(namer.getApiWrapperClassImplName(context.getInterfaceConfig())); apiClass.fileHeader(fileHeaderTransformer.generateFileHeader(context)); String outputPath = pathMapper.getOutputPath(context.getInterface().getFullName(), context.getProductConfig()); apiClass.outputPath(outputPath + "/" + name + ".php"); return apiClass.build(); }
/** * Analyze any mixins and attach attributes related to interfaces and methods which are used by * the main merging phase. */ @Override public void startMerging() { for (Api api : getModel().getServiceConfig().getApisList()) { // Resolve the including interface. Interface including = resolveInterface( api.getName(), MessageLocationContext.create(api, Api.NAME_FIELD_NUMBER)); // Process each mixin declaration. for (Mixin mixin : api.getMixinsList()) { Interface included = resolveInterface( mixin.getName(), MessageLocationContext.create(mixin, Mixin.NAME_FIELD_NUMBER)); if (including == null || included == null) { // Errors have been reported. continue; } // Attach the mixin attribute. MixinAttribute mixinAttr = MixinAttribute.create(included, mixin); including.addAttribute(MixinAttribute.KEY, mixinAttr); // Process each method in the included interface. for (Method method : included.getMethods()) { startMergeMethod(including, method); } } } }
apiImplClass.protoFilename(context.getInterface().getFile().getSimpleName()); String implName = namer.getApiWrapperClassImplName(context.getInterfaceConfig()); apiImplClass.name(implName); apiImplClass.serviceHostname(productServiceConfig.getServiceHostname(serviceAddress)); apiImplClass.servicePort(productServiceConfig.getServicePort(serviceAddress)); apiImplClass.serviceTitle(productServiceConfig.getTitle(context.getInterface().getModel())); apiImplClass.authScopes(productServiceConfig.getAuthScopes(context.getInterface().getModel())); apiImplClass.interfaceKey(context.getInterface().getFullName()); String grpcClientTypeName = namer.getAndSaveNicknameForGrpcClientTypeName(
/** * Returns the methods reachable with the active scoper. */ public Iterable<Method> getReachableMethods() { return getModel().reachable(methods); }
@Override public String getFileSimpleName() { return protoInterface.getFile().getSimpleName(); }
@Test public void mergesWithApiVersion() throws Exception { createApi("service_with_version.proto"); model.setConfigSources( testConfig.getApiYamlConfigSources( model.getDiagReporter().getDiagCollector(), ImmutableList.of("service_with_version.yaml"))); StandardSetup.registerStandardConfigAspects(model); model.establishStage(Merged.KEY); Interface iface = getInterface("protiary.test.v2.Storage"); Assert.assertEquals("v2", iface.getAttribute(VersionAttribute.KEY).majorVersion()); }
for (Method method : protoInterface.getReachableMethods()) { for (CollectionPattern collectionPattern : CollectionPattern.getCollectionPatternsFromMethod(method)) {
for (int i = 0; i < serviceProtos.size(); i++) { String childPath = buildPath(null, FileDescriptorProto.SERVICE_FIELD_NUMBER, i); interfacesBuilder.add(Interface.create(this, serviceProtos.get(i), childPath));
@Test public void testGetLocation_service() { Interface service = getService(); verifyLocation("service Service {", service.getLocation()); }
@VisitsBefore void normalize(Interface iface) { Api.Builder coreApiBuilder = Api.newBuilder().setName(iface.getFullName()); coreApiBuilder.setSourceContext( SourceContext.newBuilder().setFileName(iface.getFile().getLocation().getDisplayString())); coreApiBuilder.setSyntax(iface.getSyntax()); for (Method method : iface.getReachableMethods()) { com.google.protobuf.Method.Builder coreMethodBuilder = com.google.protobuf.Method.newBuilder() .setName(method.getSimpleName()) .setRequestTypeUrl(generateTypeUrl(method.getInputType())) .setResponseTypeUrl(generateTypeUrl(method.getOutputType())); coreMethodBuilder.setRequestStreaming(method.getRequestStreaming()); coreMethodBuilder.setResponseStreaming(method.getResponseStreaming()); coreMethodBuilder.addAllOptions( DescriptorNormalization.getMethodOptions( method.getOptionFields(), false, includeDefaults)); coreApiBuilder.addMethods(coreMethodBuilder); } coreApiBuilder.addAllOptions( DescriptorNormalization.getOptions(iface.getProto(), includeDefaults)); coreApiBuilder.setVersion(iface.getAttribute(VersionAttribute.KEY).majorVersion()); apis.add(coreApiBuilder.build()); }
@Override public List<MethodModel> getInterfaceMethods() { ImmutableList.Builder<MethodModel> methodBuilder = ImmutableList.builder(); for (Method method : getInterface().getMethods()) { methodBuilder.add(new ProtoMethodModel(method)); } return methodBuilder.build(); }