Fawkes API  Fawkes Development Version
Fact.cpp
1 
2 /****************************************************************************
3  * Fact
4  * (auto-generated, do not modify directly)
5  *
6  * CLIPS REST API.
7  * Enables access to CLIPS environments.
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 "Fact.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 Fact::Fact(const std::string &json)
28 {
29  from_json(json);
30 }
31 
32 Fact::Fact(const rapidjson::Value &v)
33 {
34  from_json_value(v);
35 }
36 
38 {
39 }
40 
41 std::string
42 Fact::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 Fact::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 (index_) {
79  rapidjson::Value v_index;
80  v_index.SetInt64(*index_);
81  v.AddMember("index", v_index, allocator);
82  }
83  if (template_name_) {
84  rapidjson::Value v_template_name;
85  v_template_name.SetString(*template_name_, allocator);
86  v.AddMember("template_name", v_template_name, allocator);
87  }
88  if (formatted_) {
89  rapidjson::Value v_formatted;
90  v_formatted.SetString(*formatted_, allocator);
91  v.AddMember("formatted", v_formatted, allocator);
92  }
93  rapidjson::Value v_slots(rapidjson::kArrayType);
94  v_slots.Reserve(slots_.size(), allocator);
95  for (const auto &e : slots_) {
96  rapidjson::Value v(rapidjson::kObjectType);
97  e->to_json_value(d, v);
98  v_slots.PushBack(v, allocator);
99  }
100  v.AddMember("slots", v_slots, allocator);
101 }
102 
103 void
104 Fact::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 Fact::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("index") && d["index"].IsInt64()) {
122  index_ = d["index"].GetInt64();
123  }
124  if (d.HasMember("template_name") && d["template_name"].IsString()) {
125  template_name_ = d["template_name"].GetString();
126  }
127  if (d.HasMember("formatted") && d["formatted"].IsString()) {
128  formatted_ = d["formatted"].GetString();
129  }
130  if (d.HasMember("slots") && d["slots"].IsArray()) {
131  const rapidjson::Value &a = d["slots"];
132  slots_ = std::vector<std::shared_ptr<SlotValue>>{};
133  ;
134  slots_.reserve(a.Size());
135  for (auto &v : a.GetArray()) {
136  std::shared_ptr<SlotValue> nv{new SlotValue()};
137  nv->from_json_value(v);
138  slots_.push_back(std::move(nv));
139  }
140  }
141 }
142 
143 void
144 Fact::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 (!index_)
152  missing.push_back("index");
153  if (!template_name_)
154  missing.push_back("template_name");
155 
156  if (!missing.empty()) {
157  if (subcall) {
158  throw missing;
159  } else {
160  std::ostringstream s;
161  s << "Fact is missing field" << ((missing.size() > 0) ? "s" : "") << ": ";
162  for (std::vector<std::string>::size_type i = 0; i < missing.size(); ++i) {
163  s << missing[i];
164  if (i < (missing.size() - 1)) {
165  s << ", ";
166  }
167  }
168  throw std::runtime_error(s.str());
169  }
170  }
171 }
Fact::from_json
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: Fact.cpp:104
Fact::from_json_value
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: Fact.cpp:113
Fact::~Fact
virtual ~Fact()
Destructor.
Definition: Fact.cpp:37
Fact::validate
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: Fact.cpp:144
SlotValue::from_json_value
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: SlotValue.cpp:103
Fact::to_json
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: Fact.cpp:42
SlotValue
SlotValue representation for JSON transfer.
Definition: SlotValue.h:28
Fact::to_json_value
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: Fact.cpp:61
Fact::Fact
Fact()
Constructor.
Definition: Fact.cpp:23