Skip to content

Commit 0db7c08

Browse files
committed
Refactor table class init code and make it accessible from Lua
1 parent 7d958ee commit 0db7c08

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

src/output-flex.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ TRAMPOLINE(table_add_row, add_row)
7979
TRAMPOLINE(table_columns, columns)
8080
TRAMPOLINE(table_tostring, __tostring)
8181

82-
static char const *const osm2pgsql_table_name = "osm2pgsql.table";
82+
static char const *const osm2pgsql_table_name = "osm2pgsql.Table";
8383
static char const *const osm2pgsql_object_metatable =
8484
"osm2pgsql.object_metatable";
8585

@@ -1711,6 +1711,31 @@ output_flex_t::output_flex_t(
17111711
}
17121712
}
17131713

1714+
/**
1715+
* Define the osm2pgsql.Table class/metatable.
1716+
*/
1717+
static void init_table_class(lua_State *lua_state)
1718+
{
1719+
lua_getglobal(lua_state, "osm2pgsql");
1720+
if (luaL_newmetatable(lua_state, osm2pgsql_table_name) != 1) {
1721+
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
1722+
}
1723+
lua_pushvalue(lua_state, -1); // Copy of new metatable
1724+
1725+
// Add metatable as osm2pgsql.Table so we can access it from Lua
1726+
lua_setfield(lua_state, -3, "Table");
1727+
1728+
// Now add functions to metatable
1729+
lua_pushvalue(lua_state, -1);
1730+
lua_setfield(lua_state, -2, "__index");
1731+
luaX_add_table_func(lua_state, "__tostring", lua_trampoline_table_tostring);
1732+
luaX_add_table_func(lua_state, "add_row", lua_trampoline_table_add_row);
1733+
luaX_add_table_func(lua_state, "name", lua_trampoline_table_name);
1734+
luaX_add_table_func(lua_state, "schema", lua_trampoline_table_schema);
1735+
luaX_add_table_func(lua_state, "cluster", lua_trampoline_table_cluster);
1736+
luaX_add_table_func(lua_state, "columns", lua_trampoline_table_columns);
1737+
}
1738+
17141739
void output_flex_t::init_lua(std::string const &filename)
17151740
{
17161741
m_lua_state.reset(luaL_newstate(),
@@ -1739,19 +1764,7 @@ void output_flex_t::init_lua(std::string const &filename)
17391764

17401765
lua_setglobal(lua_state(), "osm2pgsql");
17411766

1742-
// Define "osmpgsql.table" metatable
1743-
if (luaL_newmetatable(lua_state(), osm2pgsql_table_name) != 1) {
1744-
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
1745-
}
1746-
lua_pushvalue(lua_state(), -1);
1747-
lua_setfield(lua_state(), -2, "__index");
1748-
luaX_add_table_func(lua_state(), "__tostring",
1749-
lua_trampoline_table_tostring);
1750-
luaX_add_table_func(lua_state(), "add_row", lua_trampoline_table_add_row);
1751-
luaX_add_table_func(lua_state(), "name", lua_trampoline_table_name);
1752-
luaX_add_table_func(lua_state(), "schema", lua_trampoline_table_schema);
1753-
luaX_add_table_func(lua_state(), "cluster", lua_trampoline_table_cluster);
1754-
luaX_add_table_func(lua_state(), "columns", lua_trampoline_table_columns);
1767+
init_table_class(lua_state());
17551768

17561769
// Clean up stack
17571770
lua_settop(lua_state(), 0);

tests/bdd/flex/lua-basics.feature

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Feature: Flex output uses a Lua config file
2+
3+
Scenario: Check access to osm2pgsql object from Lua
4+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
5+
And the lua style
6+
"""
7+
print("version=" .. osm2pgsql.version)
8+
print("mode=" .. osm2pgsql.mode)
9+
print("stage=" .. osm2pgsql.stage)
10+
print("Table=" .. type(osm2pgsql.Table))
11+
"""
12+
Then running osm2pgsql flex fails
13+
And the error output contains
14+
"""
15+
No tables defined in Lua config. Nothing to do!
16+
"""
17+
And the standard output contains
18+
"""
19+
mode=create
20+
"""
21+
And the standard output contains
22+
"""
23+
stage=1
24+
"""
25+
And the standard output contains
26+
"""
27+
Table=table
28+
"""

0 commit comments

Comments
 (0)