Search code examples
thrift

Thrift IDL : service response as list


In thrift IDL, can the service response be list or map also?

Because, normally I have seen it to be some struct or some primitive types like string, double, etc.

Also, what is the source from where I can verify? Please also mention the source.


Solution

  • Sure it can:

    service FooBar {
        list<double>  Foobarizer()
    }
    

    is perfectly legal.

    So why are people using structs then? Well, it makes the API extensible. And this has to do with what is called "soft versioning".

    Here's an Example:

    Consider you want to add another flag that gives some valuable additional information. How can we add it to the response? With only a list, you are lost, because changing it to something like

    struct FoobarizerReturn {
      1: list<double>  data
      2: bool theValuableInfo     // added in V2
    }
    
    service FooBar {
        //list<double>  Foobarizer()
        FoobarizerReturn  Foobarizer()
    }
    

    will essentially break the API for older clients. To keep it compatible, you would have to change your IDL instead like so:

    service FooBar {
        /** V1 deprecated */
        list<double>  Foobarizer()  
        /** V2 use this now */
        FoobarizerReturn  FoobarizerV2()
    }
    

    which has at least three disadvantages:

    • it requires additional code at the server end
    • it causes additional work at the client's end
    • is just butt-ugly (that's a technical term)

    Hence, the most forward compatible solution is to start with a struct from the beginning, which can later easily be extended as needed w/o breaking compatibility.

    struct FoobarizerReturn {
      1: list<double>  data
       // more data to come later
    }
    
    service FooBar {
        FoobarizerReturn  Foobarizer()
    }