Fawkes API  Fawkes Development Version
DomainFact.cpp
1 
2 /****************************************************************************
3  * DomainFact
4  * (auto-generated, do not modify directly)
5  *
6  * CLIPS Executive REST API.
7  * Enables access to goals, plans, and all items in the domain model.
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 "DomainFact.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 <numeric>
22 #include <sstream>
23 
25 {
26 }
27 
28 DomainFact::DomainFact(const std::string &json)
29 {
30  from_json(json);
31 }
32 
33 DomainFact::DomainFact(const rapidjson::Value &v)
34 {
35  from_json_value(v);
36 }
37 
39 {
40 }
41 
42 std::string
43 DomainFact::to_json(bool pretty) const
44 {
45  rapidjson::Document d;
46 
47  to_json_value(d, d);
48 
49  rapidjson::StringBuffer buffer;
50  if (pretty) {
51  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
52  d.Accept(writer);
53  } else {
54  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
55  d.Accept(writer);
56  }
57 
58  return buffer.GetString();
59 }
60 
61 void
62 DomainFact::to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
63 {
64  rapidjson::Document::AllocatorType &allocator = d.GetAllocator();
65  v.SetObject();
66  // Avoid unused variable warnings
67  (void)allocator;
68 
69  if (kind_) {
70  rapidjson::Value v_kind;
71  v_kind.SetString(*kind_, allocator);
72  v.AddMember("kind", v_kind, allocator);
73  }
74  if (apiVersion_) {
75  rapidjson::Value v_apiVersion;
76  v_apiVersion.SetString(*apiVersion_, allocator);
77  v.AddMember("apiVersion", v_apiVersion, allocator);
78  }
79  if (name_) {
80  rapidjson::Value v_name;
81  v_name.SetString(*name_, allocator);
82  v.AddMember("name", v_name, allocator);
83  }
84  rapidjson::Value v_param_values(rapidjson::kArrayType);
85  v_param_values.Reserve(param_values_.size(), allocator);
86  for (const auto &e : param_values_) {
87  rapidjson::Value v;
88  v.SetString(e, allocator);
89  v_param_values.PushBack(v, allocator);
90  }
91  v.AddMember("param-values", v_param_values, allocator);
92 }
93 
94 void
95 DomainFact::from_json(const std::string &json)
96 {
97  rapidjson::Document d;
98  d.Parse(json);
99 
100  from_json_value(d);
101 }
102 
103 void
104 DomainFact::from_json_value(const rapidjson::Value &d)
105 {
106  if (d.HasMember("kind") && d["kind"].IsString()) {
107  kind_ = d["kind"].GetString();
108  }
109  if (d.HasMember("apiVersion") && d["apiVersion"].IsString()) {
110  apiVersion_ = d["apiVersion"].GetString();
111  }
112  if (d.HasMember("name") && d["name"].IsString()) {
113  name_ = d["name"].GetString();
114  }
115  if (d.HasMember("param-values") && d["param-values"].IsArray()) {
116  const rapidjson::Value &a = d["param-values"];
117  param_values_ = std::vector<std::string>{};
118 
119  param_values_.reserve(a.Size());
120  for (auto &v : a.GetArray()) {
121  param_values_.push_back(v.GetString());
122  }
123  }
124 }
125 
126 void
127 DomainFact::validate(bool subcall) const
128 {
129  std::vector<std::string> missing;
130  if (!kind_) {
131  missing.push_back("kind");
132  }
133  if (!apiVersion_) {
134  missing.push_back("apiVersion");
135  }
136  if (!name_) {
137  missing.push_back("name");
138  }
139 
140  if (!missing.empty()) {
141  if (subcall) {
142  throw missing;
143  } else {
144  std::string s =
145  std::accumulate(std::next(missing.begin()),
146  missing.end(),
147  missing.front(),
148  [](std::string &s, const std::string &n) { return s + ", " + n; });
149  throw std::runtime_error("DomainFact is missing " + s);
150  }
151  }
152 }
virtual std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: DomainFact.cpp:43
virtual void validate(bool subcall=false) const
Validate if all required fields have been set.
Definition: DomainFact.cpp:127
virtual void to_json_value(rapidjson::Document &d, rapidjson::Value &v) const
Render object to JSON.
Definition: DomainFact.cpp:62
virtual void from_json_value(const rapidjson::Value &v)
Retrieve data from JSON string.
Definition: DomainFact.cpp:104
virtual ~DomainFact()
Destructor.
Definition: DomainFact.cpp:38
DomainFact()
Constructor.
Definition: DomainFact.cpp:24
virtual void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: DomainFact.cpp:95