rttrorg / rttr

C++ Reflection Library
https://www.rttr.org
MIT License
3.18k stars 442 forks source link

Add metadata after registration #197

Open NuckNuck opened 6 years ago

NuckNuck commented 6 years ago

Hi,

is it possible to add additional metadata after a type is registered? This would be great. Here is my use case:

Base registration for Model:

registration::class_<Model>("Model")
    .property("id", &Model::id, &Model::setID) ;

In an other library I want to add some persistence infos. Pseudo code:

auto modelType = rttr::type::get<Model>();
modelType
    .add_metainfo("isPersistent", true)
    .add_metainfo("TableName", "model");
modelType.get_property("id")
    .add_metainfo("isPersistent", true)
    .add_metainfo("ColumnName", "id")
    .add_metainfo("IsPrimaryKey", true);

Currently I add this infos during the initial type registration. But this is not nice ;-)

Greetings Bastian

acki-m commented 6 years ago

I thought about this use case. But didn't came up with a good reason. Why can you not do it in one go, where you register your properties?

NuckNuck commented 6 years ago

Hi, I develop persistence layers with your beautiful RTTR lib. For this I use the reflection info to map objects to tables or json structs. Currently I have to insert persistence metainfos like "ColumnName", "ColumnType",... during the type registration. But I should't known during the type registration, which type of persistence layer will be used.

My workaround is, to register ALL persistence metainfos during the type registration. To separate the different persistence infos a naming convention is used:

...
    .add_metainfo("SQL::ColumnName", "id")
    .add_metainfo("SQL::IsPrimaryKey", true)
    .add_metainfo("JSON::Key", "id");
...

If it would be possible to add metainfos after the registration, my different persistence layer libs can add metainfos from config files at runtime. This would be great.

Thanks for your great and intuitive library, Bastian

acki-m commented 6 years ago

The question is, how do you know where to add the metadata (i.e. to which property?) Do you not repeat yourself again?

The drawback of this feature would be, I would need to make the setter and getter thread safe for the meta data. Because it would be possible to set the data from multiple threads.

It seems to be that you can do what you want (no blocker), but you would like to have some logical separation.

NuckNuck commented 6 years ago

The question is, how do you know where to add the metadata (i.e. to which property?) Do you not repeat yourself again?

My plan is to use an UI to define the persistence metadata. The UI should load all the registered types, properties and provide the possibility to add metadata.

rttr

The UI should store the metadata in an JSON file which my application can load and add the loaded metadata.

{
  "Class 1": {
    "IsPersistent": true,
    "Property 1": {
      "IsPersistent": true,
      "MySQL::ColumnName": "property1",
      "MySQL::Datatype": "Varchar",
      "MySQL::Size": 100
    }
  }
}
rttr::type::get_by_name("Class 1").get_property("Property 1").add_metainfo("MySQL::ColumnName", "property1");

I hope this explains my use case a bit better.

The drawback of this feature would be, I would need to make the setter and getter thread safe for the meta data. Because it would be possible to set the data from multiple threads.

Sure...this is a drawback. But this would be a very powerful feature.