Skip to content

Mysterious function behavior in combination with a function result as first argument #158

@DeiRex75

Description

@DeiRex75

It seems to exist an issue with functions.
Two functions:

  • concat > a simple concatination of strings (2 args)
  • dump_function > dumps the argument (1 arg)

If dump_function is called as first argument, it gets the second argument for the concat.

Will take a look myself too.

nlohmann::json data;
auto array = nlohmann::json::array();
for(int i=0; i<2; i++){
    nlohmann::json data_key;
    data_key["a_key"] = std::to_string(i);
    array.push_back(data_key);
}
data["val"] = array;
data["val_null"] = nlohmann::json::array();

inja::Environment env;

env.add_callback("concat", 2, [](inja::Arguments& args) {

    std::stringstream result;

    for(ulong i=0;i<args.size();i++)
    {
         result << args.at(i)->get<std::string>();
    }
    return result.str();
});

env.add_callback("dump_function ", 1, [](inja::Arguments& args) {
    std::cout << "\tDUMP: " << (*args.at(0)).dump() << std::endl;
    std::stringstream result;
    result << "(";
    result << (*args.at(0)).dump();
    result << ")";
    return result.str();
});

std::cout << env.render("{{ concat(\"A\", dump_function (val)) }} ", data) << std::endl;
std::cout << env.render("{{ concat(dump_function (val), \"B\") }} ", data) << std::endl;
std::cout << env.render("{{ concat(\"A\", dump_function (val_null)) }} ", data) << std::endl;
std::cout << env.render("{{ concat(dump_function (val_null), \"B\") }} ", data) << std::endl;

V2.2.0

	DUMP: [{"a_key":"0"},{"a_key":"1"}]
A([{"a_key":"0"},{"a_key":"1"}]) 
	DUMP: [{"a_key":"0"},{"a_key":"1"}]
([{"a_key":"0"},{"a_key":"1"}])B 
	DUMP: []
A([]) 
	DUMP: []
([])B 

V3.0 (master)

		DUMP: [{"a_key":"0"},{"a_key":"1"}]
A([{"a_key":"0"},{"a_key":"1"}]) 
	DUMP: "B"
terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.302] type must be string, but is array

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions