Fawkes API  Fawkes Development Version
Backend.cpp
1 
2 /****************************************************************************
3  * Backend
4  * (auto-generated, do not modify directly)
5  *
6  * Fawkes Backend Info REST API.
7  * Provides backend meta information to the frontend.
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 "Backend.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 Backend::Backend(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 Backend::Backend(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 Backend::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 Backend::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 (name_) {
84  rapidjson::Value v_name;
85  v_name.SetString(*name_, allocator);
86  v.AddMember("name", v_name, allocator);
87  }
88  if (url_) {
89  rapidjson::Value v_url;
90  v_url.SetString(*url_, allocator);
91  v.AddMember("url", v_url, allocator);
92  }
93  rapidjson::Value v_services(rapidjson::kArrayType);
94  v_services.Reserve(services_.size(), allocator);
95  for (const auto &e : services_) {
96  rapidjson::Value v(rapidjson::kObjectType);
97  e->to_json_value(d, v);
98  v_services.PushBack(v, allocator);
99  }
100  v.AddMember("services", v_services, allocator);
101 }
102 
103 void
104 Backend::from_json(const std::string &json)
105 {
106  rapidjson::Document d;
107  d.Parse(json);
108 
109  from_json_value(d);
110 }
111 
112 void
113 Backend::from_json_value(const rapidjson::Value &d)
114 {
115  if (d.HasMember("kind") && d["kind"].IsString()) {
116  kind_ = d["kind"].GetString();
117  }
118  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
119  apiVersion_ = d["apiVersion"].GetString();
120  }
121  if (d.HasMember("id") && d["id"].IsString()) {
122  id_ = d["id"].GetString();
123  }
124  if (d.HasMember("name") && d["name"].IsString()) {
125  name_ = d["name"].GetString();
126  }
127  if (d.HasMember("url") && d["url"].IsString()) {
128  url_ = d["url"].GetString();
129  }
130  if (d.HasMember("services") && d["services"].IsArray()) {
131  const rapidjson::Value &a = d["services"];
132  services_ = std::vector<std::shared_ptr<Service>>{};
133  ;
134  services_.reserve(a.Size());
135  for (auto &v : a.GetArray()) {
136  std::shared_ptr<Service> nv{new Service()};
137  nv->from_json_value(v);
138  services_.push_back(std::move(nv));
139  }
140  }
141 }
142 
143 void
144 Backend::validate(bool subcall) const
145 {
146  std::vector<std::string> missing;
147  if (!kind_)
148  missing.push_back("kind");
149  if (!apiVersion_)
150  missing.push_back("apiVersion");
151  if (!id_)
152  missing.push_back("id");
153  if (!name_)
154  missing.push_back("name");
155  for (size_t i = 0; i < services_.size(); ++i) {
156  if (!services_[i]) {
157  missing.push_back("services[" + std::to_string(i) + "]");
158  } else {
159  try {
160  services_[i]->validate(true);
161  } catch (std::vector<std::string> &subcall_missing) {
162  for (const auto &s : subcall_missing) {
163  missing.push_back("services[" + std::to_string(i) + "]." + s);
164  }
165  }
166  }
167  }
168 
169  if (!missing.empty()) {
170  if (subcall) {
171  throw missing;
172  } else {
173  std::ostringstream s;
174  s << "Backend is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
175  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
176  s << missing[i];
177  if (i < (missing.size() - 1)) {
178  s << ", ";
179  }
180  }
181  throw std::runtime_error(s.str());
182  }
183  }
184 }
Backend::to_json
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: Backend.cpp:41
Backend::from_json_value
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: Backend.cpp:112
Backend::validate
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: Backend.cpp:143
Backend::~Backend
virtual ~Backend()
Destructor.
Definition: Backend.cpp:36
Backend::to_json_value
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: Backend.cpp:60
Service
Service representation for JSON transfer.
Definition: Service.h:25
Service::from_json_value
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: Service.cpp:89
Backend::Backend
Backend()
Constructor.
Definition: Backend.cpp:22
Backend::from_json
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: Backend.cpp:103