Skip to content

Commit d28c233

Browse files
committed
Add functions to explicitly set params by type and add some tests
1 parent 0e7f92a commit d28c233

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/params.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,48 @@ class params_t
3939
m_map.insert_or_assign(std::forward<K>(key), std::forward<V>(value));
4040
}
4141

42+
template <typename K>
43+
void set_null(K &&key)
44+
{
45+
m_map.insert_or_assign(std::forward<K>(key), null_param_t{});
46+
}
47+
48+
template <typename K>
49+
void set_string(K &&key, char const *value)
50+
{
51+
m_map.insert_or_assign(std::forward<K>(key), value);
52+
}
53+
54+
template <typename K>
55+
void set_string(K &&key, std::string const &value)
56+
{
57+
m_map.insert_or_assign(std::forward<K>(key), value);
58+
}
59+
60+
template <typename K>
61+
void set_string(K &&key, std::string_view value)
62+
{
63+
m_map.insert_or_assign(std::forward<K>(key), value);
64+
}
65+
66+
template <typename K>
67+
void set_int64(K &&key, int64_t value)
68+
{
69+
m_map.insert_or_assign(std::forward<K>(key), value);
70+
}
71+
72+
template <typename K>
73+
void set_double(K &&key, double value)
74+
{
75+
m_map.insert_or_assign(std::forward<K>(key), value);
76+
}
77+
78+
template <typename K>
79+
void set_bool(K &&key, bool value)
80+
{
81+
m_map.insert_or_assign(std::forward<K>(key), value);
82+
}
83+
4284
template <typename K>
4385
void remove(K &&key)
4486
{

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ set_test(test-output-pgsql-style-file)
8686
set_test(test-output-pgsql-tablespace LABELS Tablespace)
8787
set_test(test-output-pgsql-validgeom)
8888
set_test(test-output-pgsql-z_order)
89+
set_test(test-params LABELS NoDB)
8990
set_test(test-persistent-cache LABELS NoDB)
9091
set_test(test-pgsql)
9192
set_test(test-pgsql-capabilities)

tests/test-params.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* SPDX-License-Identifier: GPL-2.0-or-later
3+
*
4+
* This file is part of osm2pgsql (https://osm2pgsql.org/).
5+
*
6+
* Copyright (C) 2006-2024 by the osm2pgsql developer community.
7+
* For a full list of authors see the git log.
8+
*/
9+
10+
#include <catch.hpp>
11+
12+
#include "params.hpp"
13+
14+
TEST_CASE("Set param value", "[NoDB]")
15+
{
16+
param_value_t const p_null{null_param_t()};
17+
param_value_t const p_str{"foo"};
18+
param_value_t const p_int{26};
19+
param_value_t const p_double{3.141};
20+
param_value_t const p_true{true};
21+
param_value_t const p_false{false};
22+
23+
REQUIRE(to_string(p_null).empty());
24+
REQUIRE(to_string(p_str) == "foo");
25+
REQUIRE(to_string(p_int) == "26");
26+
REQUIRE(to_string(p_double) == "3.141");
27+
REQUIRE(to_string(p_true) == "true");
28+
REQUIRE(to_string(p_false) == "false");
29+
}
30+
31+
TEST_CASE("Params with different value types", "[NoDB]")
32+
{
33+
params_t params;
34+
REQUIRE_FALSE(params.has("foo"));
35+
36+
params.set("foo", 99);
37+
REQUIRE(params.has("foo"));
38+
REQUIRE(params.get("foo") == param_value_t(99));
39+
REQUIRE(params.get_int64("foo") == 99);
40+
41+
params.set("foo", "astring");
42+
REQUIRE(params.has("foo"));
43+
REQUIRE(params.get("foo") == param_value_t("astring"));
44+
REQUIRE(params.get_string("foo") == "astring");
45+
REQUIRE_THROWS(params.get_int64("foo"));
46+
}
47+
48+
TEST_CASE("Set params with explicit type", "[NoDB]")
49+
{
50+
params_t params;
51+
52+
params.set_null("isnull");
53+
params.set_string("isstring", "hi");
54+
params.set_int64("isint", 567);
55+
params.set_double("isdouble", 567);
56+
params.set_bool("istrue", true);
57+
params.set_bool("isfalse", false);
58+
59+
REQUIRE(params.get("isnull") == param_value_t());
60+
REQUIRE(params.get_string("isstring") == "hi");
61+
REQUIRE(params.get_int64("isint") == 567);
62+
REQUIRE(params.get_double("isdouble") == Approx(567.0));
63+
REQUIRE(params.get_bool("istrue"));
64+
REQUIRE_FALSE(params.get_bool("isfalse"));
65+
}

0 commit comments

Comments
 (0)