Skip to content

Commit e1e48dd

Browse files
inkychrispboettch
authored andcommittedSep 2, 2022
Replace dynamic_cast with member functions (#210)
1 parent 5ec1961 commit e1e48dd

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed
 

‎src/json-validator.cpp

+39-10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ class schema
4040
root_schema *root_;
4141
json default_value_ = nullptr;
4242

43+
protected:
44+
virtual std::shared_ptr<schema> make_for_default_(
45+
std::shared_ptr<::schema> & /* sch */,
46+
root_schema * /* root */,
47+
std::vector<nlohmann::json_uri> & /* uris */,
48+
nlohmann::json & /* default_value */) const
49+
{
50+
return nullptr;
51+
};
52+
4353
public:
4454
virtual ~schema() = default;
4555

@@ -92,6 +102,21 @@ class schema_ref : public schema
92102
return default_value_;
93103
}
94104

105+
protected:
106+
virtual std::shared_ptr<schema> make_for_default_(
107+
std::shared_ptr<::schema> &sch,
108+
root_schema *root,
109+
std::vector<nlohmann::json_uri> &uris,
110+
nlohmann::json &default_value) const override
111+
{
112+
// create a new reference schema using the original reference (which will be resolved later)
113+
// to store this overloaded default value #209
114+
auto result = std::make_shared<schema_ref>(uris[0].to_string(), root);
115+
result->set_target(sch, true);
116+
result->set_default_value(default_value);
117+
return result;
118+
};
119+
95120
public:
96121
schema_ref(const std::string &id, root_schema *root)
97122
: schema(root), id_(id) {}
@@ -509,6 +534,18 @@ class type_schema : public schema
509534
}
510535
}
511536

537+
protected:
538+
virtual std::shared_ptr<schema> make_for_default_(
539+
std::shared_ptr<::schema> & /* sch */,
540+
root_schema * /* root */,
541+
std::vector<nlohmann::json_uri> & /* uris */,
542+
nlohmann::json &default_value) const override
543+
{
544+
auto result = std::make_shared<type_schema>(*this);
545+
result->set_default_value(default_value);
546+
return result;
547+
};
548+
512549
public:
513550
type_schema(json &sch,
514551
root_schema *root,
@@ -1289,16 +1326,8 @@ std::shared_ptr<schema> schema::make(json &schema,
12891326
attr = schema.find("default");
12901327
if (attr != schema.end()) {
12911328
// copy the referenced schema depending on the underlying type and modify the default value
1292-
if (dynamic_cast<schema_ref *>(sch.get())) {
1293-
// create a new reference schema use the original reference (which will be resolved later)
1294-
// to store this overloaed default value #209
1295-
auto overloaded_ref_sch = std::make_shared<schema_ref>(uris[0].to_string(), root);
1296-
overloaded_ref_sch->set_target(sch, true);
1297-
overloaded_ref_sch->set_default_value(attr.value());
1298-
sch = overloaded_ref_sch;
1299-
} else if (auto *type_sch = dynamic_cast<type_schema *>(sch.get())) {
1300-
sch = std::make_shared<type_schema>(*type_sch);
1301-
sch->set_default_value(attr.value());
1329+
if (auto new_sch = sch->make_for_default_(sch, root, uris, attr.value())) {
1330+
sch = new_sch;
13021331
}
13031332
schema.erase(attr);
13041333
}

0 commit comments

Comments
 (0)