You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Matthieu Imbert <ma...@ens-lyon.fr> on 2009/07/01 07:46:21 UTC
optional fields in function declaration
structs can have optional fields, but it's not currently possible to put
an optional parameter in a thrift function prototype.
if i understand correctly, internally function parameters passing is
made through structs, so wouldn't it be easy and usefull to add support
to optional function parameters?
--
Matthieu
RE: optional fields in function declaration
Posted by Mark Slee <ms...@facebook.com>.
Yeah, but things get pretty messy when you have three parameters (or any situation of multiple optional params with the same type).
void function foo(int a, int b, int c)
void function foo(int a, int b)
void function foo(int a, int c) <= NO THANKS! Already declared a function foo with 2-int params.
Also gets messy if you decide to add/remove/reorder the params to your function. Now you've got to go update all the calls to helpers in your application code. Easier to just be explicit here.
-----Original Message-----
From: Jonathan Marcus [mailto:jamspam@jobstick.com]
Sent: Wednesday, July 01, 2009 1:42 PM
To: thrift-user@incubator.apache.org
Subject: Re: optional fields in function declaration
Nonetheless, it should be fairly easy to implement optional parameters
across languages, using method overloading in those languages that don't
directly support optional parameters.
Thrift:
void function foo(1:i32 a, 2:i32 b = -1)
C#/Java:
public void foo(int a)
{
foo(a, -1);
}
public void foo(int a, int b)
PHP:
public function foo($a, $b = -1)
You are correct about it being impossible to disambiguate a
default-value from one not set, but aside from that this should work in
every language.
--Jonathan
Mark Slee wrote:
> Yeah there's really no way to do this in most languages that don't support variable args. For instance, consider C++ and Java
>
> void myMethod(int x)
>
> There's just no way to make x optional. An integer variable x simply MUST be passed into this function. You can provide a default value if you like and not set it yourself, but there's no getting around the argument being present.
>
> If your application needs to disambiguate between a default-value and absence of an argument, then you must use a struct and inspect the isset field directly.
>
> -----Original Message-----
> From: Matthieu Imbert [mailto:matthieu.imbert@ens-lyon.fr]
> Sent: Wednesday, July 01, 2009 11:14 AM
> To: thrift-user@incubator.apache.org
> 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.
>
Re: optional fields in function declaration
Posted by Jonathan Marcus <ja...@jobstick.com>.
Nonetheless, it should be fairly easy to implement optional parameters
across languages, using method overloading in those languages that don't
directly support optional parameters.
Thrift:
void function foo(1:i32 a, 2:i32 b = -1)
C#/Java:
public void foo(int a)
{
foo(a, -1);
}
public void foo(int a, int b)
PHP:
public function foo($a, $b = -1)
You are correct about it being impossible to disambiguate a
default-value from one not set, but aside from that this should work in
every language.
--Jonathan
Mark Slee wrote:
> Yeah there's really no way to do this in most languages that don't support variable args. For instance, consider C++ and Java
>
> void myMethod(int x)
>
> There's just no way to make x optional. An integer variable x simply MUST be passed into this function. You can provide a default value if you like and not set it yourself, but there's no getting around the argument being present.
>
> If your application needs to disambiguate between a default-value and absence of an argument, then you must use a struct and inspect the isset field directly.
>
> -----Original Message-----
> From: Matthieu Imbert [mailto:matthieu.imbert@ens-lyon.fr]
> Sent: Wednesday, July 01, 2009 11:14 AM
> To: thrift-user@incubator.apache.org
> 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.
>
RE: optional fields in function declaration
Posted by Mark Slee <ms...@facebook.com>.
Yeah there's really no way to do this in most languages that don't support variable args. For instance, consider C++ and Java
void myMethod(int x)
There's just no way to make x optional. An integer variable x simply MUST be passed into this function. You can provide a default value if you like and not set it yourself, but there's no getting around the argument being present.
If your application needs to disambiguate between a default-value and absence of an argument, then you must use a struct and inspect the isset field directly.
-----Original Message-----
From: Matthieu Imbert [mailto:matthieu.imbert@ens-lyon.fr]
Sent: Wednesday, July 01, 2009 11:14 AM
To: thrift-user@incubator.apache.org
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
Re: optional fields in function declaration
Posted by Michael Andrews <ma...@liveops.com>.
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 <ma...@liveops.com>
Reply-To: <th...@incubator.apache.org>
Date: Wed, 1 Jul 2009 15:58:47 -0700
To: <th...@incubator.apache.org>, Matthieu Imbert
<ma...@ens-lyon.fr>
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 <ma...@ens-lyon.fr>
Reply-To: <th...@incubator.apache.org>
Date: Wed, 1 Jul 2009 11:14:29 -0700
To: <th...@incubator.apache.org>
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
Re: optional fields in function declaration
Posted by Michael Andrews <ma...@liveops.com>.
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 <ma...@ens-lyon.fr>
Reply-To: <th...@incubator.apache.org>
Date: Wed, 1 Jul 2009 11:14:29 -0700
To: <th...@incubator.apache.org>
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
Re: optional fields in function declaration
Posted by Matthieu Imbert <ma...@ens-lyon.fr>.
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
Re: optional fields in function declaration
Posted by Bryan Duxbury <br...@rapleaf.com>.
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?
-Bryan
On Jun 30, 2009, at 10:46 PM, Matthieu Imbert wrote:
> structs can have optional fields, but it's not currently possible
> to put
> an optional parameter in a thrift function prototype.
>
> if i understand correctly, internally function parameters passing is
> made through structs, so wouldn't it be easy and usefull to add
> support
> to optional function parameters?
>
> --
> Matthieu