libcruft-util/json/constraint/combine.cpp

96 lines
2.8 KiB
C++

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#include "combine.hpp"
#include "properties.hpp"
#include "../tree.hpp"
#include "../../iterator.hpp"
#include "../../cast.hpp"
using util::json::schema::constraint::combine;
///////////////////////////////////////////////////////////////////////////////
combine::combine (::json::tree::node const &def)
{
for (auto const &[key,val]: def.as_object ()) {
if (key == "$schema") {
m_version = val->as_string ();
continue;
}
if (key == "default") {
m_default = val->clone ();
continue;
}
m_constraints.push_back (instantiate (key, *val));
if (key == "additionalProperties") {
m_additional = util::cast::known<additional_properties> (m_constraints.back ().get ());
m_additional->use (m_properties);
m_additional->use (m_patterns);
} else if (key == "properties") {
m_properties = util::cast::known<properties> (m_constraints.back().get ());
if (m_additional)
m_additional->use (m_properties);
} else if (key == "patternProperties") {
m_patterns = util::cast::known<pattern_properties> (m_constraints.back ().get ());
if (m_additional)
m_additional->use (m_patterns);
}
}
}
///////////////////////////////////////////////////////////////////////////////
combine::output_iterator
combine::validate (output_iterator res, ::json::tree::node &data) const noexcept
{
for (auto const &i: m_constraints)
res = i->validate (res, data);
return res;
}
///////////////////////////////////////////////////////////////////////////////
std::ostream&
combine::describe (std::ostream &os) const
{
return os << "{ combine: [ ";
for (auto const &i: m_constraints)
os << *i << ", ";
return os << " ] }";
}
///////////////////////////////////////////////////////////////////////////////
bool
combine::has_default (void) const
{
return !!m_default;
}
//-----------------------------------------------------------------------------
std::unique_ptr<::json::tree::node>
combine::default_value (void) const
{
return m_default->clone ();
}