2019-04-27 21:50:53 +02:00
|
|
|
// jsonproc.cpp
|
|
|
|
|
|
|
|
#include "jsonproc.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include <string>
|
2019-07-02 05:47:45 +02:00
|
|
|
using std::string; using std::to_string;
|
2019-04-27 21:50:53 +02:00
|
|
|
|
2022-04-05 05:22:43 +02:00
|
|
|
#include <algorithm>
|
|
|
|
using std::replace_if;
|
|
|
|
|
2019-04-27 21:50:53 +02:00
|
|
|
#include <inja.hpp>
|
|
|
|
using namespace inja;
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
|
|
|
std::map<string, string> customVars;
|
|
|
|
|
|
|
|
void set_custom_var(string key, string value)
|
|
|
|
{
|
|
|
|
customVars[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
string get_custom_var(string key)
|
|
|
|
{
|
|
|
|
return customVars[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc != 4)
|
|
|
|
FATAL_ERROR("USAGE: jsonproc <json-filepath> <template-filepath> <output-filepath>\n");
|
|
|
|
|
|
|
|
string jsonfilepath = argv[1];
|
|
|
|
string templateFilepath = argv[2];
|
|
|
|
string outputFilepath = argv[3];
|
|
|
|
|
|
|
|
Environment env;
|
2022-04-05 05:22:43 +02:00
|
|
|
env.set_trim_blocks(true);
|
2019-04-27 21:50:53 +02:00
|
|
|
|
|
|
|
// Add custom command callbacks.
|
|
|
|
env.add_callback("doNotModifyHeader", 0, [jsonfilepath, templateFilepath](Arguments& args) {
|
2019-06-30 18:05:45 +02:00
|
|
|
return "//\n// DO NOT MODIFY THIS FILE! It is auto-generated from " + jsonfilepath +" and Inja template " + templateFilepath + "\n//\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
env.add_callback("subtract", 2, [](Arguments& args) {
|
|
|
|
int minuend = args.at(0)->get<int>();
|
|
|
|
int subtrahend = args.at(1)->get<int>();
|
|
|
|
|
|
|
|
return minuend - subtrahend;
|
2019-04-27 21:50:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
env.add_callback("setVar", 2, [=](Arguments& args) {
|
|
|
|
string key = args.at(0)->get<string>();
|
|
|
|
string value = args.at(1)->get<string>();
|
|
|
|
set_custom_var(key, value);
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
|
2019-07-02 05:47:45 +02:00
|
|
|
env.add_callback("setVarInt", 2, [=](Arguments& args) {
|
|
|
|
string key = args.at(0)->get<string>();
|
|
|
|
string value = to_string(args.at(1)->get<int>());
|
|
|
|
set_custom_var(key, value);
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
|
2019-04-27 21:50:53 +02:00
|
|
|
env.add_callback("getVar", 1, [=](Arguments& args) {
|
|
|
|
string key = args.at(0)->get<string>();
|
|
|
|
return get_custom_var(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
env.add_callback("concat", 2, [](Arguments& args) {
|
|
|
|
string first = args.at(0)->get<string>();
|
|
|
|
string second = args.at(1)->get<string>();
|
|
|
|
return first + second;
|
|
|
|
});
|
|
|
|
|
|
|
|
env.add_callback("removePrefix", 2, [](Arguments& args) {
|
|
|
|
string rawValue = args.at(0)->get<string>();
|
|
|
|
string prefix = args.at(1)->get<string>();
|
|
|
|
string::size_type i = rawValue.find(prefix);
|
|
|
|
if (i != 0)
|
|
|
|
return rawValue;
|
|
|
|
|
|
|
|
return rawValue.erase(0, prefix.length());
|
|
|
|
});
|
|
|
|
|
|
|
|
env.add_callback("removeSuffix", 2, [](Arguments& args) {
|
|
|
|
string rawValue = args.at(0)->get<string>();
|
|
|
|
string suffix = args.at(1)->get<string>();
|
|
|
|
string::size_type i = rawValue.rfind(suffix);
|
|
|
|
if (i == string::npos)
|
|
|
|
return rawValue;
|
|
|
|
|
|
|
|
return rawValue.substr(0, i);
|
|
|
|
});
|
|
|
|
|
2019-09-30 23:58:01 +02:00
|
|
|
// single argument is a json object
|
|
|
|
env.add_callback("isEmpty", 1, [](Arguments& args) {
|
|
|
|
return args.at(0)->empty();
|
|
|
|
});
|
|
|
|
|
2022-04-05 05:22:43 +02:00
|
|
|
env.add_callback("isEmptyString", 1, [](Arguments& args) {
|
|
|
|
return args.at(0)->get<string>().empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
env.add_callback("cleanString", 1, [](Arguments& args) {
|
2022-10-22 22:09:37 +02:00
|
|
|
string badChars = ".'{} \n\t-\u00e9";
|
2022-04-05 05:22:43 +02:00
|
|
|
string str = args.at(0)->get<string>();
|
2022-11-02 17:39:32 +01:00
|
|
|
for (unsigned int i = 0; i < str.length(); i++) {
|
2022-10-22 22:09:37 +02:00
|
|
|
if (badChars.find(str[i]) != std::string::npos) {
|
|
|
|
str[i] = '_';
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 05:22:43 +02:00
|
|
|
return str;
|
|
|
|
});
|
|
|
|
|
2019-04-27 21:50:53 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
env.write_with_json_file(templateFilepath, jsonfilepath, outputFilepath);
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
FATAL_ERROR("JSONPROC_ERROR: %s\n", e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|