Search in sources :

Example 1 with MessageElement

use of com.squareup.wire.schema.internal.parser.MessageElement in project kgiraffe by rayokota.

the class GraphQLProtobufConverter method createOutputType.

@Override
public GraphQLOutputType createOutputType(SchemaContext ctx, Either<Type, ParsedSchema> schema) {
    ProtobufSchema protobufSchema = (ProtobufSchema) schema.get();
    if (hasMultipleMessageTypes(protobufSchema)) {
        String name = ctx.qualify(protobufSchema.fullName() + "_union");
        GraphQLObjectType.Builder builder = GraphQLObjectType.newObject().name(name);
        // set to false before recursing
        ctx.setRoot(false);
        for (TypeElement type : protobufSchema.rawSchema().getTypes()) {
            if (type instanceof MessageElement) {
                String fieldName = type.getName();
                Descriptor descriptor = protobufSchema.toDescriptor(type.getName());
                builder.field(GraphQLFieldDefinition.newFieldDefinition().name(fieldName).type(createOutputRecord(ctx, descriptor)).build());
            }
        }
        return builder.build();
    } else {
        return createOutputRecord(ctx, ((ProtobufSchema) schema.get()).toDescriptor());
    }
}
Also used : TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) GraphQLObjectType(graphql.schema.GraphQLObjectType) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) NamedProtobufSchema(io.kgraph.kgiraffe.util.proto.NamedProtobufSchema) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement)

Example 2 with MessageElement

use of com.squareup.wire.schema.internal.parser.MessageElement in project kgiraffe by rayokota.

the class GraphQLProtobufConverter method createInputType.

@Override
public GraphQLInputType createInputType(SchemaContext ctx, Either<Type, ParsedSchema> schema) {
    ProtobufSchema protobufSchema = (ProtobufSchema) schema.get();
    if (hasMultipleMessageTypes(protobufSchema)) {
        String name = ctx.qualify(protobufSchema.fullName() + "_union");
        GraphQLInputObjectType.Builder builder = GraphQLInputObjectType.newInputObject().name(name).field(GraphQLInputObjectField.newInputObjectField().name(Logical.OR.symbol()).description("Logical operation for expressions").type(new GraphQLList(new GraphQLTypeReference(name))).build()).field(GraphQLInputObjectField.newInputObjectField().name(Logical.AND.symbol()).description("Logical operation for expressions").type(new GraphQLList(new GraphQLTypeReference(name))).build());
        // set to false before recursing
        ctx.setRoot(false);
        for (TypeElement type : protobufSchema.rawSchema().getTypes()) {
            if (type instanceof MessageElement) {
                String fieldName = type.getName();
                Descriptor descriptor = protobufSchema.toDescriptor(type.getName());
                builder.field(GraphQLInputObjectField.newInputObjectField().name(fieldName).type(createInputRecord(ctx, descriptor)).build());
            }
        }
        return builder.build();
    } else {
        return createInputRecord(ctx, ((ProtobufSchema) schema.get()).toDescriptor());
    }
}
Also used : GraphQLList(graphql.schema.GraphQLList) GraphQLTypeReference(graphql.schema.GraphQLTypeReference) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) GraphQLInputObjectType(graphql.schema.GraphQLInputObjectType) ProtobufSchema(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema) NamedProtobufSchema(io.kgraph.kgiraffe.util.proto.NamedProtobufSchema) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement)

Example 3 with MessageElement

use of com.squareup.wire.schema.internal.parser.MessageElement 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 4 with MessageElement

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

the class ProtoTransportPayloadConfiguration method getDynamicMessageBuilder.

