thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Philip Polkovnikov <polkovnikov...@gmail.com>
Subject Re: How to return more than one value in a Thrift service method?
Date Thu, 01 Oct 2015 12:25:57 GMT
Hello Gajanan,

It's a pity that Thrift doesn't support generics, because it would ideally be

struct Status<T> {
    1: StatusCode Code,
    2: T Value
}

service ImportantService {
    Status<ImportantData> GetImportantData()
}

The absence of generics lower code reuse, because there's no way to
generically handle StatusCode's if there're methods with several
return value types. I've heard that generics was thought to be not a
very good idea due to a lack of generics (or static typing at all) in
most of supported languages, and one would need to create proxy types.
Protobuf.net uses this workaround for generics. It generates proxies
like List_Int32. Thrift already has some builtin support for lists and
maps, and we could abstract implementation approach from these.

Another solution would be at least to create a pair<T, U> builtin
type. That would allow us to write

struct Status {
    1: StatusCode Code
     /* extension is possible here */
}

service ImportantService {
    pair<ImportantData, Status> GetImportantData()
}

So in case you're concerned with this issue, you could add any of
these features into a generator for the languages you need. We had a
need for attributes on fields and structs in production, so we've
added support into C# and JSON generators, and it was quite easy.
Generator's source code is pretty simple, it's written in a small
subset of C++ without extra dependencies.

2015-10-01 14:24 GMT+03:00 Randy Abernethy <randy.abernethy@gmail.com>:
> Hello Gajanan,
>
> Many consider it a best practice to return a struct in thrift service
> methods. This allows you to return multiple values and to evolve the things
> returned over time. Adding new attributes to a thrift struct is a backwards
> compatible operation.
>
> Best,
> Randy
>
> On Thu, Oct 1, 2015 at 3:32 AM, Gajanan H <grhegde09@gmail.com> wrote:
>
>> Hi,
>>
>> I have a thrift service method `ImportantData GetImportantData()` defined
>> in the IDL. I would like to return a Status code for this method. I would
>> like to be doing some thing like `Status GetImportantData(ImportantData&)`
>>
>> Is there a way to do that ? Pardon me I am very new to Thrift. BTW I am
>> using C++ as language.
>>
>> Regards,
>> Gajanan
>>

Mime
View raw message