Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/set_prop.cc

overpass_api/statements/set_prop.cc

232 of 380 instrumented lines run by the suite (61.1%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "../data/tag_store.h"
20#include "../data/utils.h"
21#include "set_prop.h"
24Set_Prop_Statement::Statement_Maker Set_Prop_Statement::statement_maker;
25Set_Prop_Statement::Evaluator_Maker Set_Prop_Statement::evaluator_maker;
28Statement* Set_Prop_Statement::Evaluator_Maker::create_evaluator(
29 const Token_Node_Ptr& tree_it, Statement::QL_Context tree_context,
30 Statement::Factory& stmt_factory, Parsed_Query& global_settings, Error_Output* error_output)
312028124 q{
322028124 q if ((tree_context == Statement::generic || tree_context == Statement::in_convert)
332028124 q && tree_it->token == "!" && tree_it->rhs)
346015 q {
356015 q std::map< std::string, std::string > attributes;
366015 q attributes["k"] = decode_json(tree_it.rhs()->token, error_output);
376015 q attributes["keytype"] = "tag";
386015 q return new Set_Prop_Statement(tree_it->line_col.first, attributes, global_settings);
396015 q }
411968124 q if (tree_context != Statement::generic && tree_context != Statement::in_convert)
428418 q return 0;
441884118 q if (!tree_it->lhs)
450 {
460 if (error_output)
470 error_output->add_parse_error("To set a property it must have a name to the left of the equal sign",
480 tree_it->line_col.first);
490 return 0;
500 }
521884118 q if (!tree_it->rhs)
53163 q {
54163 q if (error_output)
55163 q error_output->add_parse_error("To set a property it must have a value to the right of the equal sign",
56163 q tree_it->line_col.first);
57163 q return 0;
58163 q }
601868118 q Statement::Eval_Return_Type eval_type = Statement::string;
611868118 q std::map< std::string, std::string > attributes;
621868118 q if (tree_it.lhs()->token == "::")
6344872 q {
6444872 q if (tree_it.lhs()->rhs)
6537270 q {
6637270 q if (tree_it.lhs().rhs()->token == "id")
6719646 q attributes["keytype"] = "id";
6817641 q else if (tree_it.lhs().rhs()->token == "geom")
6917641 q {
7017641 q attributes["keytype"] = "geometry";
7117641 q eval_type = Statement::geometry;
7217641 q }
730 else if (error_output)
740 error_output->add_parse_error("The only allowed special properties are \"id\" and \"geometry\"",
750 tree_it->line_col.first);
7637270 q }
777619 q else if (tree_it.lhs()->lhs)
780 {
790 attributes["keytype"] = "generic";
800 attributes["from"] = tree_it.lhs().lhs()->token;
810 }
827619 q else if (tree_context == Statement::in_convert)
837619 q attributes["keytype"] = "generic";
840 else if (error_output)
850 error_output->add_parse_error("The generic tag set property can only be called from convert",
860 tree_it->line_col.first);
8744872 q }
881420118 q else
891420118 q {
901420118 q if (tree_it.lhs()->lhs || tree_it.lhs()->rhs)
9241 q if (error_output)
9341 q error_output->add_parse_error(std::string("if special character \"") + tree_it.lhs()->token
9441 q + "\" is present in property name then the property name must be in quotes",
9541 q tree_it->line_col.first);
9641 q return 0;
981416118 q attributes["k"] = decode_json(tree_it.lhs()->token, error_output);
991416118 q attributes["keytype"] = "tag";
1001416118 q }
1011864118 q Statement* result = new Set_Prop_Statement(tree_it->line_col.first, attributes, global_settings);
1021864118 q if (result)
1031864118 q {
1041864118 q Statement* rhs = stmt_factory.create_evaluator(tree_it.rhs(),
1051864118 q tree_context == Statement::in_convert ? Statement::elem_eval_possible : Statement::evaluator_expected,
1061864118 q Statement::Single_Return_Type_Checker(eval_type));
1071864118 q if (rhs)
1081840118 q result->add_statement(rhs, "");
109246 q else if (error_output)
110246 q error_output->add_parse_error("Property assignment needs a right-hand-side value", tree_it->line_col.first);
1111864118 q }
1121864118 q return result;
116Set_Prop_Statement::Set_Prop_Statement
117 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
1181924118 q : Statement(line_number_), key(0), mode(Set_Prop_Task::single_key), tag_value(0)
1201924118 q std::map< std::string, std::string > attributes;
1221924118 q attributes["k"] = "";
1231924118 q attributes["keytype"] = "tag";
1241924118 q attributes["from"] = "";
1261924118 q eval_attributes_array(get_name(), attributes, input_attributes);
1281924118 q if (attributes["keytype"] == "tag")
1291476118 q {
1301476118 q if (attributes["k"] != "")
1311476118 q key = new std::string(attributes["k"]);
1320 else
1330 add_static_error("For the statement \"set-prop\" in mode \"keytype\"=\"tag\", "
1340 "the attribute \"k\" must be nonempty.");
1351476118 q }
13644872 q else if (attributes["keytype"] == "id")
13719646 q mode = Set_Prop_Task::set_id;
13825250 q else if (attributes["keytype"] == "geometry")
13917641 q mode = Set_Prop_Task::set_geometry;
1407619 q else if (attributes["keytype"] == "generic")
1417619 q mode = Set_Prop_Task::generic;
1420 else
1430 add_static_error("For the attribute \"keytype\" of the element \"set-prop\""
1440 " the only allowed values are \"tag\", \"id\", \"geometry\", or \"generic\".");
1461924118 q if (!attributes["from"].empty())
1470 {
1480 if (attributes["keytype"] == "generic")
1490 input = attributes["from"];
1500 else
1510 add_static_error("The attribute \"from\" of the element \"set-prop\""
1520 " can only be set to a non-empty value if \"keytype\" is \"generic\".");
1530 }
157void Set_Prop_Statement::add_statement(Statement* statement, std::string text)
1591840118 q Evaluator* tag_value_ = dynamic_cast< Evaluator* >(statement);
1601840118 q if (tag_value_ && !tag_value)
1611840118 q tag_value = tag_value_;
1620 else if (tag_value)
1630 add_static_error("set-prop must have exactly one evaluator substatement.");
1640 else
1650 substatement_error(get_name(), statement);
169std::string Set_Prop_Statement::dump_xml(const std::string& indent) const
171481118 q if (!tag_value)
1722120 q return indent + "<set-prop keytype=\"tag\" k=\"" + (key ? *key : "") + "\"/>\n";
174460118 q std::string attributes;
175460118 q if (mode == Set_Prop_Task::set_id)
1764946 q attributes = " keytype=\"id\"";
177460118 q if (mode == Set_Prop_Task::set_geometry)
1784441 q attributes = " keytype=\"geometry\"";
179416118 q else if (!key)
1806850 q attributes = " keytype=\"generic\"";
181348118 q else
182348118 q attributes = std::string(" keytype=\"tag\" k=\"") + (key ? *key : "") + "\"";
184460118 q if (!input.empty())
1850 attributes += " from=\"" + input + "\"";
187460118 q return indent + "<set-prop" + attributes + ">\n"
188460118 q + tag_value->dump_xml(indent + " ")
189460118 q + indent + "</set-prop>\n";
193std::string Set_Prop_Statement::dump_compact_ql(const std::string&) const
195962118 q std::string result;
196962118 q if (mode == Set_Prop_Task::set_id)
1979846 q result = "::id=";
198962118 q if (mode == Set_Prop_Task::set_geometry)
1998841 q result = "::geom=";
200874118 q else if (!key)
20113650 q result = input + "::=";
202738118 q else if (!tag_value)
2034220 q return std::string("!") + escape_cstr(key ? *key : "");
204696118 q else
205696118 q result = escape_cstr(key ? *key : "") + "=";
207920118 q if (tag_value)
208920118 q return result + tag_value->dump_compact_ql("");
2100 return result;
215Requested_Context Set_Prop_Statement::request_context() const
217537109 q if (tag_value)
219523109 q Requested_Context result = tag_value->request_context();
220523109 q if (input.empty())
221523109 q result.add_usage(Set_Usage::TAGS);
2220 else
2230 result.add_usage(input, Set_Usage::TAGS);
225523109 q return result;
2281414 q Requested_Context result;
2291414 q result.add_usage(Set_Usage::TAGS);
2301414 q return result;
234template< typename Index, typename Maybe_Attic >
235void eval_elems(std::set< std::string >& existing_keys, Set_With_Context& input_set,
236 const std::map< Index, std::vector< Maybe_Attic > >& elems,
237 const std::vector< std::string >& otherwise_set_keys)
2380{
2390 for (typename std::map< Index, std::vector< Maybe_Attic > >::const_iterator idx_it = elems.begin();
2400 idx_it != elems.end(); ++idx_it)
2410 {
2420 for (typename std::vector< Maybe_Attic >::const_iterator elem_it = idx_it->second.begin();
2430 elem_it != idx_it->second.end(); ++elem_it)
2440 {
2450 Element_With_Context< Maybe_Attic > data = input_set.get_context(idx_it->first, *elem_it);
2460 if (data.tags)
2470 {
2480 for (std::vector< std::pair< std::string, std::string > >::const_iterator it_keys = data.tags->begin();
2490 it_keys != data.tags->end(); ++it_keys)
2500 {
2510 if (!std::binary_search(otherwise_set_keys.begin(), otherwise_set_keys.end(), it_keys->first))
2520 existing_keys.insert(it_keys->first);
2530 }
2540 }
2550 }
2560 }
2570}
260void eval_elems(std::set< std::string >& existing_keys, Set_With_Context& input_set,
261 const std::map< Uint31_Index, std::vector< Derived_Structure > >& elems,
262 const std::vector< std::string >& otherwise_set_keys)
2630{
2640 for (std::map< Uint31_Index, std::vector< Derived_Structure > >::const_iterator idx_it = elems.begin();
2650 idx_it != elems.end(); ++idx_it)
2660 {
2670 for (std::vector< Derived_Structure >::const_iterator elem_it = idx_it->second.begin();
2680 elem_it != idx_it->second.end(); ++elem_it)
2690 {
2700 Element_With_Context< Derived_Skeleton > data = input_set.get_context(idx_it->first, *elem_it);
2710 if (data.tags)
2720 {
2730 for (std::vector< std::pair< std::string, std::string > >::const_iterator it_keys = data.tags->begin();
2740 it_keys != data.tags->end(); ++it_keys)
2750 {
2760 if (!std::binary_search(otherwise_set_keys.begin(), otherwise_set_keys.end(), it_keys->first))
2770 existing_keys.insert(it_keys->first);
2780 }
2790 }
2800 }
2810 }
2820}
285Set_Prop_Task* Set_Prop_Statement::get_task(
286 Prepare_Task_Context& context, const std::vector< std::string >& otherwise_set_keys)
288537109 q if (input.empty())
290537109 q if (mode == Set_Prop_Task::set_geometry)
2914138 q return new Set_Prop_Geometry_Task(tag_value ? tag_value->get_geometry_task(context) : 0);
293496109 q return new Set_Prop_Plain_Task(tag_value ? tag_value->get_string_task(context, key) : 0,
294496109 q key ? *key : "", mode);
2970 Set_Prop_Generic_Task* result = new Set_Prop_Generic_Task();
2990 Set_With_Context* input_set = context.get_set(input);
3000 if (input_set && input_set->base)
3010 {
3020 std::set< std::string > existing_keys;
3040 eval_elems(existing_keys, *input_set, input_set->base->nodes, otherwise_set_keys);
3050 eval_elems(existing_keys, *input_set, input_set->base->attic_nodes, otherwise_set_keys);
3060 eval_elems(existing_keys, *input_set, input_set->base->ways, otherwise_set_keys);
3070 eval_elems(existing_keys, *input_set, input_set->base->attic_ways, otherwise_set_keys);
3080 eval_elems(existing_keys, *input_set, input_set->base->relations, otherwise_set_keys);
3090 eval_elems(existing_keys, *input_set, input_set->base->attic_relations, otherwise_set_keys);
3100 eval_elems(existing_keys, *input_set, input_set->base->areas, otherwise_set_keys);
3110 eval_elems(existing_keys, *input_set, input_set->base->deriveds, otherwise_set_keys);
3130 for (std::set< std::string >::const_iterator it = existing_keys.begin(); it != existing_keys.end(); ++it)
3140 result->add_key(*it, tag_value ? tag_value->get_string_task(context, &*it) : 0);
3150 }
3170 return result;
321void Set_Prop_Plain_Task::process(Derived_Structure& result, bool& id_set) const
32322056 q if (!rhs)
32411 q return;
32621956 q if (mode == single_key)
32721956 q result.tags.push_back(std::make_pair(key, rhs->eval(0)));
3280 else if (mode == set_id)
3290 {
3300 if (!id_set)
3310 {
3320 int64 id = 0;
3330 id_set |= try_int64(rhs->eval(0), id);
3340 if (id_set)
3350 result.id = Uint64(id);
3360 }
3370 }
341template< typename Object >
342void process(const std::string& key, Set_Prop_Task::Mode mode, Eval_Task* rhs,
343 const Element_With_Context< Object >& data, const std::vector< std::string >& declared_keys,
344 Derived_Structure& result, bool& id_set)
345232554057 q{
346232554057 q if (!rhs)
3477059614 q return;
349225494457 q if (mode == Set_Prop_Task::single_key)
350146863757 q result.tags.push_back(std::make_pair(key, rhs->eval(data, &key)));
35178630741 q else if (mode == Set_Prop_Task::generic)
35237200016 q {
35337200016 q if (data.tags)
3541716716 q {
3551716716 q std::vector< std::string > found_keys;
3561716716 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it_keys = data.tags->begin();
3577732716 q it_keys != data.tags->end(); ++it_keys)
3586016016 q found_keys.push_back(it_keys->first);
3591716716 q std::sort(found_keys.begin(), found_keys.end());
3601716716 q found_keys.erase(std::unique(found_keys.begin(), found_keys.end()), found_keys.end());
3611716716 q found_keys.erase(std::set_difference(found_keys.begin(), found_keys.end(),
3621716716 q declared_keys.begin(), declared_keys.end(), found_keys.begin()), found_keys.end());
3641716716 q for (std::vector< std::string >::const_iterator it_keys = found_keys.begin();
3657643416 q it_keys != found_keys.end(); ++it_keys)
3665926715 q result.tags.push_back(std::make_pair(*it_keys, rhs->eval(data, &*it_keys)));
3671716716 q }
36837200016 q }
36941430737 q else if (mode == Set_Prop_Task::set_id)
37041430737 q {
37141430737 q if (!id_set)
37241430737 q {
37341430737 q int64 id = 0;
37441430737 q id_set |= try_int64(rhs->eval(data, 0), id);
37541430737 q if (id_set)
37641430737 q result.id = Uint64(id);
37741430737 q }
37841430737 q }
379225494457 q}
382void Set_Prop_Plain_Task::process(const Element_With_Context< Node_Skeleton >& data,
383 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
384194500423 q{
385194500423 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
386194500423 q}
389void Set_Prop_Plain_Task::process(const Element_With_Context< Attic< Node_Skeleton > >& data,
390 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
39136526 q{
39236526 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
39336526 q}
396void Set_Prop_Plain_Task::process(const Element_With_Context< Way_Skeleton >& data,
397 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
39813038232 q{
39913038232 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
40013038232 q}
403void Set_Prop_Plain_Task::process(const Element_With_Context< Attic< Way_Skeleton > >& data,
404 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
40510765 q{
40610765 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
40710765 q}
410void Set_Prop_Plain_Task::process(const Element_With_Context< Relation_Skeleton >& data,
411 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
41213803221 q{
41313803221 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
41413803221 q}
417void Set_Prop_Plain_Task::process(const Element_With_Context< Attic< Relation_Skeleton > >& data,
418 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
41993943 q{
42093943 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
42193943 q}
424void Set_Prop_Plain_Task::process(const Element_With_Context< Area_Skeleton >& data,
425 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
4261694 q{
4271694 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
4281694 q}
431void Set_Prop_Plain_Task::process(const Element_With_Context< Derived_Skeleton >& data,
432 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
433978317 q{
434978317 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
435978317 q}
438void Set_Prop_Generic_Task::add_key(const std::string& key, Eval_Task* task)
4390{
4400 keys.push_back(key);
4410 rhs.push_back(task);
4420}
445void Set_Prop_Generic_Task::process(Derived_Structure& result, bool& id_set) const
4460{
4470 for (unsigned int i = 0; i < keys.size(); ++i)
4480 result.tags.push_back(std::make_pair(keys[i], rhs[i]->eval(&keys[i])));
4490}
452template< typename Object >
453void process_generic(const Owning_Array< Eval_Task* >& rhs, const std::vector< std::string >& keys,
454 const Element_With_Context< Object >& data, Derived_Structure& result)
4550{
4560 for (unsigned int i = 0; i < keys.size(); ++i)
4570 result.tags.push_back(std::make_pair(keys[i], rhs[i]->eval(data, &keys[i])));
4580}
461void Set_Prop_Generic_Task::process(const Element_With_Context< Node_Skeleton>& data,
462 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
4630{
4640 ::process_generic(rhs, keys, data, result);
4650}
468void Set_Prop_Generic_Task::process(const Element_With_Context< Attic< Node_Skeleton > >& data,
469 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
4700{
4710 ::process_generic(rhs, keys, data, result);
4720}
475void Set_Prop_Generic_Task::process(const Element_With_Context< Way_Skeleton >& data,
476 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
4770{
4780 ::process_generic(rhs, keys, data, result);
4790}
482void Set_Prop_Generic_Task::process(const Element_With_Context< Attic< Way_Skeleton > >& data,
483 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
4840{
4850 ::process_generic(rhs, keys, data, result);
4860}
489void Set_Prop_Generic_Task::process(const Element_With_Context< Relation_Skeleton >& data,
490 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
4910{
4920 ::process_generic(rhs, keys, data, result);
4930}
496void Set_Prop_Generic_Task::process(const Element_With_Context< Attic< Relation_Skeleton > >& data,
497 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
4980{
4990 ::process_generic(rhs, keys, data, result);
5000}
503void Set_Prop_Generic_Task::process(const Element_With_Context< Area_Skeleton >& data,
504 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
5050{
5060 ::process_generic(rhs, keys, data, result);
5070}
510void Set_Prop_Generic_Task::process(const Element_With_Context< Derived_Skeleton >& data,
511 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
5120{
5130 ::process_generic(rhs, keys, data, result);
5140}
517void Set_Prop_Geometry_Task::process(Derived_Structure& result, bool& id_set) const
5191918 q if (rhs)
5201817 q result.acquire_geometry(rhs->eval());
524void Set_Prop_Geometry_Task::process(const Element_With_Context< Node_Skeleton>& data,
525 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
526237486 q{
527237486 q if (rhs)
528237486 q result.acquire_geometry(rhs->eval(data));
529237486 q}
532void Set_Prop_Geometry_Task::process(const Element_With_Context< Attic< Node_Skeleton > >& data,
533 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
5340{
5350 if (rhs)
5360 result.acquire_geometry(rhs->eval(data));
5370}
540void Set_Prop_Geometry_Task::process(const Element_With_Context< Way_Skeleton >& data,
541 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
5422702112 q{
5432702112 q if (rhs)
5442702112 q result.acquire_geometry(rhs->eval(data));
5452702112 q}
548void Set_Prop_Geometry_Task::process(const Element_With_Context< Attic< Way_Skeleton > >& data,
549 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
5501021 q{
5511021 q if (rhs)
5521021 q result.acquire_geometry(rhs->eval(data));
5531021 q}
556void Set_Prop_Geometry_Task::process(const Element_With_Context< Relation_Skeleton >& data,
557 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
558229307 q{
559229307 q if (rhs)
560229307 q result.acquire_geometry(rhs->eval(data));
561229307 q}
564void Set_Prop_Geometry_Task::process(const Element_With_Context< Attic< Relation_Skeleton > >& data,
565 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
5660{
5670 if (rhs)
5680 result.acquire_geometry(rhs->eval(data));
5690}
572void Set_Prop_Geometry_Task::process(const Element_With_Context< Area_Skeleton >& data,
573 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
575271 q if (rhs)
576271 q result.acquire_geometry(rhs->eval(data));
580void Set_Prop_Geometry_Task::process(const Element_With_Context< Derived_Skeleton >& data,
581 const std::vector< std::string >& declared_keys, Derived_Structure& result, bool& id_set) const
58322 q if (rhs)
58422 q result.acquire_geometry(rhs->eval(data));