private MethodContext buildMethodContext(MethodDescriptorProto methodProto, ProtoTypeMap typeMap) { MethodContext methodContext = new MethodContext(); methodContext.methodName = lowerCaseFirst(methodProto.getName()); methodContext.inputType = typeMap.toJavaTypeName(methodProto.getInputType()); methodContext.outputType = typeMap.toJavaTypeName(methodProto.getOutputType()); methodContext.deprecated = methodProto.getOptions() != null && methodProto.getOptions().getDeprecated(); methodContext.isManyInput = methodProto.getClientStreaming(); methodContext.isManyOutput = methodProto.getServerStreaming(); if (!methodProto.getClientStreaming() && !methodProto.getServerStreaming()) { methodContext.reactiveCallsMethodName = "oneToOne"; methodContext.grpcCallsMethodName = "asyncUnaryCall"; } if (!methodProto.getClientStreaming() && methodProto.getServerStreaming()) { methodContext.reactiveCallsMethodName = "oneToMany"; methodContext.grpcCallsMethodName = "asyncServerStreamingCall"; } if (methodProto.getClientStreaming() && !methodProto.getServerStreaming()) { methodContext.reactiveCallsMethodName = "manyToOne"; methodContext.grpcCallsMethodName = "asyncClientStreamingCall"; } if (methodProto.getClientStreaming() && methodProto.getServerStreaming()) { methodContext.reactiveCallsMethodName = "manyToMany"; methodContext.grpcCallsMethodName = "asyncBidiStreamingCall"; } return methodContext; }
private String generateMethod(MethodDescriptorProto method, String inPutType, String outPutType, String methodName, String inputValue) { String methodStr = "public " + outPutType + " " + methodName + "(" + inPutType + " " + inputValue + ");"; boolean isClientStream = !method.getServerStreaming() && method.getClientStreaming(); boolean isBidiStream = method.getServerStreaming() && method.getClientStreaming(); boolean isServerStream = method.getServerStreaming() && !method.getClientStreaming(); if (isClientStream || isBidiStream) { methodStr = "public " + inPutType + " " + methodName + "(" + outPutType + " responseObserver);"; } else if (isServerStream) { methodStr = "public void " + methodName + "(" + inPutType + " " + inputValue + "," + outPutType + " responseObserver);"; } return methodStr; }
private MethodDescriptorProto generateMethod(Method method) { MethodDescriptorProto.Builder builder = MethodDescriptorProto.newBuilder(); builder.setName(method.getName()); builder.setInputType(getTypeName(method.getRequestTypeUrl())); builder.setOutputType(getTypeName(method.getResponseTypeUrl())); builder.setOptions(generateMethodOptions(method)); builder.setClientStreaming(method.getRequestStreaming()); // protoc set serverStreaming field as false for legacy streaming options, // but google.protobuf.Method set the responseStreaming field to true for both new and legacy // streaming setup. So we need to distinguish streaming style while generating // MethodDescriptorProto. // But we cannot distinguish if the new and old styles are both set which should be rare case. if (method.getResponseStreaming() && isLegacyStreaming(method)) { builder.setServerStreaming(false); } else { builder.setServerStreaming(method.getResponseStreaming()); } return builder.build(); }
protected void decompile(ServiceDescriptorProto serviceDescriptor) throws IOException { indentedFormat("service %s {", serviceDescriptor.getName()); indent++; if (serviceDescriptor.hasOptions()) { decompileOptions(serviceDescriptor.getOptions()); } for (MethodDescriptorProto methodDescriptor : serviceDescriptor.getMethodList()) { indentedFormat("rpc %s (%s) returns (%s)", methodDescriptor.getName(), methodDescriptor.getInputType(), methodDescriptor.getOutputType()); if (methodDescriptor.hasOptions()) { write("{ "); indent++; decompileOptions(methodDescriptor.getOptions()); indent--; indentedFormat("}"); } else { write(";"); } } indent--; indentedFormat("}"); }
@Override protected List<String> collectFileData() { String className = super.getClassName(); String packageName = super.getSourcePackageName().toLowerCase(); List<String> fileData = Lists.newArrayList(); fileData.add("package " + packageName + ";"); fileData.add("public interface " + className + "{"); for (MethodDescriptorProto method : serviceMethods) { String outPutType = method.getOutputType(); String inPutType = method.getInputType(); String methodName = method.getName(); inPutType = CommonUtils.findPojoTypeFromCache(inPutType, pojoTypeCache); outPutType = CommonUtils.findPojoTypeFromCache(outPutType, pojoTypeCache); String stream = generateGrpcStream(method, inPutType, outPutType); if (method.getServerStreaming() || method.getClientStreaming()) { outPutType = "io.grpc.stub.StreamObserver<" + outPutType + ">"; } String inputValue = CommonUtils.findNotIncludePackageType(inPutType).toLowerCase(); if (method.getClientStreaming()) { inPutType = "io.grpc.stub.StreamObserver<" + inPutType + ">"; } if (stream != null) fileData.add(stream); String methodStr = generateMethod(method, inPutType, outPutType, methodName, inputValue); fileData.add(methodStr); } fileData.add("}"); return fileData; }
private Interface(ProtoFile parent, ServiceDescriptorProto proto, String path) { super(parent, proto.getName(), path); this.proto = proto; // Build methods. ImmutableList.Builder<Method> methodsBuilder = ImmutableList.builder(); List<MethodDescriptorProto> methodProtos = proto.getMethodList(); for (int i = 0; i < methodProtos.size(); i++) { String childPath = buildPath(path, ServiceDescriptorProto.METHOD_FIELD_NUMBER, i); methodsBuilder.add(Method.create(this, methodProtos.get(i), childPath)); } methods = methodsBuilder.build(); }
private Method(Interface parent, MethodDescriptorProto proto, String path) { super(parent, proto.getName(), path); this.isDeprecated = proto.getOptions().getDeprecated(); this.descriptor = new MethodDescriptor(proto); this.requestStreaming = proto.getClientStreaming(); this.responseStreaming = proto.getServerStreaming(); }
private MethodDescriptor(final MethodDescriptorProto proto, final FileDescriptor file, final ServiceDescriptor parent, final int index) throws DescriptorValidationException { this.index = index; this.proto = proto; this.file = file; service = parent; fullName = parent.getFullName() + '.' + proto.getName(); file.pool.addSymbol(this); }
private MethodDescriptor (final MethodDescriptorProto proto, final FileDescriptor file, final ServiceDescriptor parent, final int index) throws DescriptorValidationException { this.index = index; this.proto = proto; this.file = file; service = parent; fullName = parent.getFullName () + '.' + proto.getName (); file.pool.addSymbol (this); }
private void makeCanonicalService(final ServiceDescriptorProto.Builder service, final ServiceDescriptor serviceDescriptor) { for (final MethodDescriptorProto.Builder method : service.getMethodBuilderList()) { final MethodDescriptor methodDescriptor = serviceDescriptor.findMethodByName(method.getName()); method.setInputType(ensureLeadingDot(methodDescriptor.getInputType().getFullName())); method.setOutputType(ensureLeadingDot(methodDescriptor.getOutputType().getFullName())); } }
@Override public void exitMethodStatement(final MethodStatementContext ctx) { final MethodDescriptorProto.Builder methodBuilder = MethodDescriptorProto.Builder.class.cast(scopes.getProtoBuilder()); methodBuilder.setName(ctx.identifier().getText()).setInputType(ctx.extendedId(0).getText()) .setOutputType(ctx.extendedId(1).getText()); scopes.popScope(); }
private void buildAllOptions(final ServiceDescriptorProto.Builder proto) { if (!buildOptions(proto.getOptionsBuilder())) { proto.clearOptions(); } for (final MethodDescriptorProto.Builder methodProto : proto.getMethodBuilderList()) { if (!buildOptions(methodProto.getOptionsBuilder())) { methodProto.clearOptions(); } } }
private boolean isCanonical(final ServiceDescriptorProto serviceProto) { if (serviceProto.hasOptions() && serviceProto.getOptions().getUninterpretedOptionCount() > 0) { return false; } for (final MethodDescriptorProto methodProto : serviceProto.getMethodList()) { if (methodProto.hasOptions() && methodProto.getOptions().getUninterpretedOptionCount() > 0) { return false; } } return true; }
public void handle(ServiceDescriptorProto service) throws IOException { ImmutableList.Builder<ServiceHandlerData.Method> methods = ImmutableList.builder(); for (MethodDescriptorProto method : service.getMethodList()) { ServiceHandlerData.Method methodData = new ServiceHandlerData.Method( method.getName(), CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, method.getName()), types.lookup(method.getInputType()).toString(), types.lookup(method.getOutputType()).toString()); methods.add(methodData); } String fullName = Joiner.on('.').skipNulls().join(protoPackage, service.getName()); ServiceHandlerData.Service serviceData = new ServiceHandlerData.Service( service.getName(), fullName, methods.build()); ServiceHandlerData data = new ServiceHandlerData(javaPackage, multipleFiles, serviceData); String template = Resources.toString(Resources.getResource(this.getClass(), "service_class.mvel"), Charsets.UTF_8); String serviceFile = (String) TemplateRuntime.eval(template, ImmutableMap.<String, Object>of("handler", data)); CodeGeneratorResponse.Builder response = CodeGeneratorResponse.newBuilder(); CodeGeneratorResponse.File.Builder file = CodeGeneratorResponse.File.newBuilder(); file.setContent(serviceFile); file.setName(javaPackage.replace('.', '/') + '/' + service.getName() + ".java"); if (!multipleFiles) { file.setName(javaPackage.replace('.', '/') + '/' + outerClassName + ".java"); file.setInsertionPoint("outer_class_scope"); } response.addFile(file); response.build().writeTo(output); }
public void setServiceMethods(List<MethodDescriptorProto> serviceMethods) { this.serviceMethods = serviceMethods; }
public static List<Option> getOptions(MethodDescriptorProto descriptor) { return getOptions(descriptor, true); }
public static List<Option> getOptions(MethodDescriptorProto descriptor, boolean withDefaults) { return toCoreOptions(maybeCombineOptionsWithDefault(withDefaults, descriptor.getOptions().getAllFields(), DEFAULT_METHOD_OPTIONS)); }
/** * Creates a method with {@link MethodDescriptorProto}. */ public static Method create(Interface parent, MethodDescriptorProto proto, String path) { return new Method(parent, proto, path); }
private MethodDescriptor(MethodDescriptorProto methodProto) { this.inputTypeName = methodProto.getInputType(); this.outputTypeName = methodProto.getOutputType(); this.optionFields = ImmutableMap.copyOf(methodProto.getOptions().getAllFields()); this.methodProto = methodProto; }
@Accepts protected void accept(MethodDescriptorProto.Builder method) { pushParent(BuilderVisitorNodeInfo.create(method, currentFile)); visit(method.getOptionsBuilder()); popExpectedParent(method); }
private void restify(MethodKind httpKind, String simpleName, String template) { Model model = Model.create(FileDescriptorSet.getDefaultInstance()); model.setServiceConfig( ConfigSource.newBuilder(Service.getDefaultInstance()) .setValue( Service.getDescriptor().findFieldByNumber(Service.CONFIG_VERSION_FIELD_NUMBER), null, UInt32Value.newBuilder().setValue(configVersion).build(), new SimpleLocation("from test")) .build()); HttpConfigAspect aspect = HttpConfigAspect.create(model); ProtoFile file = ProtoFile.create( model, FileDescriptorProto.getDefaultInstance(), true, ExtensionPool.EMPTY); Interface iface = Interface.create(file, ServiceDescriptorProto.getDefaultInstance(), ""); Method method = Method.create(iface, MethodDescriptorProto.newBuilder().setName(simpleName).build(), ""); RestMethod restMethod; ImmutableList<PathSegment> path = parse(model, template); if (!model.getDiagReporter().getDiagCollector().getDiags().isEmpty()) { restMethod = RestMethod.create(method, RestKind.CUSTOM, "*error*", "*error*", null); } else { HttpRule httpRule = HttpRule.getDefaultInstance(); HttpAttribute httpConfig = new HttpAttribute( httpRule, httpKind, MessageType.create(file, Empty.getDescriptor().toProto(), "", ExtensionPool.EMPTY), path, "", false, ImmutableList.<HttpAttribute>of(), false); RestAnalyzer analyzer = new RestAnalyzer(aspect); restMethod = analyzer.analyzeMethod(method, httpConfig); } PrintWriter pw = testOutput(); pw.print(httpKind.toString()); pw.print(" "); pw.print(simpleName); pw.print(" "); pw.print(template.isEmpty() ? "(empty)" : template); pw.println(); pw.println(Strings.repeat("=", 70)); pw.printf("Rest Kind: %s\n", restMethod.getRestKind()); pw.printf( "Version: %s\n", restMethod.getVersion().isEmpty() ? "(empty)" : restMethod.getVersion()); pw.printf( "Version with default: %s\n", restMethod.getVersionWithDefault().isEmpty() ? "(empty)" : restMethod.getVersionWithDefault()); pw.printf( "Simple collection: %s\n", restMethod.getRestCollectionName().isEmpty() ? "(empty)" : restMethod.getSimpleRestCollectionName()); pw.printf( "Versioned collection: %s\n", restMethod.getRestCollectionName().isEmpty() ? "(empty)" : restMethod.getRestCollectionName()); pw.printf("Base collection: %s\n", restMethod.getBaseRestCollectionName().isEmpty() ? "(empty)" : restMethod.getBaseRestCollectionName()); pw.printf("Custom Name: %s\n", restMethod.getRestKind() == RestKind.CUSTOM ? restMethod.getRestMethodName() : "(null)"); List<Diag> diags = model.getDiagReporter().getDiagCollector().getDiags(); if (diags.size() > 0) { pw.println("Diagnostics:"); for (Diag d : diags) { pw.printf(" %s\n", DiagUtils.getDiagToPrint(d, true)); } } pw.println(); }
/** Convert the descriptor to its protocol message representation. */ @Override public MethodDescriptorProto toProto() { return proto; }
/** See {@link FileDescriptor#setProto}. */ private void setProto(final MethodDescriptorProto proto) { this.proto = proto; }
/** * Convert the descriptor to its protocol message representation. */ public MethodDescriptorProto toProto () { return proto; }
/** * See {@link FileDescriptor#setProto}. */ private void setProto (final MethodDescriptorProto proto) { this.proto = proto; }
public MethodDescriptorProto.Builder addMethod() { final MethodDescriptorProto.Builder builder = currentScope.addMethod(); pushScope(new MethodScope(builder, currentScope)); return builder; }
protected MethodDescriptorProto.Builder addMethod() { throw new RuntimeException(NOT_APPLICABLE_IN_CURRENT_SCOPE); }
@Override protected MethodDescriptorProto.Builder addMethod() { return protoBuilder.addMethodBuilder(); }
private MethodScope(final MethodDescriptorProto.Builder protoBuilder, final Scope<?> parent) { super(protoBuilder, parent); }
public static boolean compareProto(final MethodDescriptorProto me, final @Nullable Object other) { return compareMessage(me, other, new ProtoFilter()); }