Skip to content

[bug] rfl::Processors seems broken #653

@roman-orekhov

Description

@roman-orekhov

Was trying to follow advice from here

#include <iostream>
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <string>

struct Config {
  std::string name  = "DefaultName";
  int         value = 42;
};

using Processors = rfl::Processors<rfl::DefaultIfMissing, rfl::NoExtraFields>;

int main() {
  std::string toml_extra = R"(
        name = "Test"
        value = 100
        unknown_field = "I should break this" 
    )";

  auto res1 = rfl::toml::read<Config, Processors>(toml_extra);
  if (!res1) {
    std::cout << "Test 1 (Extra Fields) caught error: " << res1.error().what() << "\n";
  } else {
    std::cout << "Test 1 failed to catch extra field!\n";
  }

  auto res2 = rfl::toml::read<Config, rfl::DefaultIfMissing, rfl::NoExtraFields>(toml_extra);
  if (!res2) {
    std::cout << "Test 2 (Extra Fields) caught error: " << res2.error().what() << "\n";
  } else {
    std::cout << "Test 2 failed to catch extra field!\n";
  }

  std::string toml_missing = R"(
        name = "OnlyName"
    )";

  auto res3 = rfl::toml::read<Config, Processors>(toml_missing);
  if (!res3) {
    std::cout << "Test 3 (Default) caught error: " << res3.error().what() << "\n";
  } else {
    std::cout << "Test 3 Result: Name=" << res3->name << ", Value=" << res3->value << "\n";
  }

  auto res4 = rfl::toml::read<Config, rfl::DefaultIfMissing, rfl::NoExtraFields>(toml_missing);
  if (!res4) {
    std::cout << "Test 4 (Default) caught error: " << res4.error().what() << "\n";
  } else {
    std::cout << "Test 4 Result: Name=" << res4->name << ", Value=" << res4->value << "\n";
  }

  return 0;
}
Test 1 failed to catch extra field!
Test 2 (Extra Fields) caught error: Value named 'unknown_field' not used. Remove the rfl::NoExtraFields processor or add rfl::ExtraFields to avoid this error message.
Test 3 (Default) caught error: Field named 'value' not found.
Test 4 Result: Name=OnlyName, Value=42

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions