Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON value s11n unit tests #603

Merged
merged 1 commit into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions drafter.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@

"test/refract/test-Utils.cc",
"test/refract/test-JsonSchema.cc",
"test/refract/test-JsonValue.cc",

"test/refract/dsd/test-Array.cc",
"test/refract/dsd/test-Bool.cc",
Expand Down
2 changes: 1 addition & 1 deletion src/RefractDataStructure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ namespace
if (!isReserved(element.element()))
return true;

if (const StringElement* value = findValue(element))
if (const StringElement* value = get<const StringElement>(findValue(element)))
// empty strings SHOULD NOT occur
// TODO @tjanc@ decide whether too strict
if (!value->get().get().empty())
Expand Down
110 changes: 91 additions & 19 deletions src/refract/ElementUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ Element* get(const IElement* e)

bool refract::inheritsFixed(const IElement& e)
{
// a default guarantees to be a valid value
if (hasDefault(e))
return true;

// otherwise interpret based on specific Element
return refract::visit(e, [](const auto& el) { //
return inheritsFixed(el);
Expand All @@ -43,18 +39,12 @@ bool refract::inheritsFixed(const IElement& e)

bool refract::inheritsFixed(const ObjectElement& e)
{
if (e.empty())
return false;

return true;
return definesValue(e);
}

bool refract::inheritsFixed(const ArrayElement& e)
{
if (e.empty())
return false;

return true;
return definesValue(e);
}

bool refract::inheritsFixed(const MemberElement& e)
Expand Down Expand Up @@ -89,17 +79,17 @@ bool refract::inheritsFixed(const NullElement& e)

bool refract::inheritsFixed(const StringElement& e)
{
return !e.empty() || hasDefault(e); // content or default
return definesValue(e);
}

bool refract::inheritsFixed(const NumberElement& e)
{
return !e.empty() || hasDefault(e); // content or default
return definesValue(e);
}

bool refract::inheritsFixed(const BooleanElement& e)
{
return !e.empty() || hasDefault(e); // content or default
return definesValue(e);
}

bool refract::inheritsFixed(const ExtendElement& e)
Expand Down Expand Up @@ -205,20 +195,102 @@ std::string refract::renderKey(const IElement& element)
}

void refract::setFixedTypeAttribute(IElement& e)
{
setTypeAttribute(e, "fixed");
}

void refract::setTypeAttribute(IElement& e, const std::string& typeAttribute)
{
auto typeAttrIt = e.attributes().find("typeAttributes");
if (e.attributes().end() == typeAttrIt) {
e.attributes().set("typeAttributes", make_element<ArrayElement>(from_primitive("fixed")));
e.attributes().set("typeAttributes", make_element<ArrayElement>(from_primitive(typeAttribute)));
} else {
if (auto* typeAttrs = get<ArrayElement>(typeAttrIt->second.get())) {
const auto b = typeAttrs->get().begin();
const auto e = typeAttrs->get().end();
if (e == std::find_if(b, e, [](const auto& el) { //
if (e == std::find_if(b, e, [&typeAttribute](const auto& el) { //
const auto* entry = get<const StringElement>(el.get());
return entry && !entry->empty() && (entry->get().get() == "fixed");
return entry && !entry->empty() && (entry->get().get() == typeAttribute);
})) {
typeAttrs->get().push_back(from_primitive("fixed"));
typeAttrs->get().push_back(from_primitive(typeAttribute));
}
}
}
}

void refract::setDefault(IElement& e, std::unique_ptr<IElement> deflt)
{
e.attributes().set("default", std::move(deflt));
}

void refract::addSample(IElement& e, std::unique_ptr<IElement> sample)
{
auto it = e.attributes().find("samples");
if (it == e.attributes().end()) {
LOG(info) << "creating new samples entry";
e.attributes().set("samples", make_element<ArrayElement>(std::move(sample)));
} else if (ArrayElement* samples = get<ArrayElement>(it->second.get())) {
if (samples->empty()) {
LOG(error) << "empty Array Element in samples";
assert(false);
}
LOG(info) << "adding new sample";
e.attributes().set("samples", make_element<ArrayElement>(std::move(sample)));
samples->get().push_back(std::move(sample));
} else {
LOG(error) << "expected samples to be held in Array Element content";
assert(false);
}
}

void refract::addEnumeration(IElement& e, std::unique_ptr<IElement> enm)
{
auto it = e.attributes().find("enumerations");
if (it == e.attributes().end())
e.attributes().set("enumerations", make_element<ArrayElement>(std::move(enm)));
else if (ArrayElement* enums = get<ArrayElement>(it->second.get())) {
if (enums->empty()) {
LOG(error) << "empty Array Element in enumerations";
assert(false);
}
enums->get().push_back(std::move(enm));
} else {
LOG(error) << "expected enumerations to be held in Array Element content";
assert(false);
}
}

const IElement* refract::findFirstSample(const IElement& e)
{
auto it = e.attributes().find("samples");
if (it != e.attributes().end()) {
if (const auto& samples = get<const ArrayElement>(it->second.get()))
if (!samples->empty() && !samples->get().empty())
return samples->get().begin()[0].get();
}
return nullptr;
}

const IElement* refract::findDefault(const IElement& e)
{
auto it = e.attributes().find("default");
if (it != e.attributes().end())
return it->second.get();
return nullptr;
}

const IElement* refract::findValue(const IElement& element)
{
if (!element.empty())
return &element;
if (const IElement* sample = findFirstSample(element))
return sample;
if (const IElement* dfault = findDefault(element))
return dfault;
return nullptr;
}

bool refract::definesValue(const IElement& e)
{
return nullptr != findValue(e);
}
48 changes: 9 additions & 39 deletions src/refract/ElementUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ namespace refract
namespace refract
{
void setFixedTypeAttribute(IElement& e);
}
void setTypeAttribute(IElement& e, const std::string& typeAttribute);
void setDefault(IElement& e, std::unique_ptr<IElement> deflt);
void addSample(IElement& e, std::unique_ptr<IElement> sample);
void addEnumeration(IElement& e, std::unique_ptr<IElement> enm);
} // namespace refract

namespace refract
{
Expand Down Expand Up @@ -114,53 +118,19 @@ namespace refract
/// @returns an Element typing a single value representing the default;
/// nullptr iff default is not found
///
template <typename Element>
const Element* findDefault(const Element& e)
{
auto it = e.attributes().find("default");
if (it != e.attributes().end()) {
if (const auto& result = get<const Element>(it->second.get()))
return result;
}
return nullptr;
}
const IElement* findDefault(const IElement& e);

///
/// Find the first sample value for an Element
///
/// @returns an Element typing a single value representing the sample;
/// nullptr iff sample is not found
///
template <typename Element>
const Element* findFirstSample(const Element& e)
{
auto it = e.attributes().find("samples");
if (it != e.attributes().end()) {
if (const auto& samples = get<const ArrayElement>(it->second.get()))
if (!samples->empty() && !samples->get().empty())
if (const auto& result = get<const Element>(samples->get().begin()[0].get()))
return result;
}
return nullptr;
}
const IElement* findFirstSample(const IElement& e);

template <typename Element>
bool definesValue(const Element& e)
{
return nullptr != findValue(e);
}
const IElement* findValue(const IElement& element);

template <typename Element>
const Element* findValue(const Element& element)
{
if (!element.empty())
return &element;
if (const Element* sample = findFirstSample(element))
return sample;
if (const Element* dfault = findDefault(element))
return dfault;
return nullptr;
}
bool definesValue(const IElement& e);

} // namespace refract

Expand Down
Loading