object-introspection icon indicating copy to clipboard operation
object-introspection copied to clipboard

Support capturing data from tagged unions

Open ajor opened this issue 1 year ago • 1 comments

Example:

  union MyUnion {
    std::vector<int> vec;
    std::unordered_map<std::string, std::string> map;
  };

  union TaggedUnion {
    MyUnion storage;
    uint8_t tag;
  };

We can't capture MyUnion by itself, as we have no way of telling which of vec or map to try to read. However, the original developers would have had the same issue, so often create a wrapper struct to "tag" their union.

If we have some way of telling OI which field represents a tag for which union, then we'll be able to read this data safely.

This will likely take the form of some extra config settings:

  • Option 1: Specify individual structs which represent tagged unions and which field is the tag for which union. e.g.: e.g.

    tagged_unions = [("TaggedUnion", "storage", "tag"), ...]
    

    This will always work, but will require a lot of configuration for codebases with a large number of tagged unioins.

  • Option 2: Large codebases will often have a standard way of creating a tagged union, whether this be through a framework such as Thrift or just coding conventions. If we can codify these conventions then we could capture all tagged unions with a lot of configuration than Option 1. e.g. a Thrift union will have the form:

    struct MyType {
      storage_type value; // The union
      underlying_type_t type; // The tag
    };
    

    Which could be codified as:

    tagged_unions = [("storage_type", "value", "underlying_type_t", "type"), ...]
    

ajor avatar Jul 13 '23 09:07 ajor