Fawkes API  Fawkes Development Version
InterfaceInfo.cpp
1 
2 /****************************************************************************
3  * InterfaceInfo
4  * (auto-generated, do not modify directly)
5  *
6  * Fawkes Blackboard REST API.
7  * Access blackboard data through a REST API.
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 "InterfaceInfo.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 InterfaceInfo::InterfaceInfo(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 InterfaceInfo::InterfaceInfo(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 InterfaceInfo::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 InterfaceInfo::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 (id_) {
79  rapidjson::Value v_id;
80  v_id.SetString(*id_, allocator);
81  v.AddMember("id", v_id, allocator);
82  }
83  if (type_) {
84  rapidjson::Value v_type;
85  v_type.SetString(*type_, allocator);
86  v.AddMember("type", v_type, allocator);
87  }
88  if (hash_) {
89  rapidjson::Value v_hash;
90  v_hash.SetString(*hash_, allocator);
91  v.AddMember("hash", v_hash, allocator);
92  }
93  if (writer_) {
94  rapidjson::Value v_writer;
95  v_writer.SetString(*writer_, allocator);
96  v.AddMember("writer", v_writer, allocator);
97  }
98  rapidjson::Value v_readers(rapidjson::kArrayType);
99  v_readers.Reserve(readers_.size(), allocator);
100  for (const auto &e : readers_) {
101  rapidjson::Value v;
102  v.SetString(e, allocator);
103  v_readers.PushBack(v, allocator);
104  }
105  v.AddMember("readers", v_readers, allocator);
106  rapidjson::Value v_fields(rapidjson::kArrayType);
107  v_fields.Reserve(fields_.size(), allocator);
108  for (const auto &e : fields_) {
109  rapidjson::Value v(rapidjson::kObjectType);
110  e->to_json_value(d, v);
111  v_fields.PushBack(v, allocator);
112  }
113  v.AddMember("fields", v_fields, allocator);
114  rapidjson::Value v_message_types(rapidjson::kArrayType);
115  v_message_types.Reserve(message_types_.size(), allocator);
116  for (const auto &e : message_types_) {
117  rapidjson::Value v(rapidjson::kObjectType);
118  e->to_json_value(d, v);
119  v_message_types.PushBack(v, allocator);
120  }
121  v.AddMember("message_types", v_message_types, allocator);
122 }
123 
124 void
125 InterfaceInfo::from_json(const std::string &json)
126 {
127  rapidjson::Document d;
128  d.Parse(json);
129 
130  from_json_value(d);
131 }
132 
133 void
134 InterfaceInfo::from_json_value(const rapidjson::Value &d)
135 {
136  if (d.HasMember("kind") && d["kind"].IsString()) {
137  kind_ = d["kind"].GetString();
138  }
139  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
140  apiVersion_ = d["apiVersion"].GetString();
141  }
142  if (d.HasMember("id") && d["id"].IsString()) {
143  id_ = d["id"].GetString();
144  }
145  if (d.HasMember("type") && d["type"].IsString()) {
146  type_ = d["type"].GetString();
147  }
148  if (d.HasMember("hash") && d["hash"].IsString()) {
149  hash_ = d["hash"].GetString();
150  }
151  if (d.HasMember("writer") && d["writer"].IsString()) {
152  writer_ = d["writer"].GetString();
153  }
154  if (d.HasMember("readers") && d["readers"].IsArray()) {
155  const rapidjson::Value &a = d["readers"];
156  readers_ = std::vector<std::string>{};
157  ;
158  readers_.reserve(a.Size());
159  for (auto &v : a.GetArray()) {
160  readers_.push_back(v.GetString());
161  }
162  }
163  if (d.HasMember("fields") && d["fields"].IsArray()) {
164  const rapidjson::Value &a = d["fields"];
165  fields_ = std::vector<std::shared_ptr<InterfaceFieldType>>{};
166  ;
167  fields_.reserve(a.Size());
168  for (auto &v : a.GetArray()) {
169  std::shared_ptr<InterfaceFieldType> nv{new InterfaceFieldType()};
170  nv->from_json_value(v);
171  fields_.push_back(std::move(nv));
172  }
173  }
174  if (d.HasMember("message_types") && d["message_types"].IsArray()) {
175  const rapidjson::Value &a = d["message_types"];
176  message_types_ = std::vector<std::shared_ptr<InterfaceMessageType>>{};
177  ;
178  message_types_.reserve(a.Size());
179  for (auto &v : a.GetArray()) {
180  std::shared_ptr<InterfaceMessageType> nv{new InterfaceMessageType()};
181  nv->from_json_value(v);
182  message_types_.push_back(std::move(nv));
183  }
184  }
185 }
186 
187 void
188 InterfaceInfo::validate(bool subcall) const
189 {
190  std::vector<std::string> missing;
191  if (!kind_)
192  missing.push_back("kind");
193  if (!apiVersion_)
194  missing.push_back("apiVersion");
195  if (!id_)
196  missing.push_back("id");
197  if (!type_)
198  missing.push_back("type");
199  if (!hash_)
200  missing.push_back("hash");
201  for (size_t i = 0; i < fields_.size(); ++i) {
202  if (!fields_[i]) {
203  missing.push_back("fields[" + std::to_string(i) + "]");
204  } else {
205  try {
206  fields_[i]->validate(true);
207  } catch (std::vector<std::string> &subcall_missing) {
208  for (const auto &s : subcall_missing) {
209  missing.push_back("fields[" + std::to_string(i) + "]." + s);
210  }
211  }
212  }
213  }
214  for (size_t i = 0; i < message_types_.size(); ++i) {
215  if (!message_types_[i]) {
216  missing.push_back("message_types[" + std::to_string(i) + "]");
217  } else {
218  try {
219  message_types_[i]->validate(true);
220  } catch (std::vector<std::string> &subcall_missing) {
221  for (const auto &s : subcall_missing) {
222  missing.push_back("message_types[" + std::to_string(i) + "]." + s);
223  }
224  }
225  }
226  }
227 
228  if (!missing.empty()) {
229  if (subcall) {
230  throw missing;
231  } else {
232  std::ostringstream s;
233  s << "InterfaceInfo is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
234  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
235  s << missing[i];
236  if (i < (missing.size() - 1)) {
237  s << ", ";
238  }
239  }
240  throw std::runtime_error(s.str());
241  }
242  }
243 }
virtual ~InterfaceInfo()
Destructor.
InterfaceInfo()
Constructor.
InterfaceMessageType representation for JSON transfer.
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
std::optional< std::string > writer() const
Get writer value.
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
InterfaceFieldType representation for JSON transfer.