Fawkes API  Fawkes Development Version
Plugin.cpp
1 
2 /****************************************************************************
3  * Plugin
4  * (auto-generated, do not modify directly)
5  *
6  * Fawkes Plugin REST API.
7  * List, load, and unload plugins.
8  *
9  * API Contact: Tim Niemueller <niemueller@kbsg.rwth-aachen.de>
10  * API Version: v1beta1
11  * API License: Apache 2.0
12  ****************************************************************************/
13 
14 #include "Plugin.h"
15 
16 #include <rapidjson/document.h>
17 #include <rapidjson/prettywriter.h>
18 #include <rapidjson/stringbuffer.h>
19 #include <rapidjson/writer.h>
20 
21 #include <sstream>
22 
24 {
25 }
26 
27 Plugin::Plugin(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 Plugin::Plugin(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 Plugin::to_json(bool pretty) const
43 {
44  rapidjson::Document d;
45 
46  to_json_value(d, d);
47 
48  rapidjson::StringBuffer buffer;
49  if (pretty) {
50  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
51  d.Accept(writer);
52  } else {
53  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
54  d.Accept(writer);
55  }
56 
57  return buffer.GetString();
58 }
59 
60 void
61 Plugin::to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
62 {
63  rapidjson::Document::AllocatorType &allocator = d.GetAllocator();
64  v.SetObject();
65  // Avoid unused variable warnings
66  (void)allocator;
67 
68  if (kind_) {
69  rapidjson::Value v_kind;
70  v_kind.SetString(*kind_, allocator);
71  v.AddMember("kind", v_kind, allocator);
72  }
73  if (apiVersion_) {
74  rapidjson::Value v_apiVersion;
75  v_apiVersion.SetString(*apiVersion_, allocator);
76  v.AddMember("apiVersion", v_apiVersion, allocator);
77  }
78  if (name_) {
79  rapidjson::Value v_name;
80  v_name.SetString(*name_, allocator);
81  v.AddMember("name", v_name, allocator);
82  }
83  if (description_) {
84  rapidjson::Value v_description;
85  v_description.SetString(*description_, allocator);
86  v.AddMember("description", v_description, allocator);
87  }
88  if (is_meta_) {
89  rapidjson::Value v_is_meta;
90  v_is_meta.SetBool(*is_meta_);
91  v.AddMember("is_meta", v_is_meta, allocator);
92  }
93  rapidjson::Value v_meta_children(rapidjson::kArrayType);
94  v_meta_children.Reserve(meta_children_.size(), allocator);
95  for (const auto &e : meta_children_) {
96  rapidjson::Value v;
97  v.SetString(e, allocator);
98  v_meta_children.PushBack(v, allocator);
99  }
100  v.AddMember("meta_children", v_meta_children, allocator);
101  if (is_loaded_) {
102  rapidjson::Value v_is_loaded;
103  v_is_loaded.SetBool(*is_loaded_);
104  v.AddMember("is_loaded", v_is_loaded, allocator);
105  }
106 }
107 
108 void
109 Plugin::from_json(const std::string &json)
110 {
111  rapidjson::Document d;
112  d.Parse(json);
113 
114  from_json_value(d);
115 }
116 
117 void
118 Plugin::from_json_value(const rapidjson::Value &d)
119 {
120  if (d.HasMember("kind") && d["kind"].IsString()) {
121  kind_ = d["kind"].GetString();
122  }
123  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
124  apiVersion_ = d["apiVersion"].GetString();
125  }
126  if (d.HasMember("name") && d["name"].IsString()) {
127  name_ = d["name"].GetString();
128  }
129  if (d.HasMember("description") && d["description"].IsString()) {
130  description_ = d["description"].GetString();
131  }
132  if (d.HasMember("is_meta") && d["is_meta"].IsBool()) {
133  is_meta_ = d["is_meta"].GetBool();
134  }
135  if (d.HasMember("meta_children") && d["meta_children"].IsArray()) {
136  const rapidjson::Value &a = d["meta_children"];
137  meta_children_ = std::vector<std::string>{};
138  ;
139  meta_children_.reserve(a.Size());
140  for (auto &v : a.GetArray()) {
141  meta_children_.push_back(v.GetString());
142  }
143  }
144  if (d.HasMember("is_loaded") && d["is_loaded"].IsBool()) {
145  is_loaded_ = d["is_loaded"].GetBool();
146  }
147 }
148 
149 void
150 Plugin::validate(bool subcall) const
151 {
152  std::vector<std::string> missing;
153  if (!kind_)
154  missing.push_back("kind");
155  if (!apiVersion_)
156  missing.push_back("apiVersion");
157  if (!name_)
158  missing.push_back("name");
159  if (!description_)
160  missing.push_back("description");
161  if (!is_meta_)
162  missing.push_back("is_meta");
163  if (!is_loaded_)
164  missing.push_back("is_loaded");
165 
166  if (!missing.empty()) {
167  if (subcall) {
168  throw missing;
169  } else {
170  std::ostringstream s;
171  s << "Plugin is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
172  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
173  s << missing[i];
174  if (i < (missing.size() - 1)) {
175  s << ", ";
176  }
177  }
178  throw std::runtime_error(s.str());
179  }
180  }
181 }
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: Plugin.cpp:118
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: Plugin.cpp:109
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: Plugin.cpp:150
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: Plugin.cpp:61
Plugin()
Constructor.
Definition: Plugin.cpp:23
virtual ~Plugin()
Destructor.
Definition: Plugin.cpp:37
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: Plugin.cpp:42