Mailman ate the attachment, so I will try and summarize the idea:
--- /dev/null 2009-02-21 00:57:02.417841437 -0500
+++ foo.thrift 2009-07-01 18:28:08.000000000 -0400
@@ -0,0 +1,12 @@
+#!/usr/bin/env thrift
+
+struct Foo {
+ optional string a,
+ optional string b
+}
+
+service Bar {
+ #pragma ruby implode
+ void baz(1:Foo foo)
+}
+
--- /dev/null 2009-02-21 00:57:02.417841437 -0500
+++ rb/RubyClient.rb 2009-07-01 18:48:32.000000000 -0400
@@ -0,0 +1,45 @@
+#!/usr/bin/env ruby
+
+$:.push('../gen-rb')
+$:.push('./lib')
+
+require 'thrift'
+require 'thrift/protocol/binaryprotocol'
+
+class Module
+ def implode(method, struct)
+ define_method method do |args|
+ param = struct.new
+
+ args.each { |k,v| param.send("#{k}=",v) if param.respond_to?("#{k}=")
}
+
+ self.send("send_#{method}", param)
+ self.send("recv_#{method}")
+ end
+ end
+end
+
+require 'Bar'
+
+class Bar::Client
+ implode :baz, Foo
+end
+
+begin
+ port = ARGV[0] || 9090
+
+ transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost',
port))
+ protocol = Thrift::BinaryProtocol.new(transport)
+ client = Bar::Client.new(protocol)
+
+ transport.open()
+
+ client.baz(:a => "A")
+ client.baz(:b => "B", :xxx => "XXX" ) # Ignores undefined parameters
+ client.baz(:b => "X", :a => "Y")
+
+ transport.close()
+
+rescue Thrift::Exception => tx
+ print 'Thrift::Exception: ', tx.message, "\n"
+end
From: Michael Andrews <[email protected]>
Reply-To: <[email protected]>
Date: Wed, 1 Jul 2009 15:58:47 -0700
To: <[email protected]>, Matthieu Imbert
<[email protected]>
Subject: Re: optional fields in function declaration
Probably not *ideal* but depending on your client language, you could
"Monkey Patch" the generated thrift class, so that it accepts named
parameters and then populates the appropriate fields in the struct before
sending it to the server. I attached a straight forward and idiomatic
example in Ruby. If Thrift supported some sort of annotation mechanism, you
could specify the named parameter to struct transformation in the IDL.
Michael
________________________________
From: Matthieu Imbert <[email protected]>
Reply-To: <[email protected]>
Date: Wed, 1 Jul 2009 11:14:29 -0700
To: <[email protected]>
Subject: Re: optional fields in function declaration
Bryan Duxbury wrote:
> I would say that there's no question it would be useful. However, I'm
> not sure how that would translate to all client languages.
>
> A completely safe and reliable way to simulate this would be to make a
> new struct for your method that has optional fields and just use that as
> the only parameter to the method. Does that make sense?
Yes, i'm already doing this and it works great. The only two drawbacks
is that you end up with a lot of structs if you have lots of functions
with optional parameters, and of course the client API is not as simple
and straightforward to use.
--
Matthieu