/** 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()); }
/** * Recursively constructs file descriptors for all dependencies of the supplied proto and returns * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the * descriptorCache argument across calls. */ private static FileDescriptor descriptorFromProto( FileDescriptorProto descriptorProto, ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex, Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException { // First, check the cache. String descritorName = descriptorProto.getName(); if (descriptorCache.containsKey(descritorName)) { return descriptorCache.get(descritorName); } // Then, fetch all the required dependencies recursively. ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder(); for (String dependencyName : descriptorProto.getDependencyList()) { if (!descriptorProtoIndex.containsKey(dependencyName)) { throw new IllegalArgumentException("Could not find dependency: " + dependencyName); } FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName); dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache)); } // Finally, construct the actual descriptor. FileDescriptor[] empty = new FileDescriptor[0]; return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty)); }
public void testInvalidPublicDependency() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto").build(); FileDescriptorProto barProto = FileDescriptorProto.newBuilder() .setName("boo.proto") .addDependency("foo.proto") .addPublicDependency(1) // Error, should be 0. .build(); FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); try { Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[] {fooFile}); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { assertTrue( e.getMessage().indexOf("Invalid public dependency index.") != -1); } }
private FileDescriptor getDeepCanonicalFileDescriptor(final FileDescriptor file, final boolean forceRebuild) throws DescriptorValidationException { if (!forceRebuild && isDeeplyCanonical(file)) { return file; } final FileDescriptor[] dependencies = new FileDescriptor[file.getDependencies().size()]; int i = 0; for (final FileDescriptor dependency : file.getDependencies()) { dependencies[i++] = getDeepCanonicalFileDescriptor(dependency, forceRebuild); } final FileDescriptorProto proto = isCanonical(file) ? file.toProto() : makeCanonicalProto(file); return buildFileDescriptorWithReserializedProto(proto, dependencies); }
@Parameters(name = "{index}:{0}") public static Collection<Object[]> data() { return ImmutableList.<Object[]>of(new Object[] {"NonUniqueFieldName1.proto", NonUniqueException.class, "field name"}, new Object[] {"NonUniqueFieldNumber1.proto", NonUniqueException.class, "field number"}, new Object[] {"FieldsInExtensionRange1.proto", FieldInExtensionRangeException.class, ""}, new Object[] {"InvalidExtensionRange1.proto", InvalidExtensionRange.class, ""}, new Object[] {"InvalidExtensionRange2.proto", InvalidExtensionRange.class, ""}, new Object[] {"InvalidExtensionRange3.proto", InvalidExtensionRange.class, ""}, new Object[] {"UnresolvedTypeName1.proto", UnresolvedTypeNameException.class, ""}, new Object[] {"UnresolvedTypeName2.proto", UnresolvedTypeNameException.class, ""}, new Object[] {"UnresolvedTypeName3.proto", UnresolvedTypeNameException.class, ""} , new Object[] {"UnresolvedTypeName4.proto", DescriptorValidationException.class, ""}, new Object[] {"UnresolvedTypeName5.proto", DescriptorValidationException.class, ""} , new Object[] {"NonUniqueExtensionName1.proto", DescriptorValidationException.class, ""}, new Object[] {"NonUniqueExtensionNumber1.proto", NonUniqueExtensionNumber.class, ""}); }
private static FileDescriptor buildDescriptor( String name, Map<String, FileDescriptor> descriptors, Map<String, FileDescriptorProto> protos) throws DescriptorValidationException { FileDescriptor file = descriptors.get(name); if (file != null) { return file; } FileDescriptorProto proto = protos.get(name); FileDescriptor[] deps = new FileDescriptor[proto.getDependencyCount()]; for (int i = 0; i < proto.getDependencyCount(); i++) { deps[i] = buildDescriptor(proto.getDependency(i), descriptors, protos); } file = FileDescriptor.buildFrom(proto, deps); descriptors.put(name, file); return file; }
/** * Builds the new {@code SoyFileSet}. * * @return The new {@code SoyFileSet}. */ public SoyFileSet build() { try { if (!protoTypeProviderBuilder.isEmpty()) { Set<SoyTypeProvider> typeProviders = ImmutableSet.<SoyTypeProvider>of(protoTypeProviderBuilder.build()); localTypeRegistry = new SoyTypeRegistry(typeProviders); } } catch (DescriptorValidationException | IOException ex) { throw new RuntimeException("Malformed descriptor set", ex); } return new SoyFileSet( coreDependencies.apiCallScope, coreDependencies.soyValueConverter, localTypeRegistry == null ? coreDependencies.typeRegistry : localTypeRegistry, coreDependencies.soyFunctionMap, coreDependencies.printDirectives, filesBuilder.build(), getGeneralOptions(), cache, conformanceConfig, loggingConfig, warningSink); }
public void testInvalidPublicDependency() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .build(); FileDescriptorProto barProto = FileDescriptorProto.newBuilder() .setName("boo.proto") .addDependency("foo.proto") .addPublicDependency(1) // Error, should be 0. .build(); FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); try { Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[] {fooFile}); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { assertTrue( e.getMessage().indexOf("Invalid public dependency index.") != -1); } }
public void testUnknownFieldsDenied() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addMessageType(DescriptorProto.newBuilder() .setName("Foo") .addField(FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) .setTypeName("Bar") .setName("bar") .setNumber(1))) .build(); try { Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { assertTrue(e.getMessage().indexOf("Bar") != -1); assertTrue(e.getMessage().indexOf("is not defined") != -1); } }
/** * Tests that the DescriptorValidationException works as intended. */ public void testDescriptorValidatorException() throws Exception { FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addMessageType(DescriptorProto.newBuilder() .setName("Foo") .addField(FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) .setType(FieldDescriptorProto.Type.TYPE_INT32) .setName("foo") .setNumber(1) .setDefaultValue("invalid") .build()) .build()) .build(); try { Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { // Expected; check that the error message contains some useful hints assertTrue(e.getMessage().indexOf("foo") != -1); assertTrue(e.getMessage().indexOf("Foo") != -1); assertTrue(e.getMessage().indexOf("invalid") != -1); assertTrue(e.getCause() instanceof NumberFormatException); assertTrue(e.getCause().getMessage().indexOf("invalid") != -1); } }
public CausalOrderProtobufSerializer(@NotNull final Serializer<T> objectSerializer) { this.objectSerializer = objectSerializer; try { final FileDescriptorProto timestampedMessageFile = FileDescriptorProto.newBuilder() .addMessageType( DescriptorProto.newBuilder() .setName(MESSAGE_NAME) .addField( FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setName(IDS_FIELD_NAME) .setNumber(IDS_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_BYTES)) .addField( FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setName(TIMESTAMPS_FIELD_NAME) .setNumber(TIMESTAMPS_FIELD_NUMBER) .setOptions(FieldOptions.newBuilder() .setPacked(true)) .setType(FieldDescriptorProto.Type.TYPE_UINT64)) .addField( FieldDescriptorProto.newBuilder() .setName(VALUE_FIELD_NAME) .setNumber(VALUE_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_BYTES))) .build(); this.messageDescriptor = FileDescriptor .buildFrom(timestampedMessageFile, new FileDescriptor[0]) .findMessageTypeByName(MESSAGE_NAME); this.ids = messageDescriptor.findFieldByName(IDS_FIELD_NAME); this.timestamps = messageDescriptor.findFieldByName(TIMESTAMPS_FIELD_NAME); this.value = messageDescriptor.findFieldByName(VALUE_FIELD_NAME); this.messageParser = DynamicMessage.newBuilder(messageDescriptor) .buildPartial() .getParserForType(); } catch (final DescriptorValidationException e) { throw new RuntimeException(e); } }
public SingleSourceFifoOrderProtobufSerializer(@NotNull final Serializer<T> objectSerializer) { this.objectSerializer = objectSerializer; try { final FileDescriptorProto timestampedMessageFile = FileDescriptorProto.newBuilder() .addMessageType( DescriptorProto.newBuilder() .setName(MESSAGE_NAME) .addField( FieldDescriptorProto.newBuilder() .setName(TIMESTAMP_FIELD_NAME) .setNumber(TIMESTAMP_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_INT64)) .addField( FieldDescriptorProto.newBuilder() .setName(VALUE_FIELD_NAME) .setNumber(VALUE_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_BYTES))) .build(); final Descriptor message = FileDescriptor .buildFrom(timestampedMessageFile, new FileDescriptor[0]) .findMessageTypeByName(MESSAGE_NAME); this.timestampedMessageField = message.findFieldByName(TIMESTAMP_FIELD_NAME); this.valueMessageField = message.findFieldByName(VALUE_FIELD_NAME); this.messageBuilder = DynamicMessage.newBuilder(message); this.messageParser = messageBuilder.buildPartial().getParserForType(); } catch (final DescriptorValidationException e) { throw new RuntimeException(e); } }
static ProtobufRowDataConverter buildRowDataConverter(HasStorage object, FileDescriptorProto fileProto) { Group group = (Group)object; FileDescriptor fileDescriptor; try { fileDescriptor = FileDescriptor.buildFrom(fileProto, DEPENDENCIES); } catch (DescriptorValidationException ex) { throw new ProtobufBuildException(ex); } return ProtobufRowDataConverter.forGroup(group, fileDescriptor); }
static ProtobufRowConverter buildRowConverter(HasStorage object, FileDescriptorProto fileProto) { Group group = (Group) object; FileDescriptor fileDescriptor; try { fileDescriptor = FileDescriptor.buildFrom(fileProto, DEPENDENCIES); } catch (DescriptorValidationException ex) { throw new ProtobufBuildException(ex); } return ProtobufRowConverter.forGroup(group, fileDescriptor); }
/** * Parses a serialized schema descriptor (from input stream; closes the stream) * * @param schemaDescIn the descriptor input stream * @return the schema object * @throws DescriptorValidationException * @throws IOException */ public static DynamicSchema parseFrom(InputStream schemaDescIn) throws DescriptorValidationException, IOException { try { int len; byte[] buf = new byte[4096]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((len = schemaDescIn.read(buf)) > 0) baos.write(buf, 0, len); return parseFrom(baos.toByteArray()); } finally { schemaDescIn.close(); } }
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()); }
public void testHiddenDependency() throws Exception { FileDescriptorProto barProto = FileDescriptorProto.newBuilder() .setName("bar.proto") .addMessageType(DescriptorProto.newBuilder().setName("Bar")) .build(); FileDescriptorProto forwardProto = FileDescriptorProto.newBuilder() .setName("forward.proto") .addDependency("bar.proto") .build(); FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addDependency("forward.proto") .addMessageType(DescriptorProto.newBuilder() .setName("Foo") .addField(FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) .setTypeName("Bar") .setName("bar") .setNumber(1))) .build(); FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom( barProto, new FileDescriptor[0]); FileDescriptor forwardFile = Descriptors.FileDescriptor.buildFrom( forwardProto, new FileDescriptor[] {barFile}); try { Descriptors.FileDescriptor.buildFrom( fooProto, new FileDescriptor[] {forwardFile}); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { assertTrue(e.getMessage().indexOf("Bar") != -1); assertTrue(e.getMessage().indexOf("is not defined") != -1); } }
private FileDescriptor build(final FileDescriptor fileDescriptor) { try { return FileDescriptor.buildFrom(makeCanonicalProto(fileDescriptor), fileDescriptor .getDependencies().toArray(new FileDescriptor[0])); } catch (final DescriptorValidationException e) { throw new DescriptorValidationRuntimeException(e); } }
private FileDescriptor build(final FileDescriptorProto proto, final FileDescriptor[] dependencies) { try { final FileDescriptor fileDescriptor = FileDescriptor.buildFrom(proto, dependencies); return FileDescriptor.buildFrom(makeCanonicalProto(fileDescriptor), dependencies); } catch (final DescriptorValidationException e) { throw new DescriptorValidationRuntimeException(e); } }
private static FileDescriptorEx buildFrom(final FileDescriptorProto proto, final FileDescriptor[] dependencies, final boolean reparseCustomOptions) { try { return getInstance(FileDescriptor.buildFrom(proto, dependencies), reparseCustomOptions); } catch (final DescriptorValidationException e) { throw new DescriptorValidationRuntimeException(e); } }
public FileDescriptor getBasicCanonicalFileDescriptor(final boolean forceRebuild) { if (!forceRebuild && isCanonical()) { return delegate; } try { return FileDescriptor.buildFrom(toCanonicalProto(), delegate.getDependencies().toArray(new FileDescriptor[0])); } catch (final DescriptorValidationException e) { throw new DescriptorValidationRuntimeException(e); } }
private FileDescriptor buildFileDescriptorWithReserializedProto(final FileDescriptorProto proto, final FileDescriptor[] dependencies) { try { final FileDescriptor fileDescriptor = FileDescriptor.buildFrom(proto, dependencies); final ExtensionRegistry registry = buildFullRegistryOf(fileDescriptor); FileDescriptor.internalUpdateFileDescriptor(fileDescriptor, registry); return fileDescriptor; } catch (final DescriptorValidationException e) { throw new DescriptorValidationRuntimeException(e); } }
/** * Builds the type provider and returns it. */ public SoyProtoTypeProvider build() throws FileNotFoundException, IOException, DescriptorValidationException { SoyProtoTypeProvider provider = new SoyProtoTypeProvider(); DescriptorAddingDescriptorTreeWalker walker = new DescriptorAddingDescriptorTreeWalker(); walkAll(walker); walker.commitInto(provider); return provider; }
/** * Like {@link #build}, but doesn't propagate exceptions that can only arise when descriptors * need to be fetched from the filesystem. */ public SoyProtoTypeProvider buildNoFiles() { Preconditions.checkState( descriptorSources.isEmpty(), "use build(), not buildNoFiles() to load descriptors from files"); try { return build(); } catch (DescriptorValidationException | IOException ex) { throw new AssertionError("File system should not have been touched", ex); } }
static FileDescriptorProto validateAndGenerate(HasStorage object, ProtobufRowFormat.Type formatType, FileDescriptorProto fileProto, AISValidationOutput output) { if (!(object instanceof Group)) { output.reportFailure(new AISValidationFailure(new StorageDescriptionInvalidException(object, "is not a Group and cannot use Protocol Buffers"))); return null; } Group group = (Group)object; if (formatType == ProtobufRowFormat.Type.SINGLE_TABLE) { if (!group.getRoot().getChildJoins().isEmpty()) { output.reportFailure(new AISValidationFailure(new StorageDescriptionInvalidException(object, "has more than one table"))); return null; } } int currentVersion = sumTableVersions(group.getRoot()); if (fileProto != null) { int storedVersion = fileProto.getOptions() .getExtension(CustomOptions.GroupOptions.fdbsql).getVersion(); if (storedVersion == currentVersion) { return fileProto; } } FileDescriptorSet set = null; if (fileProto != null) { FileDescriptorSet.Builder builder = FileDescriptorSet.newBuilder(); builder.addFile(fileProto); set = builder.build(); } AISToProtobuf ais2p = new AISToProtobuf(formatType, set); ais2p.addGroup(group); set = ais2p.build(); fileProto = set.getFile(0); // Only added one group. // Make sure it will build before committing to this format. try { FileDescriptor.buildFrom(fileProto, DEPENDENCIES); } catch (DescriptorValidationException ex) { output.reportFailure(new AISValidationFailure(new ProtobufBuildException(ex))); } return fileProto; }
public DescriptorValidationRuntimeException(final DescriptorValidationException delegate) { super(delegate.getMessage(), delegate); problemSymbolName = delegate.getProblemSymbolName(); proto = delegate.getProblemProto(); description = delegate.getDescription(); }
private FileDescriptor createFileDescriptor(FileDescriptorProto fileProto, FileDescriptor[] dependencies) throws DescriptorValidationException { return FileDescriptor.buildFrom(fileProto, dependencies); }
/** * Parses a serialized schema descriptor (from byte array) * * @param schemaDescBuf the descriptor byte array * @return the schema object * @throws DescriptorValidationException * @throws IOException */ public static DynamicSchema parseFrom(byte[] schemaDescBuf) throws DescriptorValidationException, IOException { return new DynamicSchema(FileDescriptorSet.parseFrom(schemaDescBuf)); }