3

In my proto file, I want to define a map as a custom option, tried a few things but none is working.

my metadata proto file:

syntax = "proto2";
import "google/protobuf/descriptor.proto";

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

message MyMeta {
  optional bool needValidation = 1;
  map<string, string> fileMap = 2;
}

extend google.protobuf.FieldOptions {
  optional MyMeta meta = 80412; 
}

my proto file

syntax = "proto3";

package com.test;

import "util/meta.proto";
import "google/protobuf/timestamp.proto";

message MyMeta {
  int32 id = 1 [(com.util.meta).needValidation = false, /*looking for a way to set a map (com.meta).tableOptions = {"t,raw_orders"}]*/;

}

Is this possible?

JDev
  • 1,662
  • 4
  • 25
  • 55

2 Answers2

3

This works:

message MyMeta {
  int32 id = 1 [
        (com.util.meta) = {
                needValidation: false,
                fileMap: [
                        {
                                key: "t",
                                value: "raw_orders"
                        }
                ];
        }];
}

The protobuf map is syntactic sugar for a repeated field of map entry messages with key and value fields. 1

In your case, your meta message

message MyMeta {
  optional bool needValidation = 1;
  map<string, string> fileMap = 2;
}

is equivalent to

message MyMeta {
  optional bool needValidation = 1;

  message FileMapEntry {
    string key = 1;
    string value = 2;
  }
  repeated FileMapEntry fileMap = 2;
}

Of course, it would be much nicer if there was a more obvious way to specify a map value in an option.

Alexander Klauer
  • 957
  • 11
  • 18
0

Seems it is not possible. To make my logic, I created a string like "StoreOrders:raw_orders_test,OrderItems:raw_order_items_test


message MyMeta {
  int32 id = 1 [(com.util.meta).needValidation = false, (com.meta).tableOptions = "TableA:valueA,TableB:valueB;
}

and in my jav code and splitting the string to create a hash map.

JDev
  • 1,662
  • 4
  • 25
  • 55