Search in sources :

Example 6 with Options

use of com.squareup.wire.schema.Options in project schema2proto by entur.

the class ModifyProto method addEnumConstant.

private void addEnumConstant(NewEnumConstant newEnumConstant, Schema prunedSchema) throws InvalidProtobufException {
    Type targetEnumType = prunedSchema.getType(newEnumConstant.targetEnumType);
    if (targetEnumType instanceof EnumType) {
        List<OptionElement> optionElements = new ArrayList<>();
        Options options = new Options(Options.ENUM_VALUE_OPTIONS, optionElements);
        Location location = new Location("", "", -1, -1);
        EnumConstant enumConstant = new EnumConstant(location, newEnumConstant.name, newEnumConstant.fieldNumber, newEnumConstant.documentation, options);
        EnumType enumType = (EnumType) targetEnumType;
        // Duplicate check
        Optional<EnumConstant> existing = enumType.constants().stream().filter(e -> e.getName().equals(enumConstant.getName()) || e.getTag() == enumConstant.getTag()).findFirst();
        if (existing.isPresent()) {
            throw new InvalidProtobufException("Enum constant already present: " + newEnumConstant);
        } else {
            enumType.constants().add(enumConstant);
        }
    } else {
        throw new InvalidProtobufException("Did not find existing enum " + newEnumConstant.targetEnumType);
    }
}
Also used : Schema(com.squareup.wire.schema.Schema) LoggerFactory(org.slf4j.LoggerFactory) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) MergeFrom(no.entur.schema2proto.modifyproto.config.MergeFrom) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) HashSet(java.util.HashSet) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Type(com.squareup.wire.schema.Type) GoPackageNameHelper.packageNameToGoPackageName(no.entur.schema2proto.generateproto.GoPackageNameHelper.packageNameToGoPackageName) Path(java.nio.file.Path) InvalidConfigurationException(no.entur.schema2proto.InvalidConfigurationException) MessageType(com.squareup.wire.schema.MessageType) FieldOption(no.entur.schema2proto.modifyproto.config.FieldOption) OptionReader(com.squareup.wire.schema.internal.parser.OptionReader) NewField(no.entur.schema2proto.modifyproto.config.NewField) Logger(org.slf4j.Logger) Files(java.nio.file.Files) BackwardsCompatibilityCheckException(no.entur.schema2proto.compatibility.BackwardsCompatibilityCheckException) Constructor(org.yaml.snakeyaml.constructor.Constructor) ProtoFile(com.squareup.wire.schema.ProtoFile) Collection(java.util.Collection) FileWriter(java.io.FileWriter) SyntaxReader(com.squareup.wire.schema.internal.parser.SyntaxReader) Set(java.util.Set) IdentifierSet(com.squareup.wire.schema.IdentifierSet) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) ProtolockBackwardsCompatibilityChecker(no.entur.schema2proto.compatibility.ProtolockBackwardsCompatibilityChecker) ModifyProtoConfiguration(no.entur.schema2proto.modifyproto.config.ModifyProtoConfiguration) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) EnumConstant(com.squareup.wire.schema.EnumConstant) NewEnumConstant(no.entur.schema2proto.modifyproto.config.NewEnumConstant) Objects(java.util.Objects) List(java.util.List) SchemaLoader(com.squareup.wire.schema.SchemaLoader) Writer(java.io.Writer) Optional(java.util.Optional) Field(com.squareup.wire.schema.Field) TypeDescription(org.yaml.snakeyaml.TypeDescription) Options(com.squareup.wire.schema.Options) EnumType(com.squareup.wire.schema.EnumType) InputStream(java.io.InputStream) Options(com.squareup.wire.schema.Options) Type(com.squareup.wire.schema.Type) MessageType(com.squareup.wire.schema.MessageType) EnumType(com.squareup.wire.schema.EnumType) EnumType(com.squareup.wire.schema.EnumType) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ArrayList(java.util.ArrayList) EnumConstant(com.squareup.wire.schema.EnumConstant) NewEnumConstant(no.entur.schema2proto.modifyproto.config.NewEnumConstant) Location(com.squareup.wire.schema.Location)

Example 7 with Options

use of com.squareup.wire.schema.Options in project schema2proto by entur.

the class ModifyProto method addField.

private void addField(NewField newField, Schema prunedSchema) throws InvalidProtobufException {
    MessageType type = (MessageType) prunedSchema.getType(newField.targetMessageType);
    if (type == null) {
        throw new InvalidProtobufException("Did not find existing type " + newField.targetMessageType);
    } else {
        List<OptionElement> optionElements = new ArrayList<>();
        Options options = new Options(Options.FIELD_OPTIONS, optionElements);
        int tag = newField.fieldNumber;
        String fieldPackage = StringUtils.substringBeforeLast(newField.type, ".");
        if (fieldPackage.equals(newField.type)) {
            // no package
            fieldPackage = null;
        }
        Field.Label label = null;
        if (StringUtils.trimToNull(newField.label) != null) {
            label = Field.Label.valueOf(newField.label.toUpperCase());
        }
        Location location = new Location("", "", -1, -1);
        Field field = new Field(fieldPackage, location, label, newField.name, StringUtils.trimToEmpty(newField.documentation), tag, null, newField.type, options, false, false);
        List<Field> updatedFields = new ArrayList<>(type.fields());
        updatedFields.add(field);
        type.setDeclaredFields(updatedFields);
        String importStatement = StringUtils.trimToNull(newField.importProto);
        if (importStatement != null) {
            String targetPackageName = StringUtils.trimToNull(StringUtils.substringBeforeLast(newField.targetMessageType, "."));
            ProtoFile targetFile = prunedSchema.protoFileForPackage(targetPackageName);
            if (newField.targetMessageType.equals(targetPackageName)) {
                // no package name on target
                targetFile = prunedSchema.protoFileForPackage(null);
            }
            targetFile.imports().add(importStatement);
        }
    }
}
Also used : Options(com.squareup.wire.schema.Options) ArrayList(java.util.ArrayList) ProtoFile(com.squareup.wire.schema.ProtoFile) NewField(no.entur.schema2proto.modifyproto.config.NewField) Field(com.squareup.wire.schema.Field) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) MessageType(com.squareup.wire.schema.MessageType) Location(com.squareup.wire.schema.Location)

Example 8 with Options

use of com.squareup.wire.schema.Options in project apicurio-registry by Apicurio.

the class FileDescriptorUtils method toFileDescriptorProto.

