1

I have a pipeline that has two kind of thrift message coming in

I can try deserializing each individually and see if it runs into error

TDeserializer deserializer = new TDeserializer(new TSimpleJSONProtocol.Factory());

try {
   Type1Msg t1 = new Type1Msg();
   deserializer.deserialize(t1, json, "UTF-8");
   return t1
} catch() {

}

try {
   Type2Msg t2 = new Type2Msg();
   deserializer.deserialize(t1, json, "UTF-8");
   return t2
} catch() {

}

Is there a idiomatic way of achieving deserializing multiple message type in thrift?

user16367669
  • 57
  • 1
  • 8
  • 1
    I've never used Thrift, but a quick perusal of the Javadoc suggests you want a `TUnion` – Jim Garrison Nov 16 '21 at 20:38
  • ..but its abstract and constructor is protected ;(;(;(and has very poor javadoc) – xerx593 Nov 16 '21 at 20:49
  • if, you *knew* the (message) class name, you could do `TBase obj = (TBase) Class.forName(className).getConstructor().newInstance();` [like here(eof)](https://www.programcreek.com/java-api-examples/?code=pinterest%2Fsinger%2Fsinger-master%2Fsinger%2Fsrc%2Fmain%2Fjava%2Fcom%2Fpinterest%2Fsinger%2Ftools%2FThriftLogDumper.java) ..but you probably don't!(?) – xerx593 Nov 16 '21 at 20:51
  • different approach: Why do you have (only) one pipeline(/endpoint) for two message types!? ;) (if you cannot infer from the "endpoint", check the option to infer it from the "message/payload") – xerx593 Nov 16 '21 at 20:53
  • ..and what do you do with `t1` and/or `t2` respectively? – xerx593 Nov 16 '21 at 20:55

1 Answers1

0

I answered it for Python already over here, but in fact the topic is language-agnostic.

Use a Thrift union, alternatively a struct with non-required fields will also work:

union varying {
  1 : Type1Msg msg1
  2 : Type2Msg msg2
  // room for more 
}

Further reading:

JensG
  • 13,148
  • 4
  • 45
  • 55