Skip to content

Commit a4149aa

Browse files
committed
Refactoring to satisfy the Github bot
1 parent ce54f47 commit a4149aa

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
@@ -491,6 +491,137 @@ namespace
491491
std::vector<filter_t> filters;
492492
};
493493

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

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

0 commit comments

Comments
 (0)