$darkmode
fable::schema::Struct Class Reference

#include <struct.hpp>

Inheritance diagram for fable::schema::Struct:
Collaboration diagram for fable::schema::Struct:

Public Member Functions

 Struct (std::string desc="")
 
 Struct (std::string desc, PropertyList< Box > props)
 
 Struct (PropertyList< Box > props)
 
 Struct (std::string desc, const Box &base, PropertyList< Box > props)
 
 Struct (const Box &base, PropertyList< Box > props)
 
void set_property (const std::string &key, Box &&s)
 
Struct property (const std::string &key, Box &&s) &&
 
void set_properties (PropertyList< Box > props)
 
void set_properties_from (const Struct &s)
 
void set_properties_from (const Box &s)
 
template<typename T , typename = enable_if_confable_t<T>>
void set_properties_from (const T *x)
 
template<typename T >
Struct properties_from (const T x) &&
 
void set_require (std::initializer_list< std::string > init)
 
Struct require (std::initializer_list< std::string > init) &&
 
void set_require_all ()
 
Struct require_all () &&
 
Struct additional_properties (bool v) &&
 
template<typename S , typename = enable_if_schema_t<S>>
void set_additional_properties (const S &s)
 
template<typename S , typename = enable_if_schema_t<S>>
Struct additional_properties (const S &s) &&
 
bool additional_properties () const
 
void reset_ptr () override
 
Json usage () const override
 
Json json_schema () const override
 
bool validate (const Conf &c, std::optional< SchemaError > &err) const override
 
void to_json (Json &j) const override
 
void from_conf (const Conf &c) override
 
virtual Json to_json () const
 
virtual void to_json (Json &) const=0
 
- Public Member Functions inherited from fable::schema::Base< Struct >
std::unique_ptr< Interfaceclone () const override
 
 operator Box () const
 
JsonType type () const override
 
std::string type_string () const override
 
Json usage () const override
 
bool is_required () const override
 
Struct require () &&
 
Struct required (bool value) &&
 
Struct reset_pointer () &&
 
bool has_description () const
 
void set_description (std::string s) override
 
const std::string & description () const override
 
Struct description (std::string desc) &&
 
- Public Member Functions inherited from fable::schema::Interface
virtual bool is_variant () const
 
virtual void validate_or_throw (const Conf &c) const final
 
virtual std::optional< SchemaErrorfail (const Conf &c) const final
 
virtual bool is_valid (const Conf &c) const final
 
virtual Json to_json () const
 

Additional Inherited Members

- Protected Member Functions inherited from fable::schema::Base< Struct >
 Base (const Base< Struct > &)=default
 
 Base (Base< Struct > &&) noexcept=default
 
 Base (JsonType t, std::string desc)
 
 Base (JsonType t)
 
 Base (std::string desc)
 
Base< Struct > & operator= (const Base< Struct > &)=default
 
Base< Struct > & operator= (Base< Struct > &&) noexcept=default
 
bool validate_type (const Conf &c, std::optional< SchemaError > &err) const
 
SchemaError error (const Conf &c, std::string_view format, Args &&... args) const
 
SchemaError error (const ConfError &e) const
 
SchemaError wrong_type (const Conf &c) const
 
bool set_error (std::optional< SchemaError > &err, const Conf &c, std::string_view format, Args &&... args) const
 
bool set_error (std::optional< SchemaError > &err, const ConfError &e) const
 
bool set_error (std::optional< SchemaError > &err, SchemaError &&e) const
 
bool set_wrong_type (std::optional< SchemaError > &err, const Conf &c) const
 
void augment_schema (Json &j) const
 
- Protected Member Functions inherited from fable::schema::Interface
 Interface (const Interface &)=default
 
 Interface (Interface &&) noexcept=default
 
Interfaceoperator= (const Interface &)=default
 
Interfaceoperator= (Interface &&) noexcept=default
 
- Protected Attributes inherited from fable::schema::Base< Struct >
JsonType type_
 
bool required_
 
std::string desc_
 

Detailed Description

Struct maintains a key-value mapping, where the list of keys is usually known and the values can have different types (schemas).

This is usually the basis of any schema, since most start with a JSON object.

This should not be confused with the Map type.

See also
fable/schema/map.hpp

Constructor & Destructor Documentation

◆ Struct()

fable::schema::Struct::Struct ( std::string  desc,
const Box base,
PropertyList< Box props 
)
inline

Instantiate a Struct with a base Schema, which should also be a Struct, then extend it with the property list.

This is particularly useful when the Confable is inheriting from a base class:

struct Sub : public Base {
  std::string member;
  CONFABLE_SCHEMA(Sub) {
    return Struct{
      Base::schema_impl(),
      {
        {"member", make_schema(&member, "important addition")},
      }
    };
  }
};

Warning: When implementing schema_impl, for example with CONFABLE_SCHEMA, it is absolutely important that you not call Base::schema(), as this will internally call this->schema_impl(), which will lead to an infinite recursion! Instead, call Base::schema_impl().

Here is the call graph for this function:

Member Function Documentation

◆ additional_properties()

Struct fable::schema::Struct::additional_properties ( bool  v) &&
inline

Set whether to tolerate unknown fields in this entry.

  • The default is false.
  • Meant to be used during construction.

◆ from_conf()

void fable::schema::Struct::from_conf ( const Conf )
overridevirtual

Apply the input JSON configuration.

This does not validate the input.

Implements fable::schema::Interface.

Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.
Here is the call graph for this function:

◆ json_schema()

Json fable::schema::Struct::json_schema ( ) const
overridevirtual

Return the JSON schema.

Example output:

{ "$schema": "http://json-schema.org/draft-07/schema#", "description": "stand-in no-operation simulator", "properties": { "vehicles": { "description": "list of vehicle names to make available", "items": { "type": "string" }, "type": "array" } }, "title": "nop", "additionalProperties": false, "type": "object" }

See the following links for the specification:

Implements fable::schema::Interface.

Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.

◆ reset_ptr()

void fable::schema::Struct::reset_ptr ( )
overridevirtual

Reset the internal pointer to nullptr, protecting against invalid access.

This should be used when a schema is used after the backing data has been deleted.

Implements fable::schema::Interface.

Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.

◆ set_properties()

void fable::schema::Struct::set_properties ( PropertyList< Box props)

Set all properties.

  • This overwrites any already existing property of the same key.
Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_properties_from()

void fable::schema::Struct::set_properties_from ( const Struct s)
inline

Add the properties from s to this schema.

  • This will overwrite any existing properties.
Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.
Here is the call graph for this function:

◆ set_property()

void fable::schema::Struct::set_property ( const std::string &  key,
Box &&  s 
)

Set the property to this schema.

  • This overwrites any already existing field of the same key.
Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.
Here is the caller graph for this function:

◆ set_require()

void fable::schema::Struct::set_require ( std::initializer_list< std::string >  init)

Set which entries are required.

  • Complexity: O(n*m) with n the number of current properties and m the number of properties in init.
Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.

◆ set_require_all()

void fable::schema::Struct::set_require_all ( )

Set whether all entries are required.

  • The default is false.
  • Meant to be used during construction.
  • This will only act on properties that exist at the time that this is called.
Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.

◆ to_json() [1/3]

virtual Json fable::schema::Interface::to_json
inline

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.

◆ to_json() [2/3]

virtual void fable::schema::Interface::to_json

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

◆ to_json() [3/3]

void fable::schema::Struct::to_json ( Json ) const
overridevirtual

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

Implements fable::schema::Interface.

◆ usage()

Json fable::schema::Struct::usage ( ) const
overridevirtual

Return a compact JSON description of the schema. This is useful for human-readable error output.

Example output:

{ "field1": "boolean! :: lorem ipsum dolor sit amet", "field2": { "field2.1": "integer :: lorem ipsum dolor sit amet" "field2.2": "boolean :: lorem ipsum dolor sit amet" }, "field3": "array of integer :: lorem ipsum dolor sit amet", "field4": [{ "field4.1": "string :: lorem ipsum dolor sit amet" "field4.2": "boolean :: lorem ipsum dolor sit amet" }] }

When describing the schema of a primitive array, we can represent that as a single string. But when the array contains an object, we wrap a single object in an array. This isn't awfully consistent, but it's the best we can do.

Implements fable::schema::Interface.

Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.

◆ validate()

bool fable::schema::Struct::validate ( const Conf c,
std::optional< SchemaError > &  error 
) const
overridevirtual

Validate the input JSON configuration for correctness.

  • This method should only set error if there is an error. This method should not reset error if there is no error. Therefore, the content of error is only valid if the method returns false. This allows you to chain validates and check at the end if there was an error.
  • This method should not throw if there is a schema error!
Parameters
cJSON to check
errorreference to store error if occurred
Returns
true if valid

Implements fable::schema::Interface.

Examples
/home/docs/checkouts/readthedocs.org/user_builds/cloe/checkouts/latest/fable/include/fable/schema/struct.hpp.
Here is the call graph for this function:

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