table of tables

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

table of tables

Gary Madarm
I'm trying to understand how the table of tables works in the params example given here:
https://github.com/pintsized/lua-resty-http#request_pipeline

Why doesn't the value for path get overwritten with each new instance? How can I dynamically create a table of param tables to use with this method, e.g. I want to read the contents of an external file containing the following and create the table for the request_pipeline method:

/pathTo/request1.php
/pathTo/request2.php
/pathTo/request3.php
/pathTo/request4.php

thanks,
- G
Reply | Threaded
Open this post in threaded view
|

Re: table of tables

Choonster TheMage
On 22 January 2015 at 07:29, Gordon Madarm <[hidden email]> wrote:
I'm trying to understand how the table of tables works in the params example given here:
https://github.com/pintsized/lua-resty-http#request_pipeline

Why doesn't the value for path get overwritten with each new instance? How can I dynamically create a table of param tables to use with this method, e.g. I want to read the contents of an external file containing the following and create the table for the request_pipeline method:

/pathTo/request1.php
/pathTo/request2.php
/pathTo/request3.php
/pathTo/request4.php

thanks,
- G

Why would path be overwritten? You have one outer table being passed to request_pipeline. This table contains three values at keys 1, 2 and 3; each of which is a unique table with a string at key "path". These inner tables have no effect on each other.

This is an example of how you'd create the table for request_pipeline:

-- Assumes that file_with_paths.txt contains one path on each line

local request = {} -- The request table to pass to httpc:request_pipeline
local currentKey = 0 -- The most recent key an inner params table was stored at

for line in io.lines("file_with_paths.txt") do
currentKey = currentKey + 1 -- Increment currentKey
request[currentKey] = { path = line } -- Store a params table at currentKey
end

httpc:request_pipeline(request) -- Make the request


Regards,
Choonster
Reply | Threaded
Open this post in threaded view
|

Re: table of tables

Gary Madarm
On Thu, Jan 22, 2015 at 4:32 AM, Choonster TheMage <[hidden email]> wrote:
On 22 January 2015 at 07:29, Gordon Madarm <[hidden email]> wrote:

This is an example of how you'd create the table for request_pipeline:

-- Assumes that file_with_paths.txt contains one path on each line

local request = {} -- The request table to pass to httpc:request_pipeline
local currentKey = 0 -- The most recent key an inner params table was stored at

for line in io.lines("file_with_paths.txt") do
currentKey = currentKey + 1 -- Increment currentKey
request[currentKey] = { path = line } -- Store a params table at currentKey
end

httpc:request_pipeline(request) -- Make the request

Thanks! One follow up question, the documentation uses the httpc:request_pipeline{} syntax, while you use  httpc:request_pipeline(). What is the difference between curly brackets and circle brackets here?
 
-G

Reply | Threaded
Open this post in threaded view
|

Re: table of tables

Choonster TheMage
On 25 January 2015 at 08:07, Gordon Madarm <[hidden email]> wrote:

Thanks! One follow up question, the documentation uses the httpc:request_pipeline{} syntax, while you use  httpc:request_pipeline(). What is the difference between curly brackets and circle brackets here?
 
-G


The curly braces are just a shortcut for calling a function with a new table as its only argument (though functions called with the colon syntax still receive the object before the colon as their first argument and the table as their second argument). You can also do this with string literals. This is explained in more detail in the manual[1].

This:
httpc:request_pipeline{ ... }

Is equivalent to this:
httpc:request_pipeline({ ... })

And this:
local request = { ... }
httpc:request_pipeline(request)


Reply | Threaded
Open this post in threaded view
|

Re: table of tables

Coda Highland
On Sat, Jan 24, 2015 at 7:02 PM, Choonster TheMage
<[hidden email]> wrote:

> On 25 January 2015 at 08:07, Gordon Madarm <[hidden email]> wrote:
>>
>>
>> Thanks! One follow up question, the documentation uses the
>> httpc:request_pipeline{} syntax, while you use  httpc:request_pipeline().
>> What is the difference between curly brackets and circle brackets here?
>>
>> -G
>>
>
> The curly braces are just a shortcut for calling a function with a new table
> as its only argument (though functions called with the colon syntax still
> receive the object before the colon as their first argument and the table as
> their second argument). You can also do this with string literals. This is
> explained in more detail in the manual[1].
>
> This:
> httpc:request_pipeline{ ... }
>
> Is equivalent to this:
> httpc:request_pipeline({ ... })
>
> And this:
> local request = { ... }
> httpc:request_pipeline(request)
>
>
> [1] http://www.lua.org/manual/5.3/manual.html#3.4.10

Nitpick, the last one isn't completely equivalent because the table
gets held on the stack instead of discarded as a temporary. :P

/s/ Adam