Skip to content

Commit 99eab96

Browse files
committed
Refactoring to satisfy the Github bot
1 parent 4ff4680 commit 99eab96

File tree

1 file changed

+135
-128
lines changed

1 file changed

+135
-128
lines changed

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 135 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,137 @@ namespace
493493
std::vector<filter_t> filters;
494494
};
495495

496+
template <typename JSON, typename Accessor>
497+
auto parse_filter_by_id(JSON &filter_config, Accessor &&json_accessor)
498+
-> DatasetParams::ByID
499+
{
500+
DatasetParams::ByID byID;
501+
if (!json_accessor(filter_config).contains("id"))
502+
{
503+
throw error::BackendConfigSchema(
504+
{"hdf5", "dataset", "permanent_filters", "id"},
505+
"Required key for selecting a filter by ID.");
506+
}
507+
byID.id = [&]() -> H5Z_filter_t {
508+
auto const &id_config = json_accessor(filter_config["id"]);
509+
using pair_t = std::pair<std::string, H5Z_filter_t>;
510+
std::array<pair_t, 6> filter_types{
511+
pair_t{"deflate", H5Z_FILTER_DEFLATE},
512+
pair_t{"shuffle", H5Z_FILTER_SHUFFLE},
513+
pair_t{"fletcher32", H5Z_FILTER_FLETCHER32},
514+
pair_t{"szip", H5Z_FILTER_SZIP},
515+
pair_t{"nbit", H5Z_FILTER_NBIT},
516+
pair_t{"scaleoffset", H5Z_FILTER_SCALEOFFSET}};
517+
auto id_error = [&]() {
518+
std::stringstream error;
519+
error << "Must be either of unsigned integer type or one of:";
520+
for (auto const &pair : filter_types)
521+
{
522+
error << " '" << pair.first << "'";
523+
}
524+
error << ".";
525+
return error::BackendConfigSchema(
526+
{"hdf5", "dataset", "permanent_filters", "id"},
527+
error.str());
528+
};
529+
if (id_config.is_number_integer())
530+
{
531+
return id_config.template get<H5Z_filter_t>();
532+
}
533+
auto maybe_string = json::asLowerCaseStringDynamic(id_config);
534+
if (!maybe_string.has_value())
535+
{
536+
throw id_error();
537+
}
538+
for (auto const &[key, res_type] : filter_types)
539+
{
540+
if (*maybe_string == key)
541+
{
542+
return res_type;
543+
}
544+
}
545+
throw id_error();
546+
}();
547+
byID.flags = [&]() -> unsigned int {
548+
if (!json_accessor(filter_config).contains("flags"))
549+
{
550+
return 0;
551+
}
552+
auto const &flag_config = json_accessor(filter_config["flags"]);
553+
using pair_t = std::pair<std::string, unsigned int>;
554+
std::array<pair_t, 2> filter_types{
555+
pair_t{"optional", H5Z_FLAG_OPTIONAL},
556+
pair_t{"mandatory", H5Z_FLAG_MANDATORY}};
557+
auto flag_error = [&]() {
558+
std::stringstream error;
559+
error << "Must be either of unsigned integer type or one of:";
560+
for (auto const &pair : filter_types)
561+
{
562+
error << " '" << pair.first << "'";
563+
}
564+
error << ".";
565+
return error::BackendConfigSchema(
566+
{"hdf5", "dataset", "permanent_filters", "flags"},
567+
error.str());
568+
};
569+
if (flag_config.is_number_integer())
570+
{
571+
return flag_config.template get<unsigned int>();
572+
}
573+
auto maybe_string = json::asLowerCaseStringDynamic(flag_config);
574+
if (!maybe_string.has_value())
575+
{
576+
throw flag_error();
577+
}
578+
for (auto const &[key, res_type] : filter_types)
579+
{
580+
if (*maybe_string == key)
581+
{
582+
return res_type;
583+
}
584+
}
585+
throw flag_error();
586+
}();
587+
if (json_accessor(filter_config).contains("c_values"))
588+
{
589+
auto const &c_values_config =
590+
json_accessor(filter_config["c_values"]);
591+
try
592+
{
593+
594+
byID.c_values =
595+
c_values_config.template get<std::vector<unsigned int>>();
596+
}
597+
catch (nlohmann::json::type_error const &)
598+
{
599+
throw error::BackendConfigSchema(
600+
{"hdf5", "dataset", "permanent_filters", "c_values"},
601+
"Must be an array of unsigned integers.");
602+
}
603+
}
604+
return byID;
605+
}
606+
607+
template <typename JSON, typename Accessor>
608+
auto parse_filter_zlib(JSON &filter_config, Accessor &&json_accessor)
609+
-> DatasetParams::Zlib
610+
{
611+
DatasetParams::Zlib zlib;
612+
if (json_accessor(filter_config).contains("aggression"))
613+
{
614+
auto const &aggression_config =
615+
json_accessor(filter_config["aggression"]);
616+
if (!aggression_config.is_number_integer())
617+
{
618+
throw error::BackendConfigSchema(
619+
{"hdf5", "dataset", "permanent_filters", "aggression"},
620+
"Must be of unsigned integer type.");
621+
}
622+
zlib.aggression = aggression_config.template get<unsigned>();
623+
}
624+
return zlib;
625+
}
626+
496627
template <typename JSON, typename Accessor>
497628
auto parse_filter(JSON &filter_config, Accessor &&json_accessor)
498629
-> DatasetParams::filter_t
@@ -554,134 +685,10 @@ namespace
554685

555686
switch (type)
556687
{
557-
case filter_type::ByID: {
558-
DatasetParams::ByID byID;
559-
if (!json_accessor(filter_config).contains("id"))
560-
{
561-
throw error::BackendConfigSchema(
562-
{"hdf5", "dataset", "permanent_filters", "id"},
563-
"Required key for selecting a filter by ID.");
564-
}
565-
byID.id = [&]() -> H5Z_filter_t {
566-
auto const &id_config = json_accessor(filter_config["id"]);
567-
using pair_t = std::pair<std::string, H5Z_filter_t>;
568-
std::array<pair_t, 6> filter_types{
569-
pair_t{"deflate", H5Z_FILTER_DEFLATE},
570-
pair_t{"shuffle", H5Z_FILTER_SHUFFLE},
571-
pair_t{"fletcher32", H5Z_FILTER_FLETCHER32},
572-
pair_t{"szip", H5Z_FILTER_SZIP},
573-
pair_t{"nbit", H5Z_FILTER_NBIT},
574-
pair_t{"scaleoffset", H5Z_FILTER_SCALEOFFSET}};
575-
auto id_error = [&]() {
576-
std::stringstream error;
577-
error
578-
<< "Must be either of unsigned integer type or one of:";
579-
for (auto const &pair : filter_types)
580-
{
581-
error << " '" << pair.first << "'";
582-
}
583-
error << ".";
584-
return error::BackendConfigSchema(
585-
{"hdf5", "dataset", "permanent_filters", "id"},
586-
error.str());
587-
};
588-
if (id_config.is_number_integer())
589-
{
590-
return id_config.template get<H5Z_filter_t>();
591-
}
592-
auto maybe_string = json::asLowerCaseStringDynamic(id_config);
593-
if (!maybe_string.has_value())
594-
{
595-
throw id_error();
596-
}
597-
for (auto const &[key, res_type] : filter_types)
598-
{
599-
if (*maybe_string == key)
600-
{
601-
return res_type;
602-
}
603-
}
604-
throw id_error();
605-
}();
606-
byID.flags = [&]() -> unsigned int {
607-
if (!json_accessor(filter_config).contains("flags"))
608-
{
609-
return 0;
610-
}
611-
auto const &flag_config = json_accessor(filter_config["flags"]);
612-
using pair_t = std::pair<std::string, unsigned int>;
613-
std::array<pair_t, 2> filter_types{
614-
pair_t{"optional", H5Z_FLAG_OPTIONAL},
615-
pair_t{"mandatory", H5Z_FLAG_MANDATORY}};
616-
auto flag_error = [&]() {
617-
std::stringstream error;
618-
error
619-
<< "Must be either of unsigned integer type or one of:";
620-
for (auto const &pair : filter_types)
621-
{
622-
error << " '" << pair.first << "'";
623-
}
624-
error << ".";
625-
return error::BackendConfigSchema(
626-
{"hdf5", "dataset", "permanent_filters", "flags"},
627-
error.str());
628-
};
629-
if (flag_config.is_number_integer())
630-
{
631-
return flag_config.template get<unsigned int>();
632-
}
633-
auto maybe_string = json::asLowerCaseStringDynamic(flag_config);
634-
if (!maybe_string.has_value())
635-
{
636-
throw flag_error();
637-
}
638-
for (auto const &[key, res_type] : filter_types)
639-
{
640-
if (*maybe_string == key)
641-
{
642-
return res_type;
643-
}
644-
}
645-
throw flag_error();
646-
}();
647-
if (json_accessor(filter_config).contains("c_values"))
648-
{
649-
auto const &c_values_config =
650-
json_accessor(filter_config["c_values"]);
651-
try
652-
{
653-
654-
byID.c_values =
655-
c_values_config
656-
.template get<std::vector<unsigned int>>();
657-
}
658-
catch (nlohmann::json::type_error const &)
659-
{
660-
throw error::BackendConfigSchema(
661-
{"hdf5", "dataset", "permanent_filters", "c_values"},
662-
"Must be an array of unsigned integers.");
663-
}
664-
}
665-
return byID;
666-
}
667-
break;
668-
case filter_type::Zlib: {
669-
DatasetParams::Zlib zlib;
670-
if (json_accessor(filter_config).contains("aggression"))
671-
{
672-
auto const &aggression_config =
673-
json_accessor(filter_config["aggression"]);
674-
if (!aggression_config.is_number_integer())
675-
{
676-
throw error::BackendConfigSchema(
677-
{"hdf5", "dataset", "permanent_filters", "aggression"},
678-
"Must be of unsigned integer type.");
679-
}
680-
zlib.aggression = aggression_config.template get<unsigned>();
681-
}
682-
return zlib;
683-
}
684-
break;
688+
case filter_type::ByID:
689+
return parse_filter_by_id(filter_config, json_accessor);
690+
case filter_type::Zlib:
691+
return parse_filter_zlib(filter_config, json_accessor);
685692
}
686693
throw std::runtime_error("Unreachable!");
687694
}

0 commit comments

Comments
 (0)