Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extensibility in getting/setting values from related record #307

Open
mesemus opened this issue Feb 7, 2023 · 0 comments
Open

Allow extensibility in getting/setting values from related record #307

mesemus opened this issue Feb 7, 2023 · 0 comments

Comments

@mesemus
Copy link

mesemus commented Feb 7, 2023

Is your feature request related to a problem? Please describe.

We want to reference Article from a DataSet, article being a normal record containing "metadata" section. With PIDRelation("metadata.article", keys=["metadata.title"], pid_field=...) the included metadata in the DataSet look like:

{
  "article": {
     "@v": "...",
     "id": "...",
     "metadata": {
        "title": "...", ...
     }
   }
}

In most cases, a more concise representation without the "metadata" section would look better (the same serialization we have for referenced vocabularies):

{
  "article": {
     "@v": "...",
     "id": "...",
     "title": "..."
   }
}

To achieve that, one can extend the RelationResult class and change the _dereference_one method - but that means copy/paste of Invenio code.

Describe the solution you'd like

Change dict_lookup, dict_set functions to methods on RelationResult class. Then in the use case above one would just override them.

Describe alternatives you've considered

An alternative for the usecase above would be to have the "keys" field not a list of fields, but a map from source_path to destination_path. This is not backward compatible and has more limited use than the solution above.

A workaround can be:

class MetadataRelationResultMixin:
    def _dereference_one(self, data, keys, attrs):
        ret = super()._dereference_one(data, keys, attrs)
        if "metadata" in ret:
            ret.update(ret.pop("metadata"))
        return ret

but is looks more like a kind of hack (changing generated representation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant