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());
}
}
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());
}
}
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);
}
}
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);
}
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);
}
}
Aggregations