SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Json::ArrayRequirement Class Reference

Array of Requirements. More...

#include <ArrayRequirement.h>

Inheritance diagram for Json::ArrayRequirement:
Inheritance graph
[legend]

Public Member Functions

 ArrayRequirement ()
 Constructor.
 
virtual void ClearErrors () override
 Clear list of error messages for all Requirements.
 
virtual void ClearNotices () override
 Clear notices for all Requirements.
 
virtual void Reset () override
 Reset array.
 
virtual void Parse (Value json, const std::string &path) override
 Parse JSON value and fill array. More...
 
virtual void Validate (const Value &json, const std::string &path) override
 Validate json value. More...
 
- Public Member Functions inherited from Json::Requirement
bool HasErrors ()
 Check if errors have occured. More...
 
std::vector< std::string > GetErrors ()
 Get list of error messages. More...
 
virtual bool HasNotices ()
 Check if notices have been queued. More...
 
std::vector< std::string > GetNotices ()
 Get list of notices. More...
 
virtual ~Requirement ()
 Destructor.
 

Private Member Functions

template<class T >
bool IsUnique (T X)
 Test if set is unique. More...
 

Private Attributes

RequireList items_
 List of Requirements.
 
std::unique_ptr< Requirementitem_
 Single Requirement.
 
bool addItems_
 True if items can be added.
 
bool setMin_
 True if minimum has been set.
 
bool setMax_
 True if maximum has been set.
 
bool unique_
 True if all Requirements are unique.
 
int min_
 Minimum value.
 
int max_
 Maximum value.
 

Additional Inherited Members

- Protected Member Functions inherited from Json::Requirement
void PushError (const std::string &error)
 Add error to list of error messages. More...
 
void PushNotice (const std::string &notice)
 Add message to list of notices. More...
 

Detailed Description

Array of Requirements.

Definition at line 43 of file ArrayRequirement.h.

Member Function Documentation

◆ IsUnique()

template<class T >
bool Json::ArrayRequirement::IsUnique ( X)
inlineprivate

Test if set is unique.

Template Parameters
TContainer class.
Parameters
XContainer containing Requirements.
Returns
True if no element in the container is duplicate.

Definition at line 64 of file ArrayRequirement.h.

65  {
66  std::set<T> Y(X.begin(), X.end());
67  return X.size() == Y.size();
68  }

Referenced by Validate().

Here is the caller graph for this function:

◆ Parse()

virtual void Json::ArrayRequirement::Parse ( Value  json,
const std::string &  path 
)
inlineoverridevirtual

Parse JSON value and fill array.

Parameters
jsonJSON input value.
pathPath for JSON path specification.

This function parses the JSON input value and creates new Requirements for all values in the "items" branch.

Implements Json::Requirement.

Definition at line 125 of file ArrayRequirement.h.

126  {
127  Reset();
128  RequirementLoader loader;
129 
130  if(json.isMember("items") && json["items"].isObject())
131  {
132  if((item_ = loader.LoadRequirement(json["items"])))
133  item_->Parse(json["items"], path);
134  }
135  else if(json.isMember("items") && json["items"].isArray())
136  {
137  for(auto& item : json["items"])
138  {
139  if(auto iptr = loader.LoadRequirement(item))
140  {
141  items_.push_back(std::move(iptr));
142  items_.back()->Parse(item, path);
143  }
144  }
145  }
146 
147  if(json.isMember("additionalItems") && json["additionalItems"].isBool())
148  addItems_ = json["additionalItems"].asBool();
149 
150  if(json.isMember("minItems") && json["minItems"].isInt())
151  {
152  setMin_ = true;
153  min_ = json["minItems"].asInt();
154  }
155 
156  if(json.isMember("maxItems") && json["maxItems"].isInt())
157  {
158  setMax_ = true;
159  max_ = json["maxItems"].asInt();
160  }
161 
162  if(json.isMember("uniqueItems") && json["uniqueItems"].isBool())
163  unique_ = json["uniqueItems"].asBool();
164  }
bool setMax_
True if maximum has been set.
bool setMin_
True if minimum has been set.
bool addItems_
True if items can be added.
virtual void Reset() override
Reset array.
int min_
Minimum value.
RequireList items_
List of Requirements.
bool unique_
True if all Requirements are unique.
std::unique_ptr< Requirement > item_
Single Requirement.
int max_
Maximum value.

References addItems_, item_, items_, Json::RequirementLoader::LoadRequirement(), max_, min_, Reset(), setMax_, setMin_, and unique_.

Here is the call graph for this function:

◆ Validate()

virtual void Json::ArrayRequirement::Validate ( const Value &  json,
const std::string &  path 
)
inlineoverridevirtual

Validate json value.

Parameters
jsonJSON value to be validated.
pathPath for JSON path specification.

Implements Json::Requirement.

Definition at line 171 of file ArrayRequirement.h.

172  {
173  if(!json.isArray())
174  {
175  PushError(path + ": Must be of type \"array\"");
176  return;
177  }
178 
179  if(item_ != nullptr)
180  {
181  int i = 0;
182  for(const auto& item : json)
183  {
184  item_->Validate(item, path + "/" + std::to_string(i));
185  ++i;
186  }
187 
188  // Merge errors.
189  for(const auto& error : item_->GetErrors())
190  PushError(error);
191 
192  for(const auto& notice : item_->GetNotices())
193  PushNotice(notice);
194  }
195 
196  if(items_.size() != 0)
197  {
198 
199  if(!addItems_ && json.size() > items_.size())
200  PushError(path + ": There cannot be any additional items in the array");
201 
202  int iv = std::min((int)items_.size(), (int)json.size());
203  for(int i = 0; i < iv; ++i)
204  {
205  items_[i]->Validate(json[i], path);
206 
207  // Merge errors.
208  for(const auto& error : items_[i]->GetErrors())
209  PushError(error);
210 
211  for(const auto& notice : items_[i]->GetNotices())
212  PushNotice(notice);
213  }
214  }
215 
216  if(setMin_ && (int)json.size() < min_)
217  PushError(path + ": There must be at least " + std::to_string(min_) + " elements in the array");
218 
219  if(setMax_ && (int)json.size() > max_)
220  PushError(path + ": There must be no more than " + std::to_string(max_) + " elements in the array");
221 
222  if(unique_ && !IsUnique(json))
223  PushError(path + ": Entries must be unique");
224 
225 
226  }
bool IsUnique(T X)
Test if set is unique.
void PushNotice(const std::string &notice)
Add message to list of notices.
Definition: Requirement.h:62
std::vector< std::string > GetNotices()
Get list of notices.
Definition: Requirement.h:107
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
void PushError(const std::string &error)
Add error to list of error messages.
Definition: Requirement.h:53

References addItems_, Json::Requirement::GetErrors(), Json::Requirement::GetNotices(), IsUnique(), item_, items_, max_, min_, Json::Requirement::PushError(), Json::Requirement::PushNotice(), setMax_, setMin_, and unique_.

Here is the call graph for this function:

The documentation for this class was generated from the following file: