Some clean-up in `criteria` package

This commit is contained in:
Deluan 2022-10-03 12:25:04 -04:00
parent 12b4a48842
commit bbd3882a75
4 changed files with 14 additions and 16 deletions

View File

@ -6,9 +6,8 @@ import (
"errors"
"strings"
"github.com/navidrome/navidrome/log"
"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/log"
)
type Expression = squirrel.Sqlizer

View File

@ -1,9 +1,7 @@
package criteria
import (
"fmt"
"strings"
"time"
"github.com/navidrome/navidrome/log"
)
@ -62,11 +60,3 @@ func mapFields(expr map[string]interface{}) map[string]interface{} {
}
return m
}
type Time time.Time
func (t Time) MarshalJSON() ([]byte, error) {
//do your serializing here
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02"))
return []byte(stamp), nil
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"strings"
"time"
)
type unmarshalConjunctionType []Expression
@ -115,3 +116,10 @@ func marshalConjunction(name string, conj []Expression) ([]byte, error) {
}
return json.Marshal(aux)
}
type date time.Time
func (t date) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf(`"%s"`, time.Time(t).Format("2006-01-02"))
return []byte(stamp), nil
}

View File

@ -10,8 +10,9 @@ import (
)
var _ = Describe("Operators", func() {
rangeStart := Time(time.Date(2021, 10, 01, 0, 0, 0, 0, time.Local))
rangeEnd := Time(time.Date(2021, 11, 01, 0, 0, 0, 0, time.Local))
rangeStart := date(time.Date(2021, 10, 01, 0, 0, 0, 0, time.Local))
rangeEnd := date(time.Date(2021, 11, 01, 0, 0, 0, 0, time.Local))
DescribeTable("ToSQL",
func(op Expression, expectedSql string, expectedArgs ...any) {
sql, args, err := op.ToSql()
@ -29,7 +30,7 @@ var _ = Describe("Operators", func() {
Entry("startsWith", StartsWith{"title": "Low Rider"}, "media_file.title LIKE ?", "Low Rider%"),
Entry("endsWith", EndsWith{"title": "Low Rider"}, "media_file.title LIKE ?", "%Low Rider"),
Entry("inTheRange [number]", InTheRange{"year": []int{1980, 1990}}, "(media_file.year >= ? AND media_file.year <= ?)", 1980, 1990),
Entry("inTheRange [date]", InTheRange{"lastPlayed": []Time{rangeStart, rangeEnd}}, "(annotation.play_date >= ? AND annotation.play_date <= ?)", rangeStart, rangeEnd),
Entry("inTheRange [date]", InTheRange{"lastPlayed": []date{rangeStart, rangeEnd}}, "(annotation.play_date >= ? AND annotation.play_date <= ?)", rangeStart, rangeEnd),
Entry("before", Before{"lastPlayed": rangeStart}, "annotation.play_date < ?", rangeStart),
Entry("after", After{"lastPlayed": rangeStart}, "annotation.play_date > ?", rangeStart),
// TODO These may be flaky
@ -37,7 +38,7 @@ var _ = Describe("Operators", func() {
Entry("notInTheLast", NotInTheLast{"lastPlayed": 30}, "(annotation.play_date < ? OR annotation.play_date IS NULL)", startOfPeriod(30, time.Now())),
)
DescribeTable("JSON Conversion",
DescribeTable("JSON Marshaling",
func(op Expression, jsonString string) {
obj := And{op}
newJs, err := json.Marshal(obj)