4

I am using protobuf-gradle-plugin to generate java class from the proto files.

My proto file looks like

syntax = "proto3";

package com.address;
option java_package = "com.address";


message AddressesMessage {
 int32 id = 1;
 string address_line_1 = 4;
 string address_line_2 = 5;
 string city = 7;
 string postal_code = 9;
 string country = 10;
}

The plugin is generating classes for me but now I want to add some metadata information at field level. Like

syntax = "proto3";

package com.address;
option java_package = "com.address";


message AddressesMessage {
 int32 id = 1 [ (meta) = { isfact: false }];
 string address_line_1 = 4;
 string address_line_2 = 5;
 string city = 7;
 string postal_code = 9;
 string country = 10;
}

Is this possible?

JDev
  • 1,662
  • 4
  • 25
  • 55

1 Answers1

1

Yes, that is possible via custom options, but: you'd need to define your custom options in a separate proto2 schema, which the proto3 schema then imports.

Something like (for your proto2 schema, untested):

syntax = "proto2";
import "google/protobuf/descriptor.proto";
package MetaPackage;
message MyMeta {
  optional bool isFact = 1;
}
extend google.protobuf.FieldOptions {
  optional MyMeta meta = 80412; // numbering: search for "One last thing" in the link above
}

then just add:

import "MyMeta.proto";

to your proto3 schema, and it should work. Accessing the metadata is another topic, though! See the link above.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900