CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CASM::KwargsParser Struct Reference

#include <InputParser.hh>

+ Inheritance diagram for CASM::KwargsParser:

Detailed Description

Base data structure for parsing values from JSON and storing error and warning messages

This contains two jsonParser references:

  • input: which is a reference to the top of the JSON document (technically, this can be any object in the JSON document from which all subsequent paths with be relative to).
  • self: which is a reference to the JSON subobject this->input.find_at(this->path) that will be parsed when member functions (require, optional, etc.) are called and which any error or warning messages they create are referenced to.

Basic example:

jsonParser json = jsonParser::parse(R"({
"an_options_data_structure": {
"an_integer_parameter" : 20,
"another_integer_parameter": "a string",
}
})";
bool is_required = true;
KwargsParser parser {json, fs::path {"an_options_data_structure"},
is_required}; int an_integer_parameter;
// this will assign 20 to `an_integer_parameter` without error
parser.require(an_integer_parameter, "an_integer_parameter");
int another_integer_parameter;
// this will assign nothing `another_integer_parameter` and store an error
message of type:
// "Error: could not construct type 'int' from option
'another_integer_parameter'." parser.require(another_integer_parameter,
"another_integer_parameter");
// this will construct a KwargsParser with error message of type:
// "Error: Required property 'another_options_data_structure' not found."
KwargsParser other_parser {json, fs::path
{"another_options_data_structure"}, is_required};
// since other_parser was constructed with an error, other_parser.valid()
will be false assert(!other_parser.valid()};
static jsonParser parse(const std::string &str)
Construct a jsonParser from a string containing JSON data.
Definition: jsonParser.hh:382
KwargsParser(jsonParser const &_input, fs::path _path, bool _required)
Definition: InputParser.cc:20

Similar options allow returning parsed values instead of assigning, optionally parsing if values exists in the JSON, specifying default values if no value exists, etc.

The make_report, print_errors and print_warnings methods allow formatted output of error and warning messages.

Definition at line 66 of file InputParser.hh.

Public Types

typedef std::multimap< fs::path, std::shared_ptr< KwargsParser > > map_type
 

Public Member Functions

 KwargsParser (jsonParser const &_input, fs::path _path, bool _required)
 
virtual ~KwargsParser ()
 
const jsonParserparent () const
 
fs::path parent_path () const
 
std::string name () const
 Name of this->self, equivalent to this->path.filename().string() More...
 
bool exists () const
 
fs::path relpath (const fs::path &val) const
 Return this->path / val, ensuring the result is a relative path. More...
 
map_type::const_iterator begin () const
 Begin iterator over subparsers. More...
 
map_type::const_iterator end () const
 End iterator over subparsers. More...
 
bool valid () const
 Return true if this and and all subparsers are valid. More...
 
std::map< fs::path, std::set< std::string > > all_warnings () const
 Return warning messages from this and all subparsers. More...
 
std::map< fs::path, std::set< std::string > > all_errors () const
 Return error messages from this and all subparsers. More...
 
void insert (fs::path path, const std::shared_ptr< KwargsParser > &subparser)
 
void insert_error (fs::path option, std::string message)
 Insert a subparser at location option with a single error message More...
 
void insert_warning (fs::path option, std::string message)
 Insert a subparser at location option with a single warning message More...
 
bool warn_unnecessary (const std::set< std::string > &expected)
 Insert a warning if any unexpected JSON attributes are found in self. More...
 
Validatorinsert (const Validator &other)
 
void clear ()
 
Validatorinsert (const Validator &other)
 

Public Attributes

jsonParser const & input
 Reference to the top of the JSON document being parsed. More...
 
fs::path path
 
jsonParser const & self
 
bool required
 If this->input.at(this->path) is required to exist. More...
 
std::string type_name
 
std::set< std::string > error
 
std::set< std::string > warning
 

Private Types

typedef map_type::value_type PairType
 

Private Attributes

map_type m_subparsers
 

Member Typedef Documentation

◆ map_type

typedef std::multimap<fs::path, std::shared_ptr<KwargsParser> > CASM::KwargsParser::map_type

Definition at line 134 of file InputParser.hh.

◆ PairType

typedef map_type::value_type CASM::KwargsParser::PairType
private

Definition at line 172 of file InputParser.hh.

Constructor & Destructor Documentation

◆ KwargsParser()

CASM::KwargsParser::KwargsParser ( jsonParser const &  _input,
fs::path  _path,
bool  _required 
)

Construct KwargsParser

Parameters
_inputReference to top of the JSON document
_pathLocation of JSON component, relative to input, to be parsed
_requiredIf true, insert an error if JSON component refered to by _path does not exist

If required to exist, but does not, the KwargsParser will be constructed and the following error will be inserted:

  • "Error: Required property '<_path>' not found."

Definition at line 20 of file InputParser.cc.

◆ ~KwargsParser()

virtual CASM::KwargsParser::~KwargsParser ( )
inlinevirtual

Definition at line 102 of file InputParser.hh.

Member Function Documentation

◆ all_errors()

