Search in sources :

Example 1 with EnumElement

use of com.squareup.wire.schema.internal.parser.EnumElement in project thingsboard by thingsboard.

the class ProtoTransportPayloadConfiguration method getDynamicSchema.

public DynamicSchema getDynamicSchema(ProtoFileElement protoFileElement, String schemaName) {
    DynamicSchema.Builder schemaBuilder = DynamicSchema.newBuilder();
    schemaBuilder.setName(schemaName);
    schemaBuilder.setSyntax(PROTO_3_SYNTAX);
    schemaBuilder.setPackage(!isEmptyStr(protoFileElement.getPackageName()) ? protoFileElement.getPackageName() : schemaName.toLowerCase());
    List<TypeElement> types = protoFileElement.getTypes();
    List<MessageElement> messageTypes = getMessageTypes(types);
    if (!messageTypes.isEmpty()) {
        List<EnumElement> enumTypes = getEnumElements(types);
        if (!enumTypes.isEmpty()) {
            enumTypes.forEach(enumElement -> {
                EnumDefinition enumDefinition = getEnumDefinition(enumElement);
                schemaBuilder.addEnumDefinition(enumDefinition);
            });
        }
        List<MessageDefinition> messageDefinitions = getMessageDefinitions(messageTypes);
        messageDefinitions.forEach(schemaBuilder::addMessageDefinition);
        try {
            return schemaBuilder.build();
        } catch (Descriptors.DescriptorValidationException e) {
            throw new RuntimeException("Failed to create dynamic schema due to: " + e.getMessage());
        }
    } else {
        throw new RuntimeException("Failed to get Dynamic Schema! Message types is empty for schema:" + schemaName);
    }
}
Also used : TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) EnumDefinition(com.github.os72.protobuf.dynamic.EnumDefinition) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) Descriptors(com.google.protobuf.Descriptors)

Example 2 with EnumElement

use of com.squareup.wire.schema.internal.parser.EnumElement in project apicurio-registry by Apicurio.

the class FileDescriptorUtils method fileDescriptorToProtoFile.

public static ProtoFileElement fileDescriptorToProtoFile(FileDescriptorProto file) {
    String packageName = file.getPackage();
    if ("".equals(packageName)) {
        packageName = null;
    }
    Syntax syntax = null;
    switch(file.getSyntax()) {
        case PROTO2:
            syntax = Syntax.PROTO_2;
            break;
        case PROTO3:
            syntax = Syntax.PROTO_3;
            break;
        default:
            break;
    }
    ImmutableList.Builder<TypeElement> types = ImmutableList.builder();
    for (DescriptorProto md : file.getMessageTypeList()) {
        MessageElement message = toMessage(file, md);
        types.add(message);
    }
    for (EnumDescriptorProto ed : file.getEnumTypeList()) {
        EnumElement enumer = toEnum(ed);
        types.add(enumer);
    }
    ImmutableList.Builder<ServiceElement> services = ImmutableList.builder();
    for (ServiceDescriptorProto sv : file.getServiceList()) {
        ServiceElement service = toService(sv);
        services.add(service);
    }
    ImmutableList.Builder<String> imports = ImmutableList.builder();
    ImmutableList.Builder<String> publicImports = ImmutableList.builder();
    List<String> dependencyList = file.getDependencyList();
    Set<Integer> publicDependencyList = new HashSet<>(file.getPublicDependencyList());
    for (int i = 0; i < dependencyList.size(); i++) {
        String depName = dependencyList.get(i);
        if (publicDependencyList.contains(i)) {
            publicImports.add(depName);
        } else {
            imports.add(depName);
        }
    }
    ImmutableList.Builder<OptionElement> options = ImmutableList.builder();
    if (file.getOptions().hasJavaPackage()) {
        OptionElement option = new OptionElement(JAVA_PACKAGE_OPTION, stringKind, file.getOptions().getJavaPackage(), false);
        options.add(option);
    }
    if (file.getOptions().hasJavaOuterClassname()) {
        OptionElement option = new OptionElement(JAVA_OUTER_CLASSNAME_OPTION, stringKind, file.getOptions().getJavaOuterClassname(), false);
        options.add(option);
    }
    if (file.getOptions().hasJavaMultipleFiles()) {
        OptionElement option = new OptionElement(JAVA_MULTIPLE_FILES_OPTION, booleanKind, file.getOptions().getJavaMultipleFiles(), false);
        options.add(option);
    }
    if (file.getOptions().hasJavaGenericServices()) {
        OptionElement option = new OptionElement(JAVA_GENERIC_SERVICES_OPTION, booleanKind, file.getOptions().getJavaGenericServices(), false);
        options.add(option);
    }
    if (file.getOptions().hasJavaStringCheckUtf8()) {
        OptionElement option = new OptionElement(JAVA_STRING_CHECK_UTF8_OPTION, booleanKind, file.getOptions().getJavaStringCheckUtf8(), false);
        options.add(option);
    }
    if (file.getOptions().hasCcGenericServices()) {
        OptionElement option = new OptionElement(CC_GENERIC_SERVICES_OPTION, booleanKind, file.getOptions().getCcGenericServices(), false);
        options.add(option);
    }
    if (file.getOptions().hasCcEnableArenas()) {
        OptionElement option = new OptionElement(CC_ENABLE_ARENAS_OPTION, booleanKind, file.getOptions().getCcEnableArenas(), false);
        options.add(option);
    }
    if (file.getOptions().hasCsharpNamespace()) {
        OptionElement option = new OptionElement(CSHARP_NAMESPACE_OPTION, stringKind, file.getOptions().getCsharpNamespace(), false);
        options.add(option);
    }
    if (file.getOptions().hasGoPackage()) {
        OptionElement option = new OptionElement(GO_PACKAGE_OPTION, stringKind, file.getOptions().getGoPackage(), false);
        options.add(option);
    }
    if (file.getOptions().hasObjcClassPrefix()) {
        OptionElement option = new OptionElement(OBJC_CLASS_PREFIX_OPTION, stringKind, file.getOptions().getObjcClassPrefix(), false);
        options.add(option);
    }
    if (file.getOptions().hasPhpClassPrefix()) {
        OptionElement option = new OptionElement(PHP_CLASS_PREFIX_OPTION, stringKind, file.getOptions().getPhpClassPrefix(), false);
        options.add(option);
    }
    if (file.getOptions().hasPhpGenericServices()) {
        OptionElement option = new OptionElement(PHP_GENERIC_SERVICES_OPTION, booleanKind, file.getOptions().getPhpGenericServices(), false);
        options.add(option);
    }
    if (file.getOptions().hasPhpMetadataNamespace()) {
        OptionElement option = new OptionElement(PHP_METADATA_NAMESPACE_OPTION, stringKind, file.getOptions().getPhpMetadataNamespace(), false);
        options.add(option);
    }
    if (file.getOptions().hasPhpNamespace()) {
        OptionElement option = new OptionElement(PHP_NAMESPACE_OPTION, stringKind, file.getOptions().getPhpNamespace(), false);
        options.add(option);
    }
    if (file.getOptions().hasPyGenericServices()) {
        OptionElement option = new OptionElement(PY_GENERIC_SERVICES_OPTION, booleanKind, file.getOptions().getPyGenericServices(), false);
        options.add(option);
    }
    if (file.getOptions().hasRubyPackage()) {
        OptionElement option = new OptionElement(RUBY_PACKAGE_OPTION, stringKind, file.getOptions().getRubyPackage(), false);
        options.add(option);
    }
    if (file.getOptions().hasSwiftPrefix()) {
        OptionElement option = new OptionElement(SWIFT_PREFIX_OPTION, stringKind, file.getOptions().getSwiftPrefix(), false);
        options.add(option);
    }
    if (file.getOptions().hasOptimizeFor()) {
        OptionElement option = new OptionElement(OPTIMIZE_FOR_OPTION, enumKind, file.getOptions().getOptimizeFor(), false);
        options.add(option);
    }
    return new ProtoFileElement(DEFAULT_LOCATION, packageName, syntax, imports.build(), publicImports.build(), types.build(), services.build(), Collections.emptyList(), options.build());
}
Also used : EnumDescriptorProto(com.google.protobuf.DescriptorProtos.EnumDescriptorProto) ImmutableList(com.google.common.collect.ImmutableList) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) ServiceDescriptorProto(com.google.protobuf.DescriptorProtos.ServiceDescriptorProto) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) 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) Syntax(com.squareup.wire.Syntax) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with EnumElement

use of com.squareup.wire.schema.internal.parser.EnumElement in project apicurio-registry by Apicurio.

the class FileDescriptorUtils method toDynamicSchema.

/*
     * DynamicSchema is used as a temporary helper class and should not be exposed in the API.
     */
private static DynamicSchema toDynamicSchema(String name, ProtoFileElement rootElem, Map<String, ProtoFileElement> dependencies) {
    DynamicSchema.Builder schema = DynamicSchema.newBuilder();
    try {
        Syntax syntax = rootElem.getSyntax();
        if (syntax != null) {
            schema.setSyntax(syntax.toString());
        }
        if (rootElem.getPackageName() != null) {
            schema.setPackage(rootElem.getPackageName());
        }
        for (TypeElement typeElem : rootElem.getTypes()) {
            if (typeElem instanceof MessageElement) {
                MessageDefinition message = toDynamicMessage((MessageElement) typeElem);
                schema.addMessageDefinition(message);
            } else if (typeElem instanceof EnumElement) {
                EnumDefinition enumer = toDynamicEnum((EnumElement) typeElem);
                schema.addEnumDefinition(enumer);
            }
        }
        for (String ref : rootElem.getImports()) {
            ProtoFileElement dep = dependencies.get(ref);
            if (dep != null) {
                schema.addDependency(ref);
                schema.addSchema(toDynamicSchema(ref, dep, dependencies));
            }
        }
        for (String ref : rootElem.getPublicImports()) {
            ProtoFileElement dep = dependencies.get(ref);
            if (dep != null) {
                schema.addPublicDependency(ref);
                schema.addSchema(toDynamicSchema(ref, dep, dependencies));
            }
        }
        String javaPackageName = findOption("java_package", rootElem.getOptions()).map(o -> o.getValue().toString()).orElse(null);
        if (javaPackageName != null) {
            schema.setJavaPackage(javaPackageName);
        }
        String javaOuterClassname = findOption("java_outer_classname", rootElem.getOptions()).map(o -> o.getValue().toString()).orElse(null);
        if (javaOuterClassname != null) {
            schema.setJavaOuterClassname(javaOuterClassname);
        }
        Boolean javaMultipleFiles = findOption("java_multiple_files", rootElem.getOptions()).map(o -> Boolean.valueOf(o.getValue().toString())).orElse(null);
        if (javaMultipleFiles != null) {
            schema.setJavaMultipleFiles(javaMultipleFiles);
        }
        schema.setName(name);
        return schema.build();
    } catch (Descriptors.DescriptorValidationException e) {
        throw new IllegalStateException(e);
    }
}
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) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) Syntax(com.squareup.wire.Syntax)

Example 4 with EnumElement

use of com.squareup.wire.schema.internal.parser.EnumElement in project apicurio-registry by Apicurio.

the class ProtobufFile method processMessageElement.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void processMessageElement(String scope, MessageElement messageElement) {
    // reservedFields
    Set<Object> reservedFieldSet = new HashSet<>();
    for (ReservedElement reservedElement : messageElement.getReserveds()) {
        for (Object value : reservedElement.getValues()) {
            if (value instanceof Range) {
                reservedFieldSet.addAll(ContiguousSet.create((Range) value, DiscreteDomain.integers()));
            } else {
                reservedFieldSet.add(value);
            }
        }
    }
    if (!reservedFieldSet.isEmpty()) {
        reservedFields.put(scope + messageElement.getName(), reservedFieldSet);
    }
    // fieldMap, mapMap, FieldsIDName
    Map<String, FieldElement> fieldTypeMap = new HashMap<>();
    Map<String, FieldElement> mapMap = new HashMap<>();
    Map<Integer, String> idsToNames = new HashMap<>();
    for (FieldElement fieldElement : messageElement.getFields()) {
        fieldTypeMap.put(fieldElement.getName(), fieldElement);
        if (fieldElement.getType().startsWith("map<")) {
            mapMap.put(fieldElement.getName(), fieldElement);
        }
        idsToNames.put(fieldElement.getTag(), fieldElement.getName());
    }
    for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
        for (FieldElement fieldElement : oneOfElement.getFields()) {
            fieldTypeMap.put(fieldElement.getName(), fieldElement);
            if (fieldElement.getType().startsWith("map<")) {
                mapMap.put(fieldElement.getName(), fieldElement);
            }
            idsToNames.put(fieldElement.getTag(), fieldElement.getName());
        }
    }
    if (!fieldTypeMap.isEmpty()) {
        fieldMap.put(scope + messageElement.getName(), fieldTypeMap);
    }
    if (!mapMap.isEmpty()) {
        this.mapMap.put(scope + messageElement.getName(), mapMap);
    }
    if (!idsToNames.isEmpty()) {
        fieldsById.put(scope + messageElement.getName(), idsToNames);
    }
    // nonReservedFields
    Set<Object> fieldKeySet = new HashSet<>();
    for (FieldElement fieldElement : messageElement.getFields()) {
        fieldKeySet.add(fieldElement.getTag());
        fieldKeySet.add(fieldElement.getName());
    }
    for (OneOfElement oneOfElement : messageElement.getOneOfs()) {
        for (FieldElement fieldElement : oneOfElement.getFields()) {
            fieldKeySet.add(fieldElement.getTag());
            fieldKeySet.add(fieldElement.getName());
        }
    }
    if (!fieldKeySet.isEmpty()) {
        nonReservedFields.put(scope + messageElement.getName(), fieldKeySet);
    }
    for (TypeElement typeElement : messageElement.getNestedTypes()) {
        if (typeElement instanceof MessageElement) {
            processMessageElement(messageElement.getName() + ".", (MessageElement) typeElement);
        } else if (typeElement instanceof EnumElement) {
            processEnumElement(messageElement.getName() + ".", (EnumElement) typeElement);
        }
    }
}
Also used : HashMap(java.util.HashMap) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) ReservedElement(com.squareup.wire.schema.internal.parser.ReservedElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) Range(com.google.common.collect.Range) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) HashSet(java.util.HashSet)

Example 5 with EnumElement

use of com.squareup.wire.schema.internal.parser.EnumElement in project apicurio-registry by Apicurio.

the class ProtobufFile method buildIndexes.

private void buildIndexes() {
    for (TypeElement typeElement : element.getTypes()) {
        if (typeElement instanceof MessageElement) {
            MessageElement messageElement = (MessageElement) typeElement;
            processMessageElement("", messageElement);
        } else if (typeElement instanceof EnumElement) {
            EnumElement enumElement = (EnumElement) typeElement;
            processEnumElement("", enumElement);
        } else {
            throw new RuntimeException();
        }
    }
    for (ServiceElement serviceElement : element.getServices()) {
        Set<String> rpcNames = new HashSet<>();
        Map<String, String> rpcSignatures = new HashMap<>();
        for (RpcElement rpcElement : serviceElement.getRpcs()) {
            rpcNames.add(rpcElement.getName());
            String signature = rpcElement.getRequestType() + ":" + rpcElement.getRequestStreaming() + "->" + rpcElement.getResponseType() + ":" + rpcElement.getResponseStreaming();
            rpcSignatures.put(rpcElement.getName(), signature);
        }
        if (!rpcNames.isEmpty()) {
            serviceRPCnames.put(serviceElement.getName(), rpcNames);
            serviceRPCSignatures.put(serviceElement.getName(), rpcSignatures);
        }
    }
}
Also used : RpcElement(com.squareup.wire.schema.internal.parser.RpcElement) HashMap(java.util.HashMap) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) ServiceElement(com.squareup.wire.schema.internal.parser.ServiceElement) HashSet(java.util.HashSet)

Aggregations

EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)26 MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)23 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)22 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)18 ServiceElement (com.squareup.wire.schema.internal.parser.ServiceElement)17 HashSet (java.util.HashSet)16 ImmutableList (com.google.common.collect.ImmutableList)14 EnumValueDescriptorProto (com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto)14 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)14 EnumConstantElement (com.squareup.wire.schema.internal.parser.EnumConstantElement)13 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)13 OneOfElement (com.squareup.wire.schema.internal.parser.OneOfElement)13 ReservedElement (com.squareup.wire.schema.internal.parser.ReservedElement)13 Syntax (com.squareup.wire.Syntax)12 RpcElement (com.squareup.wire.schema.internal.parser.RpcElement)12 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)11 EnumDescriptorProto (com.google.protobuf.DescriptorProtos.EnumDescriptorProto)11 FieldDescriptorProto (com.google.protobuf.DescriptorProtos.FieldDescriptorProto)11 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)11 MethodDescriptorProto (com.google.protobuf.DescriptorProtos.MethodDescriptorProto)11