Skip to content

Plugins and flexible field types#947

Closed
geigerzaehler wants to merge 22 commits intobeetbox:masterfrom
geigerzaehler:plugin_field_types
Closed

Plugins and flexible field types#947
geigerzaehler wants to merge 22 commits intobeetbox:masterfrom
geigerzaehler:plugin_field_types

Conversation

@geigerzaehler
Copy link

This is based on #919, so only the commits from 0477420 on are of interest. The PR accomplishes two things.

  1. Plugins can declare types for flexible fields
  2. The new types plugin lets the user configure the types of flexible fields.

This means we can do the following.

$ beet config
...
types:
    rating: int
...
$ beet modify rating=5 artist:Beatles
$ beets ls rating:4..
[lists all the songs by the Beatles]

TODO

  • Write documentation
  • Use the new API in plugins. AFAIK, that’s only the echonest plugin.

Supersedes #445 and resolves #647.

Thomas Scholtes added 22 commits September 11, 2014 22:48
Removing a fixed field sets it to NULL in the database. If the model is
loaded, all NULL values are passed to `Type.normalize()`, so attributes
do not return `None`. Fixes beetbox#886.
We can now set model fields to `None`. `MediaFile` removes the tags
corresponding to `None` fields. Hopefully fixes beetbox#157.
All field operations of a model use the methods of a `Type` instance to
provide custom handling. Field operations include getting, setting,
deleting and conversion to and from SQL.

* There is a one-to-one mapping between SQL values and the values
  stored in the model. Conversion is done with `Type.from_sql()` and
  `Type.to_sql()`. `NULL` values in SQL are directly mapped to `None`
  values in the model
* `Model[field]` retrieves the internal value stored in the model. For
  fixed field this either gives a value or `None`. For flex fields this
  may raise a `KeyError`.
* `Model.get(field, default)` uses `Type.default` to replace missing
  and `None` values
* `Model[field] = value` uses `Type.normalize()` to make sure internal
  values are consisten. Normalization is skipped for `None`.
* `Model.set(field, value)` excpects a string value and uses
  `Type.parse()` to convert and normalize it to an internal value.
This abstraction allows us to generalise a lot of code. It also makes
the the `normalize` method, and by extension assignment of values in
models, more restrictive. I therefore had to fix a few test cases.

For a description of the `model_type` property, see its documentation.
@sampsyo
Copy link
Member

sampsyo commented Sep 13, 2014

Awesome! At long last, this will be great to have. I've rebased the relevant commits in #951.

@geigerzaehler
Copy link
Author

Superseded by #951.

@geigerzaehler geigerzaehler deleted the plugin_field_types branch September 14, 2014 11:22
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

Successfully merging this pull request may close these issues.

Plugin hook for extending dbcore types

2 participants