fix: removing unnecessary PolicyBody

This commit is contained in:
Корней Гедерт 2024-08-05 15:45:16 +04:00
parent 111f58104f
commit 7d63273bb1
4 changed files with 10 additions and 24 deletions

View File

@ -48,27 +48,23 @@ PRegParser::PRegParser()
PolicyFile PRegParser::parse(std::istream &stream)
{
PolicyBody body;
PolicyTree instructions;
parseHeader(stream);
stream.peek();
while (!stream.eof()) {
insertInstruction(stream, body.instructions);
insertInstruction(stream, instructions);
stream.peek();
}
return { body };
return { instructions };
}
bool PRegParser::write(std::ostream &stream, const PolicyFile &file)
{
if (!file.body.has_value()) {
return true;
}
writeHeader(stream);
for (const auto &[key, records] : file.body->instructions) {
for (const auto &[key, records] : file.instructions) {
for (const auto &[value, array] : records) {
for (const auto &instruction : array) {
writeInstruction(stream, instruction, key, value);

View File

@ -84,26 +84,18 @@ typedef std::unordered_map<std::string,
std::unordered_map<std::string, std::vector<PolicyInstruction>>>
PolicyTree;
typedef struct PolicyBody
typedef struct PolicyFile
{
inline bool operator==(const PolicyBody &other) const
inline bool operator==(const PolicyFile &other) const
{
return instructions == other.instructions;
}
inline bool operator!=(const PolicyBody &other) const
inline bool operator!=(const PolicyFile &other) const
{
return instructions != other.instructions;
}
PolicyTree instructions{};
} PolicyBody;
typedef struct PolicyFile
{
inline bool operator==(const PolicyFile &other) const { return body == other.body; }
inline bool operator!=(const PolicyFile &other) const { return body != other.body; }
std::optional<PolicyBody> body{};
} PolicyFile;
class PRegParser

View File

@ -240,7 +240,7 @@ bool PolFormat::read(std::istream &input, io::RegistryFile *file)
return false;
}
for (const auto &[key, record] : result.body->instructions) {
for (const auto &[key, record] : result.instructions) {
for (const auto &[value, array] : record) {
for(const auto &entry : array) {
auto registryEntry = RegistryEntryAdapter::create(entry, key, value);
@ -260,10 +260,9 @@ bool PolFormat::write(std::ostream &output, io::RegistryFile *file)
{
auto writer = pol::createPregParser();
auto result = pol::PolicyFile();
result.body = std::make_optional<pol::PolicyBody>();
for (const auto &entry : file->getRegistry()->registryEntries) {
RegistryEntryAdapter::addInstruction(result.body->instructions, entry);
RegistryEntryAdapter::addInstruction(result.instructions, entry);
}
try {

View File

@ -166,13 +166,12 @@ pol::PolicyFile generateCase(size_t seed = -1)
}
// Generate case
data.body = std::make_optional<pol::PolicyBody>();
size_t el = dist(gen);
for (size_t i = 0; i < el; i++) {
pol::PolicyInstruction instruction;
instruction.type = generateRandomType(gen);
instruction.data = generateRandomData(instruction.type, gen);
data.body->instructions[generateRandomKeypath(gen)][generateRandomValue(gen)] = { instruction };
data.instructions[generateRandomKeypath(gen)][generateRandomValue(gen)] = { instruction };
}
return data;