You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by J <mv...@gmail.com> on 2015/11/07 00:01:10 UTC

Thrift custom types

Hi, I'm wondering what's the best pattern to add methods to Thrift types.
Say I some Thrift types like:

  struct MyStruct {
    1: i32 data1
    2: string data2
  }

Say I want to add methods to the generated Java object such as
"get10XData1()" that just returns 10x data1. Is there some way to do that
or do I have to hack the thrift compiler and libraries?

Similarly, say I want to have a custom type with custom
serialization/deserialization. Is there any way to do that without editing
thrift?

Thanks.

Re: Thrift custom types

Posted by Jens Geyer <je...@hotmail.com>.
Seems that Java does not even allow for extension methods. Too bad:
http://stackoverflow.com/questions/4359979/java-equivalent-to-c-sharp-extension-methods




-----Ursprüngliche Nachricht----- 
From: Jens Geyer
Sent: Saturday, November 7, 2015 1:56 PM
To: user@thrift.apache.org
Subject: Re: Thrift custom types

Hi,

For some languages Thrift generates partial classes, e.g. for C#. Those can
be easily extended, but not all languages (Embarcadero, I'm looking at you)
and not all language bindings support this, unfortunately.

Have fun,
JensG


-----Ursprüngliche Nachricht----- 
From: BCG
Sent: Saturday, November 7, 2015 3:01 AM
To: user@thrift.apache.org
Subject: Re: Thrift custom types



On 11/6/2015 at 6:01 PM, "J" <mv...@gmail.com> wrote:

>
>Say I want to add methods to the generated Java object such as
>"get10XData1()" that just returns 10x data1. Is there some way to
>do that
>or do I have to hack the thrift compiler and libraries?
>
>Similarly, say I want to have a custom type with custom
>serialization/deserialization. Is there any way to do that without
>editing
>thrift?

If you're using Java, easier than hacking the compiler and libraries would
be to use Facebook's Swift:

https://github.com/facebook/swift

That allows you define your structs as annotated Java classes, and you can
add whatever utility methods you like.  Also you can define "builders" that
could be useful to customize serialization and deserialization.

If you want to be able to annotate interfaces and abstract classes instead
of just concrete classes, you might try my fork of that tool (I sent a pull
request to upstream for that feature but they don't seem interested):

https://github.com/bgould/swift

If you don't want to use a tool like that and instead hack on the compiler -
for the utility methods you could probably have the Java generator recognize
an annotation that causes the generated struct to extend from some abstract
base class, or if you are on Java 8 it could be an interface with default
methods.  To do custom serialization would be trickier but not impossible I
think.

Thanks

Ben


Re: Thrift custom types

Posted by Jens Geyer <je...@hotmail.com>.
Hi,

For some languages Thrift generates partial classes, e.g. for C#. Those can 
be easily extended, but not all languages (Embarcadero, I'm looking at you) 
and not all language bindings support this, unfortunately.

Have fun,
JensG


-----Ursprüngliche Nachricht----- 
From: BCG
Sent: Saturday, November 7, 2015 3:01 AM
To: user@thrift.apache.org
Subject: Re: Thrift custom types



On 11/6/2015 at 6:01 PM, "J" <mv...@gmail.com> wrote:

>
>Say I want to add methods to the generated Java object such as
>"get10XData1()" that just returns 10x data1. Is there some way to
>do that
>or do I have to hack the thrift compiler and libraries?
>
>Similarly, say I want to have a custom type with custom
>serialization/deserialization. Is there any way to do that without
>editing
>thrift?

If you're using Java, easier than hacking the compiler and libraries would 
be to use Facebook's Swift:

https://github.com/facebook/swift

That allows you define your structs as annotated Java classes, and you can 
add whatever utility methods you like.  Also you can define "builders" that 
could be useful to customize serialization and deserialization.

If you want to be able to annotate interfaces and abstract classes instead 
of just concrete classes, you might try my fork of that tool (I sent a pull 
request to upstream for that feature but they don't seem interested):

https://github.com/bgould/swift

If you don't want to use a tool like that and instead hack on the compiler - 
for the utility methods you could probably have the Java generator recognize 
an annotation that causes the generated struct to extend from some abstract 
base class, or if you are on Java 8 it could be an interface with default 
methods.  To do custom serialization would be trickier but not impossible I 
think.

Thanks

Ben


Re: Thrift custom types

Posted by BCG <bg...@hushmail.com>.

On 11/6/2015 at 6:01 PM, "J" <mv...@gmail.com> wrote:

>
>Say I want to add methods to the generated Java object such as
>"get10XData1()" that just returns 10x data1. Is there some way to 
>do that
>or do I have to hack the thrift compiler and libraries?
>
>Similarly, say I want to have a custom type with custom
>serialization/deserialization. Is there any way to do that without 
>editing
>thrift?

If you're using Java, easier than hacking the compiler and libraries would be to use Facebook's Swift:

https://github.com/facebook/swift

That allows you define your structs as annotated Java classes, and you can add whatever utility methods you like.  Also you can define "builders" that could be useful to customize serialization and deserialization.

If you want to be able to annotate interfaces and abstract classes instead of just concrete classes, you might try my fork of that tool (I sent a pull request to upstream for that feature but they don't seem interested):

https://github.com/bgould/swift

If you don't want to use a tool like that and instead hack on the compiler - for the utility methods you could probably have the Java generator recognize an annotation that causes the generated struct to extend from some abstract base class, or if you are on Java 8 it could be an interface with default methods.  To do custom serialization would be trickier but not impossible I think.

Thanks

Ben