public void generateFile(String protoPath) { try { if (pojoTypes == null) { pojoTypes = Maps.newHashMap(); } } finally { if (!new File(protoPath).exists()) { logger.warn("protoPath:" + protoPath + " not exist, it may be in the third party jars, so it can't be generate"); return; } FileDescriptorSet fileDescriptorSet = commondProtoc.invoke(protoPath); for (FileDescriptorProto fdp : fileDescriptorSet.getFileList()) { Pair<String, String> packageClassName = this.packageClassName(fdp.getOptions()); if (packageClassName == null) { continue; } ProtocolStringList dependencyList = fdp.getDependencyList(); for (Iterator<String> it = dependencyList.iterator(); it.hasNext();) { String dependencyPath = discoveryRoot + "/" + it.next(); generateFile(dependencyPath); } doPrint(fdp, packageClassName.getLeft(), packageClassName.getRight()); } } }
/** * * @author liushiming * @param args * @since JDK 1.8 */ public static void main(String[] args) { CommandProtoc commondProtoc = CommandProtoc.configProtoPath( "/Users/liushiming/project/java/saluki/saluki-plugin/saluki-plugin-common/src/test/java/com/quancheng/saluki", new File( "/Users/liushiming/project/java/saluki/saluki-example/saluki-example-api/target/protoc-dependencies")); FileDescriptorSet fileDescriptorSet = commondProtoc.invoke( "/Users/liushiming/project/java/saluki/saluki-plugin/saluki-plugin-common/src/test/java/com/quancheng/saluki/saluki_service.proto"); Map<Integer, UnknownFieldSet.Field> lengthDelimitedList = fileDescriptorSet.getFile(0) .getMessageType(0).getField(0).getOptions().getUnknownFields().asMap(); for (Map.Entry<Integer, UnknownFieldSet.Field> integerFieldEntry : lengthDelimitedList .entrySet()) { for (ByteString byteString : integerFieldEntry.getValue().getLengthDelimitedList()) { System.out.println(integerFieldEntry.getKey() + "--" + byteString.toStringUtf8()); } } System.out.println(fileDescriptorSet); }
/** Creates a resolver which searches the supplied {@link FileDescriptorSet}. */ public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) { ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex = computeDescriptorProtoIndex(descriptorSet); Map<String, FileDescriptor> descriptorCache = new HashMap<>(); ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder(); for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) { try { result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache)); } catch (DescriptorValidationException e) { logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e); continue; } } return new ServiceResolver(result.build()); }
private void processDependencies(FileDescriptorProto fileDescriptor) { logger.debug("Processing deps of descriptor: " + fileDescriptor.getName()); fileDescriptor.getDependencyList().forEach(dep -> { if (!resolvedDescriptors.containsKey(dep) && !requestedDescriptors.contains(dep)) { requestedDescriptors.add(dep); ++outstandingRequests; requestStream.onNext(requestForDescriptor(dep)); } }); --outstandingRequests; if (outstandingRequests == 0) { logger.debug("Retrieved service definition for [{}] by reflection", serviceName); resultFuture.set(FileDescriptorSet.newBuilder() .addAllFile(resolvedDescriptors.values()) .build()); requestStream.onCompleted(); } }
/** Lists the GRPC services - filtered by service name (contains) or method name (contains) */ public static void listServices( Output output, FileDescriptorSet fileDescriptorSet, String protoDiscoveryRoot, Optional<String> serviceFilter, Optional<String> methodFilter, Optional<Boolean> withMessage) { ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet); // Add white-space before the rendered output output.newLine(); for (ServiceDescriptor descriptor : serviceResolver.listServices()) { boolean matchingDescriptor = !serviceFilter.isPresent() || descriptor.getFullName().toLowerCase().contains(serviceFilter.get().toLowerCase()); if (matchingDescriptor) { listMethods(output, protoDiscoveryRoot, descriptor, methodFilter, withMessage); } } }
private void loadService() { LOG.info("Load service definition is starting..."); InputStream in = null; FileDescriptorSet descriptorSet; try { in = ClassHelper.getClassLoader().getResourceAsStream(GrpcConstants.PROTO_DESC_FILENAME); descriptorSet = FileDescriptorSet.parseFrom(in); for (FileDescriptorProto fdp : descriptorSet.getFileList()) { FileDescriptor fd = FileDescriptor.buildFrom(fdp, new FileDescriptor[] {}, true); for (com.google.protobuf.Descriptors.ServiceDescriptor service : fd.getServices()) { addServiceDenifition(service.getName(), fd.getOptions().getJavaPackage() + '.' + service.getFullName()); } } LOG.info("Load service denifition is finished, total {} service are found.", services.size()); } catch (Exception ex) { LOG.error("Load service denifition error happened.", ex); throw new RuntimeException(ex); } finally { IOUtils.closeInputStream(in); } }
/** * Creates a model from a normalized service config, rather than from descriptor and .yaml files. */ public static Model create(Service normalizedConfig) { FileDescriptorSet regeneratedDescriptor = DescriptorGenerator.generate(normalizedConfig); Model model = create(regeneratedDescriptor); // Configured with a stripped Service Service.Builder builder = normalizedConfig.toBuilder(); ImmutableList.Builder<Api> strippedApis = ImmutableList.builder(); for (Api api : normalizedConfig.getApisList()) { strippedApis.add( Api.newBuilder().setName(api.getName()).setVersion(api.getVersion()).build()); } // NOTE: Documentation may still contain text from the original protos. builder.clearEnums(); builder.clearTypes(); builder.clearApis(); builder.addAllApis(strippedApis.build()); ConfigSource strippedConfig = ConfigSource.newBuilder(builder.build()).build(); model.setConfigSources(ImmutableList.of(strippedConfig)); return model; }
private FileDescriptorSet parseFileDescriptors( ToolOptions options, ModelBuildOverrides registry, DiagCollector diagCollector) { String fileDescriptor = options.get(ToolOptions.DESCRIPTOR_SET); if (!Strings.isNullOrEmpty(fileDescriptor)) { try { return parseFileAsDescriptorSet(FileWrapper.from(fileDescriptor), registry, diagCollector); } catch (IOException ex) { diagCollector.addDiag( Diag.error( SimpleLocation.TOPLEVEL, "Cannot read FileDescriptorSet file '%s': %s", fileDescriptor, ex.getMessage())); return null; } } else { return parseFileAsDescriptorSet( options.get(ToolOptions.DESCRIPTOR_SET_CONTENTS), registry, diagCollector); } }
private FileDescriptorSet parseFileAsDescriptorSet( FileWrapper inputFile, ModelBuildOverrides registry, DiagCollector diagCollector) { ByteString extensionFile = inputFile.getFileContents(); try { return FileDescriptorSet.parseFrom(extensionFile, registry.getPlatformExtensions()); } catch (InvalidProtocolBufferException e) { diagCollector.addDiag( Diag.error( SimpleLocation.TOPLEVEL, "Cannot read file descriptor file '%s': %s", inputFile.getFilename(), e.getMessage())); return null; } }
@Test public void resolvesWithErrors() { // Modify the descriptor injecting some errors. FileDescriptorSet.Builder builder = descriptors.toBuilder(); builder .getFileBuilder(0) .getMessageTypeBuilder(0) .getFieldBuilder(1) // required N n .setTypeName("undef_N"); builder .getFileBuilder(0) .getMessageTypeBuilder(0) .getFieldBuilder(2) // optional E e .setTypeName("undef_E"); Model testApi = Model.create(builder.build()); testApi.registerProcessor(new Resolver()); Truth.assertThat(testApi.establishStage(Resolved.KEY)).isFalse(); Truth.assertThat(testApi.getDiagReporter().getDiagCollector().getErrorCount()).isEqualTo(2); assertThat(testApi.getDiagReporter().getDiagCollector().getDiags().get(0).toString()) .contains("undef_N"); assertThat(testApi.getDiagReporter().getDiagCollector().getDiags().get(1).toString()) .contains("undef_E"); }
@Override protected Map<String, String> getDocStringsFromFiles(Map<String, byte[]> files) { return files.entrySet().stream() .flatMap(entry -> { try { FileDescriptorSet descriptors = FileDescriptorSet.parseFrom(entry.getValue()); return descriptors.getFileList().stream(); } catch (IOException e) { logger.info("Could not parse file at '{}', skipping. " + "Is the file a protobuf descriptor file?", entry.getKey()); return Stream.empty(); } }) .flatMap(f -> parseFile(f).entrySet().stream()) .collect(toImmutableMap(Entry::getKey, Entry::getValue, (entry, unused) -> entry)); }
private FileDescriptorSet.Builder addDescriptorToFileSet(FileDescriptorSet.Builder builder, Descriptor descriptor, Set<FileDescriptorProto> fileProtoSet) { List<? extends FileDescriptorProtoOrBuilder> fileList = builder.getFileOrBuilderList(); final FileDescriptor file = descriptor.getFile(); FileDescriptorProto proto = file.toProto(); if (fileList.contains(proto)) { return builder; } builder.addFile(proto); for (FileDescriptor dependency : file.getDependencies()) { proto = dependency.toProto(); if (!fileList.contains(proto)) { builder.addFile(proto); } } return builder; }
/** * Returns a map from descriptor proto name as found inside the descriptors to protos. */ private static ImmutableMap<String, FileDescriptorProto> computeDescriptorProtoIndex( FileDescriptorSet fileDescriptorSet) { ImmutableMap.Builder<String, FileDescriptorProto> resultBuilder = ImmutableMap.builder(); for (FileDescriptorProto descriptorProto : fileDescriptorSet.getFileList()) { resultBuilder.put(descriptorProto.getName(), descriptorProto); } return resultBuilder.build(); }
/** Invokes protoc and returns a {@link FileDescriptorSet} used for discovery. */ private static FileDescriptorSet getFileDescriptorSet(ProtoConfiguration protoConfig) { try { return ProtocInvoker.forConfig(protoConfig).invoke(); } catch (ProtocInvocationException e) { throw new RuntimeException("Failed to invoke the protoc binary", e); } }
/** * Returns a {@link FileDescriptorSet} containing all the transitive dependencies of the supplied * service, as provided by the remote server. */ public ListenableFuture<FileDescriptorSet> lookupService(String serviceName) { LookupServiceHandler rpcHandler = new LookupServiceHandler(serviceName); StreamObserver<ServerReflectionRequest> requestStream = ServerReflectionGrpc.newStub(channel) .withDeadlineAfter(LOOKUP_RPC_DEADLINE_MS, TimeUnit.MILLISECONDS) .serverReflectionInfo(rpcHandler); return rpcHandler.start(requestStream); }
ListenableFuture<FileDescriptorSet> start( StreamObserver<ServerReflectionRequest> requestStream) { this.requestStream = requestStream; requestStream.onNext(requestForSymbol(serviceName)); ++outstandingRequests; return resultFuture; }
private FileDescriptorSet generate() { FileDescriptorSet.Builder setBuilder = FileDescriptorSet.newBuilder(); for (Map.Entry<String, FileContents> entry : contentsByFile.entrySet()) { FileContents contents = entry.getValue(); String fileName = entry.getKey(); if (!contents.apis.isEmpty() || !contents.types.isEmpty() || !contents.enums.isEmpty()) { setBuilder.addFile(generateFile(fileName, contents)); } } return setBuilder.build(); }
/** * Creates a new model based on the given file descriptor, list of source file names and list of * experiments to be enabled for the model. */ public static Model create( FileDescriptorSet proto, Iterable<String> sources, Experiments experiments, ExtensionPool extensionPool) { DiagCollector diagCollector = new BoundedDiagCollector(); return new Model( proto, sources, experiments, extensionPool, diagCollector, new DiagSuppressor(diagCollector)); }
/** * Creates a new model based on the given file descriptor, list of source file names and list of * experiments to be enabled for the model. */ public static Model create( FileDescriptorSet proto, Iterable<String> sources, Experiments experiments, ExtensionPool extensionPool, DiagCollector diagCollector) { return new Model( proto, sources, experiments, extensionPool, diagCollector, new DiagSuppressor(diagCollector)); }
public static Model create( FileDescriptorSet proto, Iterable<String> sources, Experiments experiments, ExtensionPool extensionPool, DiagCollector diagCollector, DiagSuppressor diagSuppressor) { return new Model(proto, sources, experiments, extensionPool, diagCollector, diagSuppressor); }
/** * Creates a new model based on the given file descriptor set and list of source file names. The * file descriptor set is self-contained and contains the descriptors for the source files as well * as for all dependencies. */ public static Model create(FileDescriptorSet proto, Iterable<String> sources) { DiagCollector diagCollector = new BoundedDiagCollector(); return new Model( proto, sources, ExperimentsImpl.none(), ExtensionPool.EMPTY, diagCollector, new DiagSuppressor(diagCollector)); }
/** Creates an model where all protos in the descriptor are considered to be sources. */ public static Model create(FileDescriptorSet proto) { DiagCollector diagCollector = new BoundedDiagCollector(); return new Model( proto, null, ExperimentsImpl.none(), ExtensionPool.EMPTY, diagCollector, new DiagSuppressor(diagCollector)); }
public Builder setFileDescriptorSet(FileDescriptorSet descriptorSet) { Preconditions.checkState(this.descriptor == null, "can only add one FileDescriptorSet"); this.descriptor = descriptorSet; for (FileDescriptorProto fileDescriptor : descriptorSet.getFileList()) { add(fileDescriptor); } return this; }
/** * Builds an {@link Model} object, using the settings from {@link ToolOptions} and functional * overrides from {@link ModelBuildOverrides}. If the build fails, will return a 'null' model and * a {@link DiagCollector} containing all warnings and errors up to the failure point. */ public ModelBuildResult setup( ToolOptions options, ModelBuildOverrides registry, String builtDataPath) { DiagCollector diagCollector = new BoundedDiagCollector(); Set<FileWrapper> protoFiles = parseConfigFiles(options, builtDataPath, diagCollector); List<String> protoFileNames = Lists.newArrayList(); for (FileWrapper protoFile : protoFiles) { protoFileNames.add(protoFile.getFilename()); } FileDescriptorSet descriptor = parseFileDescriptors(options, registry, diagCollector); ExtensionPool userExtensionPool = parseExtensionDescriptor(options, registry, diagCollector); if (diagCollector.hasErrors()) { return ModelBuildResult.create(null, diagCollector); } Model model = Model.create( descriptor, protoFileNames, new ExperimentsImpl(options.get(ToolOptions.EXPERIMENTS)), userExtensionPool, diagCollector); if (diagCollector.hasErrors()) { return ModelBuildResult.create(null, diagCollector); } registry.registerProcessors(model); model.setDataPath(builtDataPath); ToolUtil.setupModelConfigs(model, protoFiles); if (diagCollector.hasErrors()) { return ModelBuildResult.create(null, diagCollector); } registry.registerAspects(model); return ModelBuildResult.create(model, diagCollector); }
@Test public void resolvesOkWithPartialNames() { // Modify the descriptor. Protoc generates full names, and // we want to check whether we can also deal with partial names. FileDescriptorSet.Builder builder = descriptors.toBuilder(); builder .getFileBuilder(0) .getMessageTypeBuilder(0) .getFieldBuilder(1) // required N n .setTypeName("N"); Model testApi = Model.create(builder.build()); testApi.registerProcessor(new Resolver()); Truth.assertThat(testApi.establishStage(Resolved.KEY)).isTrue(); Truth.assertThat(testApi.getDiagReporter().getDiagCollector().hasErrors()).isFalse(); }
public static void main(String[] args) throws IOException { ProtobufDecompiler decompiler = new ProtobufDecompiler((Appendable)System.out); FileDescriptorSet set; try (FileInputStream istr = new FileInputStream(args[0])) { set = FileDescriptorSet.parseFrom(istr); } decompiler.decompile(set); }
public void decompile(FileDescriptorSet setDescriptor) throws IOException { for (FileDescriptorProto fileDescriptor : setDescriptor.getFileList()) { newline(); format("===== %s =====", fileDescriptor.getName()); newline(); decompile(fileDescriptor); } }
protected ProtobufRowConverter converter(Group g) throws Exception { AISToProtobuf a2p = new AISToProtobuf(ProtobufRowFormat.Type.GROUP_MESSAGE); a2p.addGroup(g); FileDescriptorSet set = a2p.build(); FileDescriptor gdesc = FileDescriptor.buildFrom(set.getFile(0), ProtobufStorageDescriptionHelper.DEPENDENCIES); return ProtobufRowConverter.forGroup(g, gdesc); }
@Test public void testAIS() throws Exception { FileDescriptorSet set = null; for (File file : files) { String sql = fileContents(file); runDDL(sql); AISToProtobuf ais2p = new AISToProtobuf(ProtobufRowFormat.Type.GROUP_MESSAGE, set); for (Group group : ais().getGroups().values()) { if (group.getName().getSchemaName().equals(SCHEMA)) { ais2p.addGroup(group); } } set = ais2p.build(); StringBuilder proto = new StringBuilder(); new ProtobufDecompiler(proto).decompile(set); String actual = proto.toString(); String expected = null; File expectedFile = changeSuffix(file, ".proto"); if (expectedFile.exists()) { expected = fileContents(expectedFile); } String fullCaseName = file.getName().replace("\\.sql$", ""); if (expected == null) { fail(fullCaseName + " no expected result given. actual='" + actual + "'"); } else { assertEqualsWithoutPattern(fullCaseName, expected, actual, UUID_REGEX); } } }
protected ProtobufRowDataConverter converter(Group g) throws Exception { AISToProtobuf a2p = new AISToProtobuf(ProtobufRowFormat.Type.GROUP_MESSAGE); a2p.addGroup(g); FileDescriptorSet set = a2p.build(); if (false) { new ProtobufDecompiler((Appendable)System.out).decompile(set); } FileDescriptor gdesc = FileDescriptor.buildFrom(set.getFile(0), ProtobufStorageDescriptionHelper.DEPENDENCIES); return ProtobufRowDataConverter.forGroup(g, gdesc); }
private DynamicSchema(FileDescriptorSet fileDescSet) throws DescriptorValidationException { mFileDescSet = fileDescSet; Map<String,FileDescriptor> fileDescMap = init(fileDescSet); Set<String> msgDupes = new HashSet<String>(); Set<String> enumDupes = new HashSet<String>(); for (FileDescriptor fileDesc : fileDescMap.values()) { for (Descriptor msgType : fileDesc.getMessageTypes()) addMessageType(msgType, null, msgDupes, enumDupes); for (EnumDescriptor enumType : fileDesc.getEnumTypes()) addEnumType(enumType, null, enumDupes); } for (String msgName : msgDupes) mMsgDescriptorMapShort.remove(msgName); for (String enumName : enumDupes) mEnumDescriptorMapShort.remove(enumName); }
/** * Builds a dynamic schema * * @return the schema object * @throws DescriptorValidationException */ public DynamicSchema build() throws DescriptorValidationException { FileDescriptorSet.Builder fileDescSetBuilder = FileDescriptorSet.newBuilder(); fileDescSetBuilder.addFile(mFileDescProtoBuilder.build()); fileDescSetBuilder.mergeFrom(mFileDescSetBuilder.build()); return new DynamicSchema(fileDescSetBuilder.build()); }
@Test public void testFileSetSerialization() throws Exception { final FileDescriptorSet fileDescriptorSet = FileDescriptorSetBuilder.newBuilder() .addDescriptor(TestAllTypes.getDescriptor()).build(); String fileDescriptorSetText = TextFormat.printToString(fileDescriptorSet); FileDescriptorSet.Builder fileDescriptorSetBuilder = FileDescriptorSet.newBuilder(); TextFormat.merge(fileDescriptorSetText, fileDescriptorSetBuilder); FileDescriptorSet actualFileSet = fileDescriptorSetBuilder.build(); assertThat(actualFileSet, equalTo(fileDescriptorSet)); }
private FileDescriptorCache(final FileDescriptorSet fileDescriptorSet) { if (fileDescriptorSet == null) { throw new NullPointerException(); } this.fileCache = new LinkedHashMap<String, FileDescriptor>(buildFilesFrom(fileDescriptorSet)); this.descriptorCache = new HashMap<String, Descriptor>(); updateDecsriptorCache(); }
/** Generates a FileDescriptorSet for the specified normalized service config. */ public static FileDescriptorSet generate(Service normalizedService) { DescriptorGenerator generator = new DescriptorGenerator(); generator.analyzeService(normalizedService); return generator.generate(); }
public static final ExtensionPool create(FileDescriptorSet extensionDescriptor) { return new Builder().setFileDescriptorSet(extensionDescriptor).build(); }
private ExtensionPool( FileDescriptorSet descriptor, ImmutableMap<String, ImmutableMultimap<String, Extension>> extensions) { this.descriptor = descriptor; this.extensions = extensions; }
public FileDescriptorSet getDescriptor() { return descriptor; }