Fawkes API  Fawkes Development Version
ConfigTree.cpp
1 
2 /****************************************************************************
3  * ConfigTree
4  * (auto-generated, do not modify directly)
5  *
6  * Fawkes Configuration REST API.
7  * Retrieve information from the configuration.
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 "ConfigTree.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 ConfigTree::ConfigTree(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 ConfigTree::ConfigTree(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
37 std::string
38 ConfigTree::to_json(bool pretty) const
39 {
40  rapidjson::Document d;
41 
42  to_json_value(d, d);
43 
44  rapidjson::StringBuffer buffer;
45  if (pretty) {
46  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
47  d.Accept(writer);
48  } else {
49  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
50  d.Accept(writer);
51  }
52 
53  return buffer.GetString();
54 }
55 
56 void
57 ConfigTree::to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
58 {
59  rapidjson::Document::AllocatorType &allocator = d.GetAllocator();
60  v.SetObject();
61  // Avoid unused variable warnings
62  (void)allocator;
63 
64  if (kind_) {
65  rapidjson::Value v_kind;
66  v_kind.SetString(*kind_, allocator);
67  v.AddMember("kind", v_kind, allocator);
68  }
69  if (apiVersion_) {
70  rapidjson::Value v_apiVersion;
71  v_apiVersion.SetString(*apiVersion_, allocator);
72  v.AddMember("apiVersion", v_apiVersion, allocator);
73  }
74  if (config_) {
75  v.AddMember("config", *config_, allocator);
76  }
77 }
78 
79 void
80 ConfigTree::from_json(const std::string &json)
81 {
82  rapidjson::Document d;
83  d.Parse(json);
84 
85  from_json_value(d);
86 }
87 
88 void
89 ConfigTree::from_json_value(const rapidjson::Value &d)
90 {
91  if (d.HasMember("kind") && d["kind"].IsString()) {
92  kind_ = d["kind"].GetString();
93  }
94  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
95  apiVersion_ = d["apiVersion"].GetString();
96  }
97  if (d.HasMember("config") && d["config"].IsObject()) {
98  std::shared_ptr<rapidjson::Document> d_config = std::make_shared<rapidjson::Document>();
99  d_config->CopyFrom(d["config"], d_config->GetAllocator());
100  }
101 }
102 
103 void
104 ConfigTree::validate(bool subcall) const
105 {
106  std::vector<std::string> missing;
107  if (!kind_)
108  missing.push_back("kind");
109  if (!apiVersion_)
110  missing.push_back("apiVersion");
111  if (!config_)
112  missing.push_back("config");
113 
114  if (!missing.empty()) {
115  if (subcall) {
116  throw missing;
117  } else {
118  std::ostringstream s;
119  s << "ConfigTree is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
120  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
121  s << missing[i];
122  if (i < (missing.size() - 1)) {
123  s << ", ";
124  }
125  }
126  throw std::runtime_error(s.str());
127  }
128  }
129 }
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: ConfigTree.cpp:89
ConfigTree()
Constructor.
Definition: ConfigTree.cpp:23
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: ConfigTree.cpp:104
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: ConfigTree.cpp:38
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: ConfigTree.cpp:80
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: ConfigTree.cpp:57