clkao / plv8js-migrated

Automatically exported from code.google.com/p/plv8js
Other
0 stars 0 forks source link

plv8.find_function ignores default parameters #104

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Define a plv8 function that accepts a parameter with a default value.

create function plv8_test(username text DEFAULT 'bob') returns text as $$
  return username;
$$ LANGUAGE plv8;

2. Execute "plv8_test" function with no parameters using plv8.find_function.

create function caller(state integer) returns text as $$
  var payload;
  if (state) {
    payload = plv8.find_function('plv8_test')();
  } else {
    payload = plv8.execute('select * from plv8_test()');
  }
  return JSON.stringify(payload);
$$ LANGUAGE plv8;

select * from caller(0); -- returns [{"plv8_test":"bob"}]
select * from caller(1); -- returns NULL

What is the expected output? What do you see instead?

Expected [{"plv8_test":"bob"}] from both tests, but instead it returns null 
when the 
function is executed via plv8.find_function. 

What version of the product are you using? On what operating system?
plv8 1.4.2
psql (PostgreSQL) 9.3.4
Ubuntu Server 14.04.

Please provide any additional information below.
If you replace "return username" with "throw username" you can see that the 
parameter is undefined despite the DEFAULT option. I suppose this is correct 
behavior from a purely javascript context, but I believe that the behavior I 
expected is a bit more pragmatic.

To provide some additional detail, I'm actually using the coffeescript compiler 
and view triggers to insert parameters into my functions. I really wanted to 
take advantage of coffeescript's function parameter unpacking. e.g:

fn = plv8.find_function('my_func')
fn(an_array_of_parameters...)  

If I call the function via plv8.execute I'll have to manually expand the 
parameters into a string whenever I make the call OR handle the optional 
parameters the javascript way with a bunch of typeof x === 'undefined' checks 
which is my current work around.

Finally, thanks for this amazing piece of software.

Original issue reported on code.google.com by leo....@gmail.com on 20 Sep 2014 at 12:48