You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by Wayne Kao <wa...@corp.spock.com> on 2008/09/24 00:27:34 UTC

Namespacing generated Ruby types

In our .thrift file we have a struct like this:

struct Name{
  1: string first,
  2: string middle,
  3: string last
}


That generates Ruby code like this (used the latest Thrift compiler from
trunk):

require 'thrift/protocol'

class Name
  include Thrift::Struct
  Thrift::Struct.field_accessor self, :first, :middle, :last
  FIELDS = {
    1 => {:type => Thrift::Types::STRING, :name => 'first'},
    2 => {:type => Thrift::Types::STRING, :name => 'middle'},
    3 => {:type => Thrift::Types::STRING, :name => 'last'}
  }
end


Then, in our main (Rails) app, we include it like this at the top of a file:
$:.push("#{RAILS_ROOT}/../blah/gen-rb")
require 'Blah'
require 'blah_constants'

Unfortunately, because Ruby allows reopening classes, if we have another
class called "Name" (like a Rails model), all sorts of subtle badness
happens.  The person who created the original .thrift file probably doesn't
notice either since they may not even be a Ruby programmer.

Is there a good pattern for namespacing the generated types such that they
aren't in the global namespace?  Or does everyone just come up with fancy
naming schemes for their Thrift types (e.g. "BlahName" instead of "Name")?
Is this something we should try tackling in the compiler?

--Wayne


RE: Namespacing generated Ruby types

Posted by Wayne Kao <wa...@corp.spock.com>.
Sick.  That's exactly what I was looking for.

Thanks,

--Wayne

-----Original Message-----
From: Bryan Duxbury [mailto:bryan@rapleaf.com] 
Sent: Tuesday, September 23, 2008 3:46 PM
To: thrift-dev@incubator.apache.org
Subject: Re: Namespacing generated Ruby types

If you put "namespace rb Blah" in your .thrift file, the generated  
classes will live in that module.

-Bryan

On Sep 23, 2008, at 3:27 PM, Wayne Kao wrote:

> In our .thrift file we have a struct like this:
>
> struct Name{
>   1: string first,
>   2: string middle,
>   3: string last
> }
>
>
> That generates Ruby code like this (used the latest Thrift compiler  
> from
> trunk):
>
> require 'thrift/protocol'
>
> class Name
>   include Thrift::Struct
>   Thrift::Struct.field_accessor self, :first, :middle, :last
>   FIELDS = {
>     1 => {:type => Thrift::Types::STRING, :name => 'first'},
>     2 => {:type => Thrift::Types::STRING, :name => 'middle'},
>     3 => {:type => Thrift::Types::STRING, :name => 'last'}
>   }
> end
>
>
> Then, in our main (Rails) app, we include it like this at the top  
> of a file:
> $:.push("#{RAILS_ROOT}/../blah/gen-rb")
> require 'Blah'
> require 'blah_constants'
>
> Unfortunately, because Ruby allows reopening classes, if we have  
> another
> class called "Name" (like a Rails model), all sorts of subtle badness
> happens.  The person who created the original .thrift file probably  
> doesn't
> notice either since they may not even be a Ruby programmer.
>
> Is there a good pattern for namespacing the generated types such  
> that they
> aren't in the global namespace?  Or does everyone just come up with  
> fancy
> naming schemes for their Thrift types (e.g. "BlahName" instead of  
> "Name")?
> Is this something we should try tackling in the compiler?
>
> --Wayne
>


Re: Namespacing generated Ruby types

Posted by Bryan Duxbury <br...@rapleaf.com>.
If you put "namespace rb Blah" in your .thrift file, the generated  
classes will live in that module.

-Bryan

On Sep 23, 2008, at 3:27 PM, Wayne Kao wrote:

> In our .thrift file we have a struct like this:
>
> struct Name{
>   1: string first,
>   2: string middle,
>   3: string last
> }
>
>
> That generates Ruby code like this (used the latest Thrift compiler  
> from
> trunk):
>
> require 'thrift/protocol'
>
> class Name
>   include Thrift::Struct
>   Thrift::Struct.field_accessor self, :first, :middle, :last
>   FIELDS = {
>     1 => {:type => Thrift::Types::STRING, :name => 'first'},
>     2 => {:type => Thrift::Types::STRING, :name => 'middle'},
>     3 => {:type => Thrift::Types::STRING, :name => 'last'}
>   }
> end
>
>
> Then, in our main (Rails) app, we include it like this at the top  
> of a file:
> $:.push("#{RAILS_ROOT}/../blah/gen-rb")
> require 'Blah'
> require 'blah_constants'
>
> Unfortunately, because Ruby allows reopening classes, if we have  
> another
> class called "Name" (like a Rails model), all sorts of subtle badness
> happens.  The person who created the original .thrift file probably  
> doesn't
> notice either since they may not even be a Ruby programmer.
>
> Is there a good pattern for namespacing the generated types such  
> that they
> aren't in the global namespace?  Or does everyone just come up with  
> fancy
> naming schemes for their Thrift types (e.g. "BlahName" instead of  
> "Name")?
> Is this something we should try tackling in the compiler?
>
> --Wayne
>


Re: Namespacing generated Ruby types

Posted by Amit Sudharshan <am...@onellama.com>.
We prepend all of our structs with "Thrift" to avoid this problem, in Java
we put the classes into an AutoGenerated package.

-Amit

On Tue, Sep 23, 2008 at 5:27 PM, Wayne Kao <wa...@corp.spock.com> wrote:

> In our .thrift file we have a struct like this:
>
> struct Name{
>  1: string first,
>  2: string middle,
>  3: string last
> }
>
>
> That generates Ruby code like this (used the latest Thrift compiler from
> trunk):
>
> require 'thrift/protocol'
>
> class Name
>  include Thrift::Struct
>  Thrift::Struct.field_accessor self, :first, :middle, :last
>  FIELDS = {
>    1 => {:type => Thrift::Types::STRING, :name => 'first'},
>    2 => {:type => Thrift::Types::STRING, :name => 'middle'},
>    3 => {:type => Thrift::Types::STRING, :name => 'last'}
>  }
> end
>
>
> Then, in our main (Rails) app, we include it like this at the top of a
> file:
> $:.push("#{RAILS_ROOT}/../blah/gen-rb")
> require 'Blah'
> require 'blah_constants'
>
> Unfortunately, because Ruby allows reopening classes, if we have another
> class called "Name" (like a Rails model), all sorts of subtle badness
> happens.  The person who created the original .thrift file probably doesn't
> notice either since they may not even be a Ruby programmer.
>
> Is there a good pattern for namespacing the generated types such that they
> aren't in the global namespace?  Or does everyone just come up with fancy
> naming schemes for their Thrift types (e.g. "BlahName" instead of "Name")?
> Is this something we should try tackling in the compiler?
>
> --Wayne
>
>