0

I am trying to create a GRPC API where the payload can have custom contract per user. The user can define the contract by extending the base (fixed) contract up front (probably stored in a database) and start sending as per his custom contract.

I can define the base contract in the .proto file and generate the server and client classes at the compile time but is there any way I can enforce user defined contract at the time of de-serialization.

e.g.

BaseObject
 - Id
 - Date

UserObjectFoo
 - Id
 - Date
 - userAttr1

UserObjectBar
 - Id
 - Date
 - userAttr2

The definition of UserObjectFoo and UserObjectBar was provided after deployment so its not known to .proto.

Is there a way we could deserialize payload of type UserObjectFoo and UserObjectBar at the runtime after pulling out their definition from persistance.

Note: One rudimentary way is to have the custom fields like userAttr1 and userAttr2 as a key-value pair but it would have to be validated only after de-serialization into a Map by application logic, which could be an overhead.

i.e.

      {
        String id = 1
        Date   date = 2;
        map<string, string> attributes = 3;
      }
Neil
  • 5,919
  • 15
  • 58
  • 85
  • .proto has no concept of inheritance, so `BaseObject` and `UserObjectFoo` are *completely unrelated*, even if they look similar; what syntax version are you using? it could be that "extensions" are what you're after, but that is proto2 only; but presumably you're using `UserObjectBar` etc in the service definitions in the .proto, in which case... don't both ends already know the types? – Marc Gravell May 23 '19 at 10:18
  • From the proto perspective, the objects are completely unrelated, only problem is at compile time only ```BaseObject``` is known but at run time ```UserObjectBar``` may be needed to be transferred. So dynamically service definition needs to be created after deployment of the app before the call is made. – Neil May 23 '19 at 16:00

0 Answers0