Record Layer with DynamicMessage?

Suppose we are using a dynamically generated message like:

MessageDefinition msgDef = MessageDefinition.newBuilder("Person") // message Person
    .addField("required", "int32", "id", 1)    // required int32 id = 1
    .addField("required", "string", "name", 2)  // required string name = 2
    .addField("optional", "string", "email", 3)  // optional string email = 3
    .build();

schemaBuilder.addMessageDefinition(msgDef);
DynamicSchema schema = schemaBuilder.build();

// Create dynamic message from schema
DynamicMessage.Builder msgBuilder = schema.newMessageBuilder("Person");
Descriptors.Descriptor msgDesc = msgBuilder.getDescriptorForType();
DynamicMessage msg = msgBuilder
    .setField(msgDesc.findFieldByName("id"), 1)
    .setField(msgDesc.findFieldByName("name"), "Alan Turing")
    .setField(msgDesc.findFieldByName("email"), "at@sis.gov.uk")
    .build();

But how could we make it work with record layer? I suppose we need to pass the schema of this message to the record layer as metadata. But it looks like that there is no corresponding API for it?

Or ultimately, how could we dynamically create schema instead of using statically generated schema?

Well, I’ve figured it out :slight_smile: We can get the FileDescriptor from msgDesc:

Descriptors.FileDescriptor fileDescriptor = msgDesc.getFile();
2 Likes