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

overpass_api/statements/set_prop.cc

228 of 380 instrumented lines run by the suite (60.0%) 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)
312324132 q{
322324132 q if ((tree_context == Statement::generic || tree_context == Statement::in_convert)
332324132 q && tree_it->token == "!" && tree_it->rhs)
349624 q {
359624 q std::map< std::string, std::string > attributes;
369624 q attributes["k"] = decode_json(tree_it.rhs()->token, error_output);
379624 q attributes["keytype"] = "tag";
389624 q return new Set_Prop_Statement(tree_it->line_col.first, attributes, global_settings);
399624 q }
412228132 q if (tree_context != Statement::generic && tree_context != Statement::in_convert)
4210820 q return 0;
442120127 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 }
522120127 q if (!tree_it->rhs)
53122 q {
54122 q if (error_output)
55122 q error_output->add_parse_error("To set a property it must have a value to the right of the equal sign",
56122 q tree_it->line_col.first);
57122 q return 0;
58122 q }
602108127 q Statement::Eval_Return_Type eval_type = Statement::string;
612108127 q std::map< std::string, std::string > attributes;
622108127 q if (tree_it.lhs()->token == "::")
6351686 q {
6451686 q if (tree_it.lhs()->rhs)
6542079 q {
6642079 q if (tree_it.lhs().rhs()->token == "id")
6720849 q attributes["keytype"] = "id";
6821250 q else if (tree_it.lhs().rhs()->token == "geom")
6921250 q {
7021250 q attributes["keytype"] = "geometry";
7121250 q eval_type = Statement::geometry;
7221250 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);
7642079 q }
779624 q else if (tree_it.lhs()->lhs)
780 {
790 attributes["keytype"] = "generic";
800 attributes["from"] = tree_it.lhs().lhs()->token;
810 }
829624 q else if (tree_context == Statement::in_convert)
839624 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);
8751686 q }
881592127 q else
891592127 q {
901592127 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;
981588127 q attributes["k"] = decode_json(tree_it.lhs()->token, error_output);
991588127 q attributes["keytype"] = "tag";
1001588127 q }
1012104127 q Statement* result = new Set_Prop_Statement(tree_it->line_col.first, attributes, global_settings);
1022104127 q if (result)
1032104127 q {
1042104127 q Statement* rhs = stmt_factory.create_evaluator(tree_it.rhs(),
1052104127 q tree_context == Statement::in_convert ? Statement::elem_eval_possible : Statement::evaluator_expected,
1062104127 q Statement::Single_Return_Type_Checker(eval_type));
1072104127 q if (rhs)
1082076127 q result->add_statement(rhs, "");
109287 q else if (error_output)
110287 q error_output->add_parse_error("Property assignment needs a right-hand-side value", tree_it->line_col.first);
1112104127 q }
1122104127 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)
1182200127 q : Statement(line_number_), key(0), mode(Set_Prop_Task::single_key), tag_value(0)
1202200127 q std::map< std::string, std::string > attributes;
1222200127 q attributes["k"] = "";
1232200127 q attributes["keytype"] = "tag";
1242200127 q attributes["from"] = "";
1262200127 q eval_attributes_array(get_name(), attributes, input_attributes);
1282200127 q if (attributes["keytype"] == "tag")
1291684127 q {
1301684127 q if (attributes["k"] != "")
1311684127 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.");
1351684127 q }
13651686 q else if (attributes["keytype"] == "id")
13720849 q mode = Set_Prop_Task::set_id;
13830861 q else if (attributes["keytype"] == "geometry")
13921250 q mode = Set_Prop_Task::set_geometry;
1409624 q else if (attributes["keytype"] == "generic")
1419624 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\".");
1462200127 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)
1592076127 q Evaluator* tag_value_ = dynamic_cast< Evaluator* >(statement);
1602076127 q if (tag_value_ && !tag_value)
1612076127 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
171550127 q if (!tag_value)
1723129 q return indent + "<set-prop keytype=\"tag\" k=\"" + (key ? *key : "") + "\"/>\n";
174519127 q std::string attributes;
175519127 q if (mode == Set_Prop_Task::set_id)
1765249 q attributes = " keytype=\"id\"";
177519127 q if (mode == Set_Prop_Task::set_geometry)
1785350 q attributes = " keytype=\"geometry\"";
179466127 q else if (!key)
1807660 q attributes = " keytype=\"generic\"";
181390127 q else
182390127 q attributes = std::string(" keytype=\"tag\" k=\"") + (key ? *key : "") + "\"";
184519127 q if (!input.empty())
1850 attributes += " from=\"" + input + "\"";
187519127 q return indent + "<set-prop" + attributes + ">\n"
188519127 q + tag_value->dump_xml(indent + " ")
189519127 q + indent + "</set-prop>\n";
193std::string Set_Prop_Statement::dump_compact_ql(const std::string&) const
1951100127 q std::string result;
1961100127 q if (mode == Set_Prop_Task::set_id)
19710449 q result = "::id=";
1981100127 q if (mode == Set_Prop_Task::set_geometry)
19910650 q result = "::geom=";
200994127 q else if (!key)
20115260 q result = input + "::=";
202842127 q else if (!tag_value)
2036229 q return std::string("!") + escape_cstr(key ? *key : "");
204780127 q else
205780127 q result = escape_cstr(key ? *key : "") + "=";
2071038127 q if (tag_value)
2081038127 q return result + tag_value->dump_compact_ql("");
2100 return result;
215Requested_Context Set_Prop_Statement::request_context() const
217766117 q if (tag_value)
219743117 q Requested_Context result = tag_value->request_context();
220743117 q if (input.empty())
221743117 q result.add_usage(Set_Usage::TAGS);
2220 else
2230 result.add_usage(input, Set_Usage::TAGS);
225743117 q return result;
2282322 q Requested_Context result;
2292322 q result.add_usage(Set_Usage::TAGS);
2302322 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)
288766117 q if (input.empty())
290766117 q if (mode == Set_Prop_Task::set_geometry)
2915046 q return new Set_Prop_Geometry_Task(tag_value ? tag_value->get_geometry_task(context) : 0);
293716117 q return new Set_Prop_Plain_Task(tag_value ? tag_value->get_string_task(context, key) : 0,
294716117 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
32339659 q if (!rhs)
32431 q return;
32639359 q if (mode == single_key)
32739359 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)
345443618768 q{
346443618768 q if (!rhs)
34763158422 q return;
349380460368 q if (mode == Set_Prop_Task::single_key)
350291165668 q result.tags.push_back(std::make_pair(key, rhs->eval(data, &key)));
35189294753 q else if (mode == Set_Prop_Task::generic)
35252431722 q {
35352431722 q if (data.tags)
35410756922 q {
35510756922 q std::vector< std::string > found_keys;
35610756922 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it_keys = data.tags->begin();
35750563422 q it_keys != data.tags->end(); ++it_keys)
35839806522 q found_keys.push_back(it_keys->first);
35910756922 q std::sort(found_keys.begin(), found_keys.end());
36010756922 q found_keys.erase(std::unique(found_keys.begin(), found_keys.end()), found_keys.end());
36110756922 q found_keys.erase(std::set_difference(found_keys.begin(), found_keys.end(),
36210756922 q declared_keys.begin(), declared_keys.end(), found_keys.begin()), found_keys.end());
36410756922 q for (std::vector< std::string >::const_iterator it_keys = found_keys.begin();
36550371022 q it_keys != found_keys.end(); ++it_keys)
36639614121 q result.tags.push_back(std::make_pair(*it_keys, rhs->eval(data, &*it_keys)));
36710756922 q }
36852431722 q }
36936863042 q else if (mode == Set_Prop_Task::set_id)
37036863042 q {
37136863042 q if (!id_set)
37236863042 q {
37336863042 q int64 id = 0;
37436863042 q id_set |= try_int64(rhs->eval(data, 0), id);
37536863042 q if (id_set)
37636863042 q result.id = Uint64(id);
37736863042 q }
37836863042 q }
379380460368 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
384285970320 q{
385285970320 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
386285970320 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
391235285 q{
392235285 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
393235285 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
39897932629 q{
39997932629 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
40097932629 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
405196347 q{
406196347 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
407196347 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
41250765021 q{
41350765021 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
41450765021 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
4191683 q{
4201683 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
4211683 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
4266374 q{
4276374 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
4286374 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
4334554110 q{
4344554110 q ::process(key, mode, rhs, data, declared_keys, result, id_set);
4354554110 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
5192221 q if (rhs)
5202019 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
526398616 q{
527398616 q if (rhs)
528398616 q result.acquire_geometry(rhs->eval(data));
529398616 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
5426853511 q{
5436853511 q if (rhs)
5446853511 q result.acquire_geometry(rhs->eval(data));
5456853511 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
5506951 q{
5516951 q if (rhs)
5526951 q result.acquire_geometry(rhs->eval(data));
5536951 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
558594358 q{
559594358 q if (rhs)
560594358 q result.acquire_geometry(rhs->eval(data));
561594358 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
5740{
5750 if (rhs)
5760 result.acquire_geometry(rhs->eval(data));
5770}
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
583265 q if (rhs)
584265 q result.acquire_geometry(rhs->eval(data));