private static FileDescriptorProto toFileDescriptorProto(String schemaDefinition, String protoFileName, Optional<String> optionalPackageName) {
    final ProtobufSchemaLoader.ProtobufSchemaLoaderContext protobufSchemaLoaderContext;
    try {
        protobufSchemaLoaderContext = ProtobufSchemaLoader.loadSchema(optionalPackageName, protoFileName, schemaDefinition);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    FileDescriptorProto.Builder schema = FileDescriptorProto.newBuilder();
    ProtoFile element = protobufSchemaLoaderContext.getProtoFile();
    Schema schemaContext = protobufSchemaLoaderContext.getSchema();
    schema.setName(protoFileName);
    Syntax syntax = element.getSyntax();
    if (Syntax.PROTO_3.equals(syntax)) {
        schema.setSyntax(syntax.toString());
    }
    if (element.getPackageName() != null) {
        schema.setPackage(element.getPackageName());
    }
    for (ProtoType protoType : schemaContext.getTypes()) {
        if (!isParentLevelType(protoType, optionalPackageName)) {
            continue;
        }
        Type type = schemaContext.getType(protoType);
        if (type instanceof MessageType) {
            DescriptorProto message = messageElementToDescriptorProto((MessageType) type, schemaContext, element);
            schema.addMessageType(message);
        } else if (type instanceof EnumType) {
            EnumDescriptorProto message = enumElementToProto((EnumType) type);
            schema.addEnumType(message);
        }
    }
    for (Service service : element.getServices()) {
        ServiceDescriptorProto serviceDescriptorProto = serviceElementToProto(service);
        schema.addService(serviceDescriptorProto);
    }
    // dependencies on protobuf default types are always added
    for (String ref : element.getImports()) {
        schema.addDependency(ref);
    }
    for (String ref : element.getPublicImports()) {
        boolean add = true;
        for (int i = 0; i < schema.getDependencyCount(); i++) {
            if (schema.getDependency(i).equals(ref)) {
                schema.addPublicDependency(i);
                add = false;
            }
        }
        if (add) {
            schema.addDependency(ref);
            schema.addPublicDependency(schema.getDependencyCount() - 1);
        }
    }
    String javaPackageName = findOptionString(JAVA_PACKAGE_OPTION, element.getOptions());
    if (javaPackageName != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaPackage(javaPackageName).build();
        schema.mergeOptions(options);
    }
    String javaOuterClassname = findOptionString(JAVA_OUTER_CLASSNAME_OPTION, element.getOptions());
    if (javaOuterClassname != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaOuterClassname(javaOuterClassname).build();
        schema.mergeOptions(options);
    }
    Boolean javaMultipleFiles = findOptionBoolean(JAVA_MULTIPLE_FILES_OPTION, element.getOptions());
    if (javaMultipleFiles != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaMultipleFiles(javaMultipleFiles).build();
        schema.mergeOptions(options);
    }
    Boolean javaStringCheckUtf8 = findOptionBoolean(JAVA_STRING_CHECK_UTF8_OPTION, element.getOptions());
    if (javaStringCheckUtf8 != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaStringCheckUtf8(javaStringCheckUtf8).build();
        schema.mergeOptions(options);
    }
    Boolean javaGenericServices = findOptionBoolean(JAVA_GENERIC_SERVICES_OPTION, element.getOptions());
    if (javaGenericServices != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaGenericServices(javaGenericServices).build();
        schema.mergeOptions(options);
    }
    Boolean ccGenericServices = findOptionBoolean(CC_GENERIC_SERVICES_OPTION, element.getOptions());
    if (ccGenericServices != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setCcGenericServices(ccGenericServices).build();
        schema.mergeOptions(options);
    }
    Boolean ccEnableArenas = findOptionBoolean(CC_ENABLE_ARENAS_OPTION, element.getOptions());
    if (ccEnableArenas != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setCcEnableArenas(ccEnableArenas).build();
        schema.mergeOptions(options);
    }
    String csharpNamespace = findOptionString(CSHARP_NAMESPACE_OPTION, element.getOptions());
    if (csharpNamespace != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setCsharpNamespace(csharpNamespace).build();
        schema.mergeOptions(options);
    }
    String goPackageName = findOptionString(GO_PACKAGE_OPTION, element.getOptions());
    if (goPackageName != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setGoPackage(goPackageName).build();
        schema.mergeOptions(options);
    }
    String objcClassPrefix = findOptionString(OBJC_CLASS_PREFIX_OPTION, element.getOptions());
    if (objcClassPrefix != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setObjcClassPrefix(objcClassPrefix).build();
        schema.mergeOptions(options);
    }
    Boolean phpGenericServices = findOptionBoolean(PHP_GENERIC_SERVICES_OPTION, element.getOptions());
    if (phpGenericServices != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPhpGenericServices(phpGenericServices).build();
        schema.mergeOptions(options);
    }
    String phpClassPrefix = findOptionString(PHP_CLASS_PREFIX_OPTION, element.getOptions());
    if (phpClassPrefix != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPhpClassPrefix(phpClassPrefix).build();
        schema.mergeOptions(options);
    }
    String phpMetadataNamespace = findOptionString(PHP_METADATA_NAMESPACE_OPTION, element.getOptions());
    if (phpMetadataNamespace != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPhpMetadataNamespace(phpMetadataNamespace).build();
        schema.mergeOptions(options);
    }
    String phpNamespace = findOptionString(PHP_NAMESPACE_OPTION, element.getOptions());
    if (phpNamespace != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPhpNamespace(phpNamespace).build();
        schema.mergeOptions(options);
    }
    Boolean pyGenericServices = findOptionBoolean(PY_GENERIC_SERVICES_OPTION, element.getOptions());
    if (pyGenericServices != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPyGenericServices(pyGenericServices).build();
        schema.mergeOptions(options);
    }
    String rubyPackage = findOptionString(RUBY_PACKAGE_OPTION, element.getOptions());
    if (rubyPackage != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setRubyPackage(rubyPackage).build();
        schema.mergeOptions(options);
    }
    String swiftPrefix = findOptionString(SWIFT_PREFIX_OPTION, element.getOptions());
    if (swiftPrefix != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setSwiftPrefix(swiftPrefix).build();
        schema.mergeOptions(options);
    }
    FileOptions.OptimizeMode optimizeFor = findOption(OPTIMIZE_FOR_OPTION, element.getOptions()).map(o -> FileOptions.OptimizeMode.valueOf(o.getValue().toString())).orElse(null);
    if (optimizeFor != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setOptimizeFor(optimizeFor).build();
        schema.mergeOptions(options);
    }
    return schema.build();
}
Also used : QuaternionProto(com.google.type.QuaternionProto) Decimals(additionalTypes.Decimals) com.google.protobuf(com.google.protobuf) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) MonthProto(com.google.type.MonthProto) Set(java.util.Set) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ProtoType(com.squareup.wire.schema.ProtoType) Collectors(java.util.stream.Collectors) EnumConstant(com.squareup.wire.schema.EnumConstant) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) List(java.util.List) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) PostalAddressProto(com.google.type.PostalAddressProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) ProtobufSchemaMetadata(metadata.ProtobufSchemaMetadata) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Service(com.squareup.wire.schema.Service) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) ProtoType(com.squareup.wire.schema.ProtoType) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) EnumType(com.squareup.wire.schema.EnumType) EnumType(com.squareup.wire.schema.EnumType) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Syntax(com.squareup.wire.Syntax) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) MessageType(com.squareup.wire.schema.MessageType) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions)

Example 9 with Options

use of com.squareup.wire.schema.Options in project aws-glue-schema-registry by awslabs.

the class FileDescriptorUtils method toFileDescriptorProto.

private static FileDescriptorProto toFileDescriptorProto(String schemaDefinition, String protoFileName, Optional<String> optionalPackageName) {
    final ProtobufSchemaLoader.ProtobufSchemaLoaderContext protobufSchemaLoaderContext;
    try {
        protobufSchemaLoaderContext = ProtobufSchemaLoader.loadSchema(optionalPackageName, protoFileName, schemaDefinition);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    FileDescriptorProto.Builder schema = FileDescriptorProto.newBuilder();
    ProtoFile element = protobufSchemaLoaderContext.getProtoFile();
    Schema schemaContext = protobufSchemaLoaderContext.getSchema();
    schema.setName(protoFileName);
    Syntax syntax = element.getSyntax();
    if (Syntax.PROTO_3.equals(syntax)) {
        schema.setSyntax(syntax.toString());
    }
    if (element.getPackageName() != null) {
        schema.setPackage(element.getPackageName());
    }
    for (ProtoType protoType : schemaContext.getTypes()) {
        if (!isParentLevelType(protoType, optionalPackageName)) {
            continue;
        }
        Type type = schemaContext.getType(protoType);
        if (type instanceof MessageType) {
            DescriptorProto message = messageElementToDescriptorProto((MessageType) type, schemaContext, element);
            schema.addMessageType(message);
        } else if (type instanceof EnumType) {
            EnumDescriptorProto message = enumElementToProto((EnumType) type);
            schema.addEnumType(message);
        }
    }
    for (Service service : element.getServices()) {
        ServiceDescriptorProto serviceDescriptorProto = serviceElementToProto(service);
        schema.addService(serviceDescriptorProto);
    }
    // dependencies on protobuf default types are always added
    for (String ref : element.getImports()) {
        schema.addDependency(ref);
    }
    for (String ref : element.getPublicImports()) {
        boolean add = true;
        for (int i = 0; i < schema.getDependencyCount(); i++) {
            if (schema.getDependency(i).equals(ref)) {
                schema.addPublicDependency(i);
                add = false;
            }
        }
        if (add) {
            schema.addDependency(ref);
            schema.addPublicDependency(schema.getDependencyCount() - 1);
        }
    }
    String javaPackageName = findOptionString(JAVA_PACKAGE_OPTION, element.getOptions());
    if (javaPackageName != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaPackage(javaPackageName).build();
        schema.mergeOptions(options);
    }
    String javaOuterClassname = findOptionString(JAVA_OUTER_CLASSNAME_OPTION, element.getOptions());
    if (javaOuterClassname != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaOuterClassname(javaOuterClassname).build();
        schema.mergeOptions(options);
    }
    Boolean javaMultipleFiles = findOptionBoolean(JAVA_MULTIPLE_FILES_OPTION, element.getOptions());
    if (javaMultipleFiles != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaMultipleFiles(javaMultipleFiles).build();
        schema.mergeOptions(options);
    }
    Boolean javaStringCheckUtf8 = findOptionBoolean(JAVA_STRING_CHECK_UTF8_OPTION, element.getOptions());
    if (javaStringCheckUtf8 != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaStringCheckUtf8(javaStringCheckUtf8).build();
        schema.mergeOptions(options);
    }
    Boolean javaGenericServices = findOptionBoolean(JAVA_GENERIC_SERVICES_OPTION, element.getOptions());
    if (javaGenericServices != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setJavaGenericServices(javaGenericServices).build();
        schema.mergeOptions(options);
    }
    Boolean ccGenericServices = findOptionBoolean(CC_GENERIC_SERVICES_OPTION, element.getOptions());
    if (ccGenericServices != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setCcGenericServices(ccGenericServices).build();
        schema.mergeOptions(options);
    }
    Boolean ccEnableArenas = findOptionBoolean(CC_ENABLE_ARENAS_OPTION, element.getOptions());
    if (ccEnableArenas != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setCcEnableArenas(ccEnableArenas).build();
        schema.mergeOptions(options);
    }
    String csharpNamespace = findOptionString(CSHARP_NAMESPACE_OPTION, element.getOptions());
    if (csharpNamespace != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setCsharpNamespace(csharpNamespace).build();
        schema.mergeOptions(options);
    }
    String goPackageName = findOptionString(GO_PACKAGE_OPTION, element.getOptions());
    if (goPackageName != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setGoPackage(goPackageName).build();
        schema.mergeOptions(options);
    }
    String objcClassPrefix = findOptionString(OBJC_CLASS_PREFIX_OPTION, element.getOptions());
    if (objcClassPrefix != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setObjcClassPrefix(objcClassPrefix).build();
        schema.mergeOptions(options);
    }
    Boolean phpGenericServices = findOptionBoolean(PHP_GENERIC_SERVICES_OPTION, element.getOptions());
    if (phpGenericServices != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPhpGenericServices(phpGenericServices).build();
        schema.mergeOptions(options);
    }
    String phpClassPrefix = findOptionString(PHP_CLASS_PREFIX_OPTION, element.getOptions());
    if (phpClassPrefix != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPhpClassPrefix(phpClassPrefix).build();
        schema.mergeOptions(options);
    }
    String phpMetadataNamespace = findOptionString(PHP_METADATA_NAMESPACE_OPTION, element.getOptions());
    if (phpMetadataNamespace != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPhpMetadataNamespace(phpMetadataNamespace).build();
        schema.mergeOptions(options);
    }
    String phpNamespace = findOptionString(PHP_NAMESPACE_OPTION, element.getOptions());
    if (phpNamespace != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPhpNamespace(phpNamespace).build();
        schema.mergeOptions(options);
    }
    Boolean pyGenericServices = findOptionBoolean(PY_GENERIC_SERVICES_OPTION, element.getOptions());
    if (pyGenericServices != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setPyGenericServices(pyGenericServices).build();
        schema.mergeOptions(options);
    }
    String rubyPackage = findOptionString(RUBY_PACKAGE_OPTION, element.getOptions());
    if (rubyPackage != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setRubyPackage(rubyPackage).build();
        schema.mergeOptions(options);
    }
    String swiftPrefix = findOptionString(SWIFT_PREFIX_OPTION, element.getOptions());
    if (swiftPrefix != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setSwiftPrefix(swiftPrefix).build();
        schema.mergeOptions(options);
    }
    FileOptions.OptimizeMode optimizeFor = findOption(OPTIMIZE_FOR_OPTION, element.getOptions()).map(o -> FileOptions.OptimizeMode.valueOf(o.getValue().toString())).orElse(null);
    if (optimizeFor != null) {
        FileOptions options = DescriptorProtos.FileOptions.newBuilder().setOptimizeFor(optimizeFor).build();
        schema.mergeOptions(options);
    }
    return schema.build();
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) QuaternionProto(com.google.type.QuaternionProto) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) EmptyProto(com.google.protobuf.EmptyProto) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) MonthProto(com.google.type.MonthProto) Set(java.util.Set) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ProtoType(com.squareup.wire.schema.ProtoType) Collectors(java.util.stream.Collectors) EnumConstant(com.squareup.wire.schema.EnumConstant) StructProto(com.google.protobuf.StructProto) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) DurationProto(com.google.protobuf.DurationProto) FieldMaskProto(com.google.protobuf.FieldMaskProto) List(java.util.List) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) PostalAddressProto(com.google.type.PostalAddressProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) SourceContextProto(com.google.protobuf.SourceContextProto) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) TypeProto(com.google.protobuf.TypeProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) AnyProto(com.google.protobuf.AnyProto) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) WrappersProto(com.google.protobuf.WrappersProto) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) ApiProto(com.google.protobuf.ApiProto) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) TimestampProto(com.google.protobuf.TimestampProto) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Service(com.squareup.wire.schema.Service) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) ProtoType(com.squareup.wire.schema.ProtoType) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) EnumType(com.squareup.wire.schema.EnumType) EnumType(com.squareup.wire.schema.EnumType) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Syntax(com.squareup.wire.Syntax) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) MessageType(com.squareup.wire.schema.MessageType) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions)

Example 10 with Options

use of com.squareup.wire.schema.Options in project aws-glue-schema-registry by awslabs.

the class FileDescriptorUtils method toMessage.

private static MessageElement toMessage(FileDescriptorProto file, DescriptorProto descriptor) {
    String name = descriptor.getName();
    ImmutableList.Builder<FieldElement> fields = ImmutableList.builder();
    ImmutableList.Builder<TypeElement> nested = ImmutableList.builder();
    ImmutableList.Builder<ReservedElement> reserved = ImmutableList.builder();
    ImmutableList.Builder<ExtensionsElement> extensions = ImmutableList.builder();
    LinkedHashMap<String, ImmutableList.Builder<FieldElement>> oneofsMap = new LinkedHashMap<>();
    for (OneofDescriptorProto od : descriptor.getOneofDeclList()) {
        oneofsMap.put(od.getName(), ImmutableList.builder());
    }
    List<Map.Entry<String, ImmutableList.Builder<FieldElement>>> oneofs = new ArrayList<>(oneofsMap.entrySet());
    List<FieldElement> proto3OptionalFields = new ArrayList<>();
    for (FieldDescriptorProto fd : descriptor.getFieldList()) {
        if (fd.hasProto3Optional()) {
            proto3OptionalFields.add(toField(file, fd, false));
            continue;
        }
        if (fd.hasOneofIndex()) {
            FieldElement field = toField(file, fd, true);
            oneofs.get(fd.getOneofIndex()).getValue().add(field);
        } else {
            FieldElement field = toField(file, fd, false);
            fields.add(field);
        }
    }
    fields.addAll(proto3OptionalFields);
    for (DescriptorProto nestedDesc : descriptor.getNestedTypeList()) {
        MessageElement nestedMessage = toMessage(file, nestedDesc);
        nested.add(nestedMessage);
    }
    for (EnumDescriptorProto nestedDesc : descriptor.getEnumTypeList()) {
        EnumElement nestedEnum = toEnum(nestedDesc);
        nested.add(nestedEnum);
    }
    for (String reservedName : descriptor.getReservedNameList()) {
        ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", Collections.singletonList(reservedName));
        reserved.add(reservedElem);
    }
    for (DescriptorProto.ReservedRange reservedRange : descriptor.getReservedRangeList()) {
        List<IntRange> values = new ArrayList<>();
        int start = reservedRange.getStart();
        int end = reservedRange.getEnd() - 1;
        values.add(new IntRange(start, end));
        ReservedElement reservedElem = new ReservedElement(DEFAULT_LOCATION, "", values);
        reserved.add(reservedElem);
    }
    for (DescriptorProto.ExtensionRange extensionRange : descriptor.getExtensionRangeList()) {
        List<IntRange> values = new ArrayList<>();
        int start = extensionRange.getStart();
        int end = extensionRange.getEnd() - 1;
        values.add(new IntRange(start, end));
        ExtensionsElement extensionsElement = new ExtensionsElement(DEFAULT_LOCATION, "", values);
        extensions.add(extensionsElement);
    }
    ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
    if (descriptor.getOptions().hasMapEntry()) {
        OptionElement option = new OptionElement(MAP_ENTRY_OPTION, booleanKind, descriptor.getOptions().getMapEntry(), false);
        options.add(option);
    }
    if (descriptor.getOptions().hasNoStandardDescriptorAccessor()) {
        OptionElement option = new OptionElement(NO_STANDARD_DESCRIPTOR_OPTION, booleanKind, descriptor.getOptions().getNoStandardDescriptorAccessor(), false);
        options.add(option);
    }
    return new MessageElement(DEFAULT_LOCATION, name, "", nested.build(), options.build(), reserved.build(), fields.build(), oneofs.stream().filter(e -> e.getValue().build().size() != 0).map(e -> toOneof(e.getKey(), e.getValue())).collect(Collectors.toList()), extensions.build(), Collections.emptyList());
}
Also used : DescriptorProtos(com.google.protobuf.DescriptorProtos) QuaternionProto(com.google.type.QuaternionProto) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) Service(com.squareup.wire.schema.Service) Type(com.squareup.wire.schema.Type) EmptyProto(com.google.protobuf.EmptyProto) Map(java.util.Map) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) PhoneNumberProto(com.google.type.PhoneNumberProto) ExprProto(com.google.type.ExprProto) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) DayOfWeek(com.google.type.DayOfWeek) IntRange(kotlin.ranges.IntRange) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) Predicate(java.util.function.Predicate) MonthProto(com.google.type.MonthProto) Set(java.util.Set) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ProtoType(com.squareup.wire.schema.ProtoType) Collectors(java.util.stream.Collectors) EnumConstant(com.squareup.wire.schema.EnumConstant) StructProto(com.google.protobuf.StructProto) IntervalProto(com.google.type.IntervalProto) Objects(java.util.Objects) DurationProto(com.google.protobuf.DurationProto) FieldMaskProto(com.google.protobuf.FieldMaskProto) List(java.util.List) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) PostalAddressProto(com.google.type.PostalAddressProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Optional(java.util.Optional) SourceContextProto(com.google.protobuf.SourceContextProto) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) ColorProto(com.google.type.ColorProto) TypeProto(com.google.protobuf.TypeProto) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) TimeOfDayProto(com.google.type.TimeOfDayProto) Schema(com.squareup.wire.schema.Schema) OneOf(com.squareup.wire.schema.OneOf) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Rpc(com.squareup.wire.schema.Rpc) Location(com.squareup.wire.schema.Location) ImmutableList(com.google.common.collect.ImmutableList) Syntax(com.squareup.wire.Syntax) MoneyProto(com.google.type.MoneyProto) RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) AnyProto(com.google.protobuf.AnyProto) MessageType(com.squareup.wire.schema.MessageType) LinkedHashSet(java.util.LinkedHashSet) LOWER_UNDERSCORE(com.google.common.base.CaseFormat.LOWER_UNDERSCORE) WrappersProto(com.google.protobuf.WrappersProto) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DateProto(com.google.type.DateProto) ProtoFile(com.squareup.wire.schema.ProtoFile) ApiProto(com.google.protobuf.ApiProto) LatLng(com.google.type.LatLng) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) CalendarPeriodProto(com.google.type.CalendarPeriodProto) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) TreeMap(java.util.TreeMap) TimestampProto(com.google.protobuf.TimestampProto) Field(com.squareup.wire.schema.Field) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) Options(com.squareup.wire.schema.Options) LocalizedTextProto(com.google.type.LocalizedTextProto) EnumType(com.squareup.wire.schema.EnumType) Comparator(java.util.Comparator) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) Collections(java.util.Collections) MethodOptions(com.google.protobuf.DescriptorProtos.MethodOptions) FractionProto(com.google.type.FractionProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) ImmutableList(com.google.common.collect.ImmutableList) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ExtensionsElement(com.squareup.wire.schema.internal.parser.ExtensionsElement) OptionElement(com.squareup.wire.schema.internal.parser.OptionElement) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MethodDescriptorProto(com.google.protobuf.DescriptorProtos.MethodDescriptorProto) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) EnumValueDescriptorProto(com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) DescriptorProto(com.google.protobuf.DescriptorProtos.DescriptorProto) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) OneofDescriptorProto(com.google.protobuf.DescriptorProtos.OneofDescriptorProto) IntRange(kotlin.ranges.IntRange) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) FieldDescriptorProto(com.google.protobuf.DescriptorProtos.FieldDescriptorProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement)

Aggregations

Options (com.squareup.wire.schema.Options)16 Location (com.squareup.wire.schema.Location)15 ArrayList (java.util.ArrayList)15 Field (com.squareup.wire.schema.Field)14 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)14 MessageType (com.squareup.wire.schema.MessageType)13 EnumConstant (com.squareup.wire.schema.EnumConstant)9 ProtoFile (com.squareup.wire.schema.ProtoFile)9 EnumType (com.squareup.wire.schema.EnumType)8 Type (com.squareup.wire.schema.Type)8 HashSet (java.util.HashSet)8 OneOf (com.squareup.wire.schema.OneOf)7 Set (java.util.Set)7 Schema (com.squareup.wire.schema.Schema)6 LinkedHashSet (java.util.LinkedHashSet)6 List (java.util.List)6 Objects (java.util.Objects)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 ImmutableList (com.google.common.collect.ImmutableList)5