Skip to content

Feature-Request: Adding rfl::DefaultIfNull to support parsing empty documents or nulled values #796

Backlog

Description

@PapeCoding

Dear reflect-cpp team,
during testing I noticed that reflect-cpp can't parse an empty yaml file into the respective struct, even when parsing with rfl::DefaultIfMissing. Some examples, where I would expect it to apply the default values:

struct Logging
{
        std::string level = "info";
        std::vector<std::string> sinks;
};

struct ServerConfig
{
        std::string host = "localhost";
        int port = 8080;
        std::vector<std::string> tags;
        std::map<std::string, std::string> env;
        Logging logging;
        std::map<std::string, Logging> modules;
        std::optional<int> timeout;
};
  1. Empty file:

    • Error: Could not cast to map!
    • Expected: ServerConfig{}, all defaults.
  2. Everything commented out:

    # host: example.com
    # port: 9000
    • Error: Could not cast to map!
    • Expected: all defaults. This happens whenever someone comments out every setting.
  3. Empty list:

    host: example.com
    tags:
    • Error: Failed to parse field 'tags': Could not cast to sequence!
    • Expected: tags empty.
  4. Empty map

    host: example.com
    env:
    • Error: Failed to parse field 'env': Could not cast to map!
    • Expected: env empty.
  5. Section with all children commented out

    host: example.com
    logging:
    #  level: debug
    • Error: Failed to parse field 'logging': Could not cast to map!
    • Expected: logging with its defaults (level == "info").
  6. Map entry without settings

    modules:
      network:
      storage:
        level: debug
    • Error: Failed to parse field 'modules': Failed to parse field 'network': Could not cast to map!
    • Expected: modules["network"] is a default Logging

While I totally see that this might not be the desired default-behavior in some cases, it would be a nice feature-flag to have at least for yaml-parsing, but potentially as well for json parsing.

Best regards,
Sebastian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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