public DynamicMessage.Builder getDynamicMessageBuilder(String protoSchema, String schemaName) {
    ProtoFileElement protoFileElement = getTransportProtoSchema(protoSchema);
    DynamicSchema dynamicSchema = getDynamicSchema(protoFileElement, schemaName);
    String lastMsgName = getMessageTypes(protoFileElement.getTypes()).stream().map(MessageElement::getName).reduce((previous, last) -> last).get();
    return dynamicSchema.newMessageBuilder(lastMsgName);
}
Also used : ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement) DynamicMessage(com.google.protobuf.DynamicMessage) Descriptors(com.google.protobuf.Descriptors) TransportPayloadType(org.thingsboard.server.common.data.TransportPayloadType) ProtoParser(com.squareup.wire.schema.internal.parser.ProtoParser) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) EnumElement(com.squareup.wire.schema.internal.parser.EnumElement) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Location(com.squareup.wire.schema.Location) EnumConstantElement(com.squareup.wire.schema.internal.parser.EnumConstantElement) FieldElement(com.squareup.wire.schema.internal.parser.FieldElement) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) EnumDefinition(com.github.os72.protobuf.dynamic.EnumDefinition) TypeElement(com.squareup.wire.schema.internal.parser.TypeElement) Data(lombok.Data) MessageDefinition(com.github.os72.protobuf.dynamic.MessageDefinition) OneOfElement(com.squareup.wire.schema.internal.parser.OneOfElement) Collections(java.util.Collections) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) DynamicSchema(com.github.os72.protobuf.dynamic.DynamicSchema) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement)

Example 5 with MessageElement

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

the class ProtobufSchemaParser method parseSchema.

/**
 * @see io.apicurio.registry.serde.SchemaParser#parseSchema(byte[])
 */
@Override
public ProtobufSchema parseSchema(byte[] rawSchema, Map<String, ParsedSchema<ProtobufSchema>> resolvedReferences) {
    try {
        // textual .proto file
        ProtoFileElement fileElem = ProtoParser.Companion.parse(FileDescriptorUtils.DEFAULT_LOCATION, IoUtil.toString(rawSchema));
        Map<String, ProtoFileElement> dependencies = new HashMap<>();
        resolvedReferences.forEach((key, value) -> {
            dependencies.put(key, value.getParsedSchema().getProtoFileElement());
            if (value.hasReferences()) {
                addReferencesToDependencies(value.getSchemaReferences(), dependencies);
            }
        });
        MessageElement firstMessage = FileDescriptorUtils.firstMessage(fileElem);
        if (firstMessage != null) {
            try {
                final Descriptors.Descriptor fileDescriptor = FileDescriptorUtils.toDescriptor(firstMessage.getName(), fileElem, dependencies);
                return new ProtobufSchema(fileDescriptor.getFile(), fileElem);
            } catch (IllegalStateException ise) {
                // If we fail to init the dynamic schema, try to get the descriptor from the proto element
                return getFileDescriptorFromElement(fileElem);
            }
        } else {
            return getFileDescriptorFromElement(fileElem);
        }
    } catch (DescriptorValidationException pe) {
        throw new SerializationException("Error parsing protobuf schema ", pe);
    }
}
Also used : SerializationException(org.apache.kafka.common.errors.SerializationException) HashMap(java.util.HashMap) DescriptorValidationException(com.google.protobuf.Descriptors.DescriptorValidationException) ProtobufSchema(io.apicurio.registry.utils.protobuf.schema.ProtobufSchema) MessageElement(com.squareup.wire.schema.internal.parser.MessageElement) Descriptors(com.google.protobuf.Descriptors) ProtoFileElement(com.squareup.wire.schema.internal.parser.ProtoFileElement)

Aggregations

MessageElement (com.squareup.wire.schema.internal.parser.MessageElement)30 TypeElement (com.squareup.wire.schema.internal.parser.TypeElement)25 EnumElement (com.squareup.wire.schema.internal.parser.EnumElement)21 FieldElement (com.squareup.wire.schema.internal.parser.FieldElement)14 ProtoFileElement (com.squareup.wire.schema.internal.parser.ProtoFileElement)14 ServiceElement (com.squareup.wire.schema.internal.parser.ServiceElement)14 HashSet (java.util.HashSet)14 OptionElement (com.squareup.wire.schema.internal.parser.OptionElement)12 OneOfElement (com.squareup.wire.schema.internal.parser.OneOfElement)11 ArrayList (java.util.ArrayList)11 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)10 Syntax (com.squareup.wire.Syntax)10 ImmutableList (com.google.common.collect.ImmutableList)9 DescriptorProto (com.google.protobuf.DescriptorProtos.DescriptorProto)9 EnumDescriptorProto (com.google.protobuf.DescriptorProtos.EnumDescriptorProto)9 EnumValueDescriptorProto (com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto)9 FieldDescriptorProto (com.google.protobuf.DescriptorProtos.FieldDescriptorProto)9 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)9 MethodDescriptorProto (com.google.protobuf.DescriptorProtos.MethodDescriptorProto)9 OneofDescriptorProto (com.google.protobuf.DescriptorProtos.OneofDescriptorProto)9