1int ${event.CamelCase}::set_error(sd_bus_error* e) const
2{
3    if (jsonString.empty())
4    {
5        jsonString = to_json().dump();
6    }
7
8    return sd_bus_error_set(e, errName, jsonString.c_str());
9}
10
11int ${event.CamelCase}::set_error(SdBusInterface* i, sd_bus_error* e) const
12{
13    if (jsonString.empty())
14    {
15        jsonString = to_json().dump();
16    }
17
18    return i->sd_bus_error_set(e, errName, jsonString.c_str());
19}
20
21auto ${event.CamelCase}::to_json() const -> nlohmann::json
22{
23    nlohmann::json j = { };
24% for m in event.metadata:
25    % if m.typeName == "object_path":
26    j["${m.SNAKE_CASE}"] = ${m.camelCase}.str;
27    % elif m.is_enum():
28    j["${m.SNAKE_CASE}"] = sdbusplus::message::convert_to_string(${m.camelCase});
29    % else:
30    j["${m.SNAKE_CASE}"] = ${m.camelCase};
31    % endif
32% endfor
33
34    // Add common source and pid info.
35    nlohmann::json source_info = {};
36    source_info["FILE"] = source_file;
37    source_info["FUNCTION"] = source_func;
38    source_info["LINE"] = source_line;
39    source_info["COLUMN"] = source_column;
40    source_info["PID"] = pid;
41    j["_SOURCE"] = source_info;
42
43    return nlohmann::json{ { errName, std::move(j) } };
44}
45
46${event.CamelCase}::${event.CamelCase}(
47    const nlohmann::json& j, const std::source_location& s)
48{
49    const nlohmann::json& self = j.at(errName);
50
51% for m in event.metadata:
52    % if m.typeName == "object_path":
53    ${m.camelCase} = self.at("${m.SNAKE_CASE}").get<std::string>();
54    % elif m.is_enum():
55    if (auto enum_value =
56            sdbusplus::message::convert_from_string<decltype(${m.camelCase})>(
57                self.at("${m.SNAKE_CASE}"));
58        enum_value.has_value())
59    {
60        ${m.camelCase} = enum_value.value();
61    }
62    else
63    {
64        throw sdbusplus::exception::InvalidEnumString();
65    }
66    % else:
67    ${m.camelCase} = self.at("${m.SNAKE_CASE}");
68    % endif
69% endfor
70
71    if (!self.contains("_SOURCE"))
72    {
73        source_file = s.file_name();
74        source_func = s.function_name();
75        source_line = s.line();
76        source_column = s.column();
77        pid = getpid();
78    }
79    else
80    {
81        source_file = self.at("FILE");
82        source_func = self.at("FUNCTION");
83        source_line = self.at("LINE");
84        source_column = self.at("COLUMN");
85        pid = self.at("PID");
86    }
87
88}
89
90namespace details
91{
92void register_${event.CamelCase}()
93{
94    sdbusplus::sdbuspp::register_event_helper<${event.CamelCase}>::hook();
95}
96}
97