Skip to content

Using Field in combination with validation #475

@ansible42

Description

@ansible42

Working with structs that will become config files down the line. Trying to generate schema as well as have a bit of custom json formatting.

Structs

    using SimpleNameValidator =
      rfl::Validator <std::string,
                        rfl::AllOf<
                         rfl::PatternValidator<R"(^[A-Za-z1-9_]+$)", "TagName">>,
                         rfl::Size<rfl::Minimum<3>>,
                         rfl::Size<rfl::Maximum<50>>>;
    /**
     * @struct TagConfig
     * @brief Configuration details for a tag.
     */
    struct TagConfig
    {
        SimpleNameValidator Name;
        int32_t Length;
        TagDatatype DataType;
        bool WriteEnabled;
    };

    struct ThreadConfig
    {
        rfl::Field<"type", SimpleNameValidator> Type;
        rfl::Field<"Name", SimpleNameValidator> Name;
        rfl::Field<"ReadTags", std::vector<TagConfig>> ReadTags;
        rfl::Field<"WriteTags", std::vector<TagConfig>> WriteTags;
        rfl::Field<"ThreadPriority", int32_t> ThreadPriority;
        rfl::Field<"DecimationFactor", int32_t> DecimationFactor;
        rfl::Field<"TimingOffset", int32_t> TimingOffset;
    };

But when I go to test it at the line marked //problem here I get a bunch of type mismatch warnings. Almost like the pre and post serialization are different types.

invalid operands to binary expression ('ExprLhs<rfl::Field<internal::StringLiteral<5>{{"Name"}}, rfl::Validator<std::basic_string<char, std::char_traits, std::allocator>, rfl::AllOf<rfl::PatternValidator<internal::StringLiteral<16>{{"^[A-Za-z1-9_]+$"}}, internal::StringLiteral<8>{{"TagName"}}>>, rfl::Size<rfl::Minimum<3>>, rfl::Size<rfl::Maximum<50>>>> &>' and 'rfl::Field<"Name", SimpleNameValidator>' (aka 'Field<"Name", Validator<basic_string, AllOf<rfl::PatternValidator<"^[A-Za-z1-9_]+$", "TagName">>, Size<rfl::Minimum<3>>, Size<rfl::Maximum<50>>>>')) [clang-diagnostic-error]

TEST_CASE("Thread Configs Serialize Basic", "[Serialize]")
{
        nets::config::TagConfig read_tag1 = {
      .Name = "READ_TAG_1",
      .Length = 5,
      .DataType = blue::nets::config::TagDatatype::INT32,
      .WriteEnabled = false
    };

    nets::config::TagConfig write_tag1 = {
      .Name = "WRITE_TAG_1",
      .Length = 10,
      .DataType = blue::nets::config::TagDatatype::INT32,
      .WriteEnabled = true
    };
    nets::config::ThreadConfig test_thread_config = {
        .Type = "BasicThread",
        .Name = "TEST_1",
        .ReadTags = std::vector<blue::nets::config::TagConfig> {read_tag1},
        .WriteTags = std::vector<blue::nets::config::TagConfig> {write_tag1},
        .ThreadPriority = -1,
        .DecimationFactor = 10,
        .TimingOffset = 0
    };
    auto json_string = rfl::json::write(test_thread_config);

    std::cout << "Serialized JSON :: " << json_string << std::endl;
    auto deserialized_thread_config = rfl::json::read<nets::config::ThreadConfig>(json_string).value();
    REQUIRE(test_thread_config.Name == deserialized_thread_config.Name)); //Problem here 
    REQUIRE(test_thread_config.Type == deserialized_thread_config.Type);
    REQUIRE(test_thread_config.DecimationFactor == deserialized_thread_config.DecimationFactor);
    REQUIRE(test_thread_config.ThreadPriority == deserialized_thread_config.ThreadPriority);
    REQUIRE(test_thread_config.TimingOffset == deserialized_thread_config.TimingOffset);

    // Check ReadTags
    REQUIRE(test_thread_config.ReadTags.size() == deserialized_thread_config.ReadTags.size());
    REQUIRE(test_thread_config.ReadTags[0].Name == deserialized_thread_config.ReadTags[0].Name);
    REQUIRE(test_thread_config.ReadTags[0].Length == deserialized_thread_config.ReadTags[0].Length);
    REQUIRE(test_thread_config.ReadTags[0].DataType == deserialized_thread_config.ReadTags[0].DataType);
    REQUIRE(test_thread_config.ReadTags[0].WriteEnabled == deserialized_thread_config.ReadTags[0].WriteEnabled);

    // Check WriteTags
    REQUIRE(test_thread_config.WriteTags.size() == deserialized_thread_config.WriteTags.size());
    REQUIRE(test_thread_config.WriteTags[0].Name == deserialized_thread_config.WriteTags[0].Name);
    REQUIRE(test_thread_config.WriteTags[0].Length == deserialized_thread_config.WriteTags[0].Length);
    REQUIRE(test_thread_config.WriteTags[0].DataType == deserialized_thread_config.WriteTags[0].DataType);
    REQUIRE(test_thread_config.WriteTags[0].WriteEnabled == deserialized_thread_config.WriteTags[0].WriteEnabled);
    std::cout << std::endl;
}

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