$darkmode
fable::schema::Variant Class Reference

#include <variant.hpp>

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

Public Member Functions

 Variant (std::initializer_list< Box > vec)
 
 Variant (std::string &&desc, std::initializer_list< Box > vec)
 
 Variant (std::vector< Box > &&vec)
 
 Variant (std::string &&desc, std::vector< Box > &&vec)
 
Interfaceclone () const override
 
 operator Box () const
 
JsonType type () const override
 
std::string type_string () const override
 
bool is_variant () const override
 
Json usage () const override
 
bool is_required () const override
 
Variant require () &&
 
Variant required (bool value) &&
 
bool has_description () const
 
void set_description (const std::string &s) override
 
void set_description (std::string &&s)
 
const std::string & description () const override
 
Variant description (std::string &&desc) &&
 
Variant unique_match (bool value) &&
 
Variant reset_pointer () &&
 
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
 
void reset_ptr () override
 
- Public Member Functions inherited from fable::schema::Interface
virtual bool is_valid (const Conf &c) const
 
virtual Json to_json () const
 

Detailed Description

Variant deserializes JSON data into one of multiple variants.

Whenever the JSON input can come in different forms but is more or less destined for the same variable (or set of variables), we need to be able to deal with variants. For example, an enumeration is a variant of constants.

Deciding which variant to use for deserializing JSON is not always unambiguous. This class will simply use the first valid schema. For this reason it is strongly recommended to pay attention to making all schemas that are part of a variant distinct from one another. One way to do this is to introduce a required constant (such as a value from an enumeration) into the schema.

Serialization uses the very first schema in the variant list for output. For this reason, the very first schema in the list should contain the schema of the desired output.

Member Function Documentation

◆ clone()

Interface* fable::schema::Variant::clone ( ) const
inlineoverridevirtual

Return a new instance of the object.

This is implemented by Base and allows us to wrap implementors of Interface with Schema.

Implements fable::schema::Interface.

◆ description()

const std::string& fable::schema::Variant::description ( ) const
inlineoverridevirtual

Return human-readable description.

Implements fable::schema::Interface.

Here is the call graph for this function:

◆ from_conf()

void fable::schema::Variant::from_conf ( const Conf )
inlineoverridevirtual

Apply the input JSON configuration.

This does not validate the input.

Implements fable::schema::Interface.

Here is the call graph for this function:

◆ is_required()

bool fable::schema::Variant::is_required ( ) const
inlineoverridevirtual

Return whether this interface needs to be set.

Implements fable::schema::Interface.

◆ is_variant()

bool fable::schema::Variant::is_variant ( ) const
inlineoverridevirtual

Return whether the accepted input type is a variant.

A variant type is one that is composed of other types, such as the special types: any-of, one-of, and all-of.

Reimplemented from fable::schema::Interface.

Here is the call graph for this function:

◆ json_schema()

Json fable::schema::Variant::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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset_ptr()

void fable::schema::Variant::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.

Here is the caller graph for this function:

◆ set_description()

void fable::schema::Variant::set_description ( const std::string &  s)
inlineoverridevirtual

Set human-readable description.

Implements fable::schema::Interface.

◆ to_json()

void fable::schema::Variant::to_json ( Json ) const
inlineoverridevirtual

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.

◆ type()

JsonType fable::schema::Variant::type ( ) const
inlineoverridevirtual

Return the JSON type.

If this is a variant type of differing types, then null should be returned. Otherwise, the type remains unique and can be returned. A type that is null is almost always a variant type of some sort, if even only an optional type.

Implements fable::schema::Interface.

◆ type_string()

std::string fable::schema::Variant::type_string ( ) const
inlineoverridevirtual

Return the type as a string.

The format of the string is:

"[array of] TYPE"

where TYPE is one of: null, object, boolean, float, integer, unsigned, string, and unknown.

Example output:

"object"
"array of boolean"
"array of object"

Implements fable::schema::Interface.

◆ usage()

Json fable::schema::Variant::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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validate()

void fable::schema::Variant::validate ( const Conf c) const
inlineoverridevirtual

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.


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