You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Kristinn Örn Sigurðsson <kr...@gmail.com> on 2009/05/27 04:59:19 UTC

Dependencies

Hi all.
I'm having a little trouble with dependencies in Thrift. See this for an
example:

struct TAccount {
1: i32 id,
2: string email,
3: string name,
4: string password,
5: list<TPhone> phones,
6: string address,
7: string postalCode,
8: string city,
9: string country,
10: list<TCritic> critics
}

struct TCritic {
1: i32 id,
2: TAccount author,
3: i32 rating,
4: string text
}

struct TCompany {
1: i32 id,
2: string name,
3: string email,
4: string phone,
5: string address,
6: string postalCode,
7: string city,
8: string country,
9: list<TAccount> employees,
10: list<TCritic> critics
}

This is just a partial copy/paste. Some structs aren't shown here.

This doesn't compile into Thrift files. The problem here is that TAccount
depends on TCritic and TCritic depends on TAccount. The thing is that
structs can't see other structs that are defined after (below) them. Is
there a workaround for this?

Thanks in advance.

Regards,
Kristinn.

Re: Dependencies

Posted by Kristinn Örn Sigurðsson <kr...@gmail.com>.
Hi Mark.
Thanks for your quick answer and your advice.

The reason I'm making TCritic hold an TAccount struct is because I'm using
java and hibernate as backend for this web application and hibernate makes
nice objects for me. The TCritic struct is identical to the object hibernate
returns to me.

I understand the limitation. I'll find some workaround for this. :-)

Btw. Thanks for this nice program.

Regards,
Kristinn.

On Wed, May 27, 2009 at 3:17 AM, Mark Slee <ms...@facebook.com> wrote:

> This is a known limitation. Thrift does not allow forward declarations.
> Forward declarations require using pointers and having all Thrift structs be
> nullable.
>
> This code couldn't be compiled into C++, because the data members of Thrift
> structs in C++ are true value members, not pointers or references.
>
> It looks like what you really want here might be something more like this:
>
> struct TCritic {
> 1: i32 id,
> 2: i32 author_id, // the ID of the author, not the actual object
> 3: i32 rating,
> 4: string text
> }
>
> Similarly, you might want to just store a list of critic_ids in the
> TAccount.
>
> -----Original Message-----
> From: Kristinn Örn Sigurðsson [mailto:kristinn.orn.sigurdsson@gmail.com]
> Sent: Tuesday, May 26, 2009 7:59 PM
> To: thrift-user@incubator.apache.org
> Subject: Dependencies
>
> Hi all.
> I'm having a little trouble with dependencies in Thrift. See this for an
> example:
>
> struct TAccount {
> 1: i32 id,
> 2: string email,
> 3: string name,
> 4: string password,
> 5: list<TPhone> phones,
> 6: string address,
> 7: string postalCode,
> 8: string city,
> 9: string country,
> 10: list<TCritic> critics
> }
>
> struct TCritic {
> 1: i32 id,
> 2: TAccount author,
> 3: i32 rating,
> 4: string text
> }
>
> struct TCompany {
> 1: i32 id,
> 2: string name,
> 3: string email,
> 4: string phone,
> 5: string address,
> 6: string postalCode,
> 7: string city,
> 8: string country,
> 9: list<TAccount> employees,
> 10: list<TCritic> critics
> }
>
> This is just a partial copy/paste. Some structs aren't shown here.
>
> This doesn't compile into Thrift files. The problem here is that TAccount
> depends on TCritic and TCritic depends on TAccount. The thing is that
> structs can't see other structs that are defined after (below) them. Is
> there a workaround for this?
>
> Thanks in advance.
>
> Regards,
> Kristinn.
>

RE: Dependencies

Posted by Mark Slee <ms...@facebook.com>.
This is a known limitation. Thrift does not allow forward declarations. Forward declarations require using pointers and having all Thrift structs be nullable.

This code couldn't be compiled into C++, because the data members of Thrift structs in C++ are true value members, not pointers or references.

It looks like what you really want here might be something more like this:

struct TCritic {
1: i32 id,
2: i32 author_id, // the ID of the author, not the actual object
3: i32 rating,
4: string text
}

Similarly, you might want to just store a list of critic_ids in the TAccount.

-----Original Message-----
From: Kristinn Örn Sigurðsson [mailto:kristinn.orn.sigurdsson@gmail.com] 
Sent: Tuesday, May 26, 2009 7:59 PM
To: thrift-user@incubator.apache.org
Subject: Dependencies

Hi all.
I'm having a little trouble with dependencies in Thrift. See this for an
example:

struct TAccount {
1: i32 id,
2: string email,
3: string name,
4: string password,
5: list<TPhone> phones,
6: string address,
7: string postalCode,
8: string city,
9: string country,
10: list<TCritic> critics
}

struct TCritic {
1: i32 id,
2: TAccount author,
3: i32 rating,
4: string text
}

struct TCompany {
1: i32 id,
2: string name,
3: string email,
4: string phone,
5: string address,
6: string postalCode,
7: string city,
8: string country,
9: list<TAccount> employees,
10: list<TCritic> critics
}

This is just a partial copy/paste. Some structs aren't shown here.

This doesn't compile into Thrift files. The problem here is that TAccount
depends on TCritic and TCritic depends on TAccount. The thing is that
structs can't see other structs that are defined after (below) them. Is
there a workaround for this?

Thanks in advance.

Regards,
Kristinn.

Re: Dependencies

Posted by Kristinn Örn Sigurðsson <kr...@gmail.com>.
Thank you for your answer Ben.
This seems to work (at least it compiles). I haven't had the time to test it
yet but so far it seems like it's going to work. :-)

Thanks again.

Regards,
Kristinn.

On Thu, May 28, 2009 at 12:22 AM, Ben Taitelbaum <bt...@cs.oberlin.edu>wrote:

> When I've needed to do something like this, and there is a cycle (so I
> can't include another file, or declare things in order), I've just been
> declaring a stub. It works for me in ruby, and I just tried it in java and
> the generated code looked decent (but I didn't try compililng it).
>
> struct B {}
> struct A {
>  10: List<B> myBList
> }
> struct B{
>  10: A myA
> }
>
> Does this work in other languages, or do you end up with a situation where
> some classes are multiply / incorrectly defined? If so, is it something you
> can fix with some sed?
>
> -Ben
>
>
> On May 26, 2009, at 10:59 PM, Kristinn Örn Sigurðsson wrote:
>
>  Hi all.
>> I'm having a little trouble with dependencies in Thrift. See this for an
>> example:
>>
>> struct TAccount {
>> 1: i32 id,
>> 2: string email,
>> 3: string name,
>> 4: string password,
>> 5: list<TPhone> phones,
>> 6: string address,
>> 7: string postalCode,
>> 8: string city,
>> 9: string country,
>> 10: list<TCritic> critics
>> }
>>
>> struct TCritic {
>> 1: i32 id,
>> 2: TAccount author,
>> 3: i32 rating,
>> 4: string text
>> }
>>
>> struct TCompany {
>> 1: i32 id,
>> 2: string name,
>> 3: string email,
>> 4: string phone,
>> 5: string address,
>> 6: string postalCode,
>> 7: string city,
>> 8: string country,
>> 9: list<TAccount> employees,
>> 10: list<TCritic> critics
>> }
>>
>> This is just a partial copy/paste. Some structs aren't shown here.
>>
>> This doesn't compile into Thrift files. The problem here is that TAccount
>> depends on TCritic and TCritic depends on TAccount. The thing is that
>> structs can't see other structs that are defined after (below) them. Is
>> there a workaround for this?
>>
>> Thanks in advance.
>>
>> Regards,
>> Kristinn.
>>
>
>

Re: Dependencies

Posted by Ben Taitelbaum <bt...@cs.oberlin.edu>.
When I've needed to do something like this, and there is a cycle (so I  
can't include another file, or declare things in order), I've just  
been declaring a stub. It works for me in ruby, and I just tried it in  
java and the generated code looked decent (but I didn't try compililng  
it).

struct B {}
struct A {
  10: List<B> myBList
}
struct B{
  10: A myA
}

Does this work in other languages, or do you end up with a situation  
where some classes are multiply / incorrectly defined? If so, is it  
something you can fix with some sed?

-Ben

On May 26, 2009, at 10:59 PM, Kristinn Örn Sigurðsson wrote:

> Hi all.
> I'm having a little trouble with dependencies in Thrift. See this  
> for an
> example:
>
> struct TAccount {
> 1: i32 id,
> 2: string email,
> 3: string name,
> 4: string password,
> 5: list<TPhone> phones,
> 6: string address,
> 7: string postalCode,
> 8: string city,
> 9: string country,
> 10: list<TCritic> critics
> }
>
> struct TCritic {
> 1: i32 id,
> 2: TAccount author,
> 3: i32 rating,
> 4: string text
> }
>
> struct TCompany {
> 1: i32 id,
> 2: string name,
> 3: string email,
> 4: string phone,
> 5: string address,
> 6: string postalCode,
> 7: string city,
> 8: string country,
> 9: list<TAccount> employees,
> 10: list<TCritic> critics
> }
>
> This is just a partial copy/paste. Some structs aren't shown here.
>
> This doesn't compile into Thrift files. The problem here is that  
> TAccount
> depends on TCritic and TCritic depends on TAccount. The thing is that
> structs can't see other structs that are defined after (below) them.  
> Is
> there a workaround for this?
>
> Thanks in advance.
>
> Regards,
> Kristinn.