$darkmode
fable::schema::Struct Class Reference

#include <struct.hpp>

Inheritance diagram for fable::schema::Struct:
[legend]
Collaboration diagram for fable::schema::Struct:
[legend]

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
 
void validate (const Conf &c) const override
 
void to_json (Json &j) const override
 
void from_conf (const Conf &c) override
 
- Public Member Functions inherited from fable::schema::Base< Struct >
 Base (JsonType t, std::string &&desc)
 
 Base (JsonType t)
 
 Base (std::string &&desc)
 
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 (const std::string &s) override
 
void set_description (std::string &&s)
 
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 bool is_valid (const Conf &c) const
 
virtual Json to_json () const
 

Additional Inherited Members

- Protected Member Functions inherited from fable::schema::Base< Struct >
void validate_type (const Conf &c) const
 
void throw_error (const Conf &c, const char *format, Args... args) const
 
void throw_error (const ConfError &e) const
 
void throw_wrong_type (const Conf &c) const
 
void augment_schema (Json &j) const
 
- 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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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/v0.20.0/fable/include/fable/schema/struct.hpp.
Here is the call graph for this function:
Here is the caller 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/v0.20.0/fable/include/fable/schema/struct.hpp.
Here is the caller graph for this function:

◆ 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/v0.20.0/fable/include/fable/schema/struct.hpp.
Here is the caller graph for this function:

◆ 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/v0.20.0/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/v0.20.0/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/v0.20.0/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/v0.20.0/fable/include/fable/schema/struct.hpp.
Here is the caller graph for this function:

◆ 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/v0.20.0/fable/include/fable/schema/struct.hpp.
Here is the caller graph for this function:

◆ to_json()

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/v0.20.0/fable/include/fable/schema/struct.hpp.
Here is the caller graph for this function:

◆ validate()

void fable::schema::Struct::validate ( const Conf c) const
overridevirtual

Validate the input JSON configuration.

If you don't have a Conf but would like to validate a Json type, then construct a Conf on the fly:

s.validate(Conf{j});

This function is not provided inline to prevent incorrect use.

Implements fable::schema::Interface.

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

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