Skip to content

Commit

Permalink
Introduce new DB type: DelimeteredString
Browse files Browse the repository at this point in the history
  • Loading branch information
jpluscplusm committed Nov 11, 2022
1 parent e201dd4 commit bc21caa
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions beets/dbcore/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,29 @@ def normalize(self, value):
else:
return self.model_type(value)

class DelimeteredString(String):

This comment has been minimized.

Copy link
@mkhl

mkhl Dec 11, 2022

for context this is the version i'm using now:

class DelimitedString(String):
    model_type = list

    def __init__(self, delimiter):
        self.delimiter = delimiter

    def format(self, value):
        return self.delimiter.join(value)

    def parse(self, string):
        if not string:
            return []
        return string.split(self.delimiter)

    def to_sql(self, model_value):
        return self.delimiter.join(model_value)

i think 'delimited string' is the correct term here

the rest of the code doesn't seem to use many abbreviations, so calling the property delimiter seems appropriate

i don't think a default value for the delimiter makes a lot of sense?

the list comprehension and string conversions in to_sql don't seem necessary to me, see above. did you run into trouble without them?

the from_sql seems to just be missing functionality from the Type parent and seems completely unnecessary

parse shouldn't be able to fail because at that point, we've ensured that its argument is actually a string.
i also added a if not string condition because otherwise the empty list of album types would turn into the list containing the empty string (because that's what split returns)

if we don't define a format method here we print the internal python representation of the values (i.e. ['album', 'live'] or somesuch) in the beet write output.
joining on the delimiter nicely formats the output :)

just so i don't ping you twice unnecessarily, i think it's better to instantiate this type with '; ' (semicolon space) as the delimiter, because that's what was used before to join the albumtypes and what we'll find in the database

"""A list of Unicode strings, represented in-database by a single string
containing delimiter-separated values.
"""
model_type = list

def __init__(self, delim=','):
self.delim = delim

def to_sql(self, model_value):
return self.delim.join([str(elem) for elem in model_value])

def from_sql(self, sql_value):
if sql_value is None:
return self.null()
else:
return self.parse(sql_value)

def parse(self, string):
try:
return string.split(self.delim)
except:
return self.null

class Boolean(Type):
"""A boolean type.
Expand All @@ -231,3 +254,4 @@ def parse(self, string):
NULL_FLOAT = NullFloat()
STRING = String()
BOOLEAN = Boolean()
SEMICOLON_DSV = DelimeteredString(delim=';')

0 comments on commit bc21caa

Please sign in to comment.