std::map< fs::path, std::set< std::string > > CASM::KwargsParser::all_errors ( ) const

Return error messages from this and all subparsers.

Definition at line 77 of file InputParser.cc.

◆ all_warnings()

std::map< fs::path, std::set< std::string > > CASM::KwargsParser::all_warnings ( ) const

Return warning messages from this and all subparsers.

Definition at line 61 of file InputParser.cc.

◆ begin()

KwargsParser::map_type::const_iterator CASM::KwargsParser::begin ( ) const

Begin iterator over subparsers.

Definition at line 48 of file InputParser.cc.

◆ clear()

void CASM::Validator::clear ( )
inlineinherited

Definition at line 14 of file Validator.hh.

◆ end()

KwargsParser::map_type::const_iterator CASM::KwargsParser::end ( ) const

End iterator over subparsers.

Definition at line 52 of file InputParser.cc.

◆ exists()

bool CASM::KwargsParser::exists ( ) const

Check if this->input.find_at(this->path) exists

When KwargsParser is contructed with a path that does not exist, then this->self=this->input.

Definition at line 44 of file InputParser.cc.

◆ insert() [1/3]

Validator& CASM::Validator::insert
inline

Definition at line 19 of file Validator.hh.

◆ insert() [2/3]

Validator& CASM::Validator::insert ( const Validator other)
inlineinherited

Definition at line 19 of file Validator.hh.

◆ insert() [3/3]

void CASM::KwargsParser::insert ( fs::path  path,
const std::shared_ptr< KwargsParser > &  subparser 
)

Insert a subparser

Subparsers are stored in a map of path (from this->input) to the subparser. After being inserted, the subparser's errors and warnings are included in validation checks (valid) and in parser output (make_report, print_warnings, print_errors, etc.).

Definition at line 92 of file InputParser.cc.

◆ insert_error()

void CASM::KwargsParser::insert_error ( fs::path  option,
std::string  message 
)

Insert a subparser at location option with a single error message

Definition at line 97 of file InputParser.cc.

◆ insert_warning()

void CASM::KwargsParser::insert_warning ( fs::path  option,
std::string  message 
)

Insert a subparser at location option with a single warning message

Definition at line 104 of file InputParser.cc.

◆ name()

std::string CASM::KwargsParser::name ( ) const

Name of this->self, equivalent to this->path.filename().string()

Definition at line 42 of file InputParser.cc.

◆ parent()

const jsonParser & CASM::KwargsParser::parent ( ) const

Return a const reference to the parent JSON object of this->self

If self==input, returns self.

Definition at line 33 of file InputParser.cc.

◆ parent_path()

fs::path CASM::KwargsParser::parent_path ( ) const

Return a fs::path from the top-level to the parent JSON object of this->self

The result satisfies: this->parent()==this->input.find_at(this->parent_path())

Definition at line 40 of file InputParser.cc.

◆ relpath()

fs::path CASM::KwargsParser::relpath ( const fs::path &  val) const
inline

Return this->path / val, ensuring the result is a relative path.

Definition at line 128 of file InputParser.hh.

◆ valid()

bool CASM::KwargsParser::valid ( ) const

Return true if this and and all subparsers are valid.

Definition at line 56 of file InputParser.cc.

◆ warn_unnecessary()

bool CASM::KwargsParser::warn_unnecessary ( const std::set< std::string > &  expected)

Insert a warning if any unexpected JSON attributes are found in self.

Definition at line 111 of file InputParser.cc.

Member Data Documentation

◆ error

std::set<std::string> CASM::Validator::error
inherited

Definition at line 11 of file Validator.hh.

◆ input

jsonParser const& CASM::KwargsParser::input

Reference to the top of the JSON document being parsed.

Definition at line 68 of file InputParser.hh.

◆ m_subparsers

map_type CASM::KwargsParser::m_subparsers
private

Used to store sub-parsers. Allows code re-use w/ storage of all errors & warnings. Can use static_cast to get values from InputParser subparsers if they are included.

Definition at line 178 of file InputParser.hh.

◆ path

fs::path CASM::KwargsParser::path

Path to the JSON component to be parsed from the opt of the JSON document. If it exists, this->self = this->input.at(this->path))

Definition at line 72 of file InputParser.hh.

◆ required

bool CASM::KwargsParser::required

If this->input.at(this->path) is required to exist.

Definition at line 84 of file InputParser.hh.

◆ self

jsonParser const& CASM::KwargsParser::self

Reference to the JSON component to be parsed, if it exists, otherwise set to this->input

Note:

  • if path.empty(), or path not found in input, self = input;
    • else: self = *input.find_at(path)
  • Use this->exists() to check if the JSON component exists in the document

Definition at line 81 of file InputParser.hh.

◆ type_name

std::string CASM::KwargsParser::type_name

Default empty, can be used to differentiate between parsers when multiple values are parsed from a single JSON object

Definition at line 88 of file InputParser.hh.

◆ warning

std::set<std::string> CASM::Validator::warning
inherited

Definition at line 12 of file Validator.hh.


The documentation for this struct was generated from the following files: