Some clean-up in `criteria` package
This commit is contained in:
parent
12b4a48842
commit
bbd3882a75
|
@ -6,9 +6,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/navidrome/navidrome/log"
|
|
||||||
|
|
||||||
"github.com/Masterminds/squirrel"
|
"github.com/Masterminds/squirrel"
|
||||||
|
"github.com/navidrome/navidrome/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Expression = squirrel.Sqlizer
|
type Expression = squirrel.Sqlizer
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
package criteria
|
package criteria
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/navidrome/navidrome/log"
|
"github.com/navidrome/navidrome/log"
|
||||||
)
|
)
|
||||||
|
@ -62,11 +60,3 @@ func mapFields(expr map[string]interface{}) map[string]interface{} {
|
||||||
}
|
}
|
||||||
return m
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type unmarshalConjunctionType []Expression
|
type unmarshalConjunctionType []Expression
|
||||||
|
@ -115,3 +116,10 @@ func marshalConjunction(name string, conj []Expression) ([]byte, error) {
|
||||||
}
|
}
|
||||||
return json.Marshal(aux)
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -10,8 +10,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Operators", func() {
|
var _ = Describe("Operators", func() {
|
||||||
rangeStart := Time(time.Date(2021, 10, 01, 0, 0, 0, 0, time.Local))
|
rangeStart := date(time.Date(2021, 10, 01, 0, 0, 0, 0, time.Local))
|
||||||
rangeEnd := Time(time.Date(2021, 11, 01, 0, 0, 0, 0, time.Local))
|
rangeEnd := date(time.Date(2021, 11, 01, 0, 0, 0, 0, time.Local))
|
||||||
|
|
||||||
DescribeTable("ToSQL",
|
DescribeTable("ToSQL",
|
||||||
func(op Expression, expectedSql string, expectedArgs ...any) {
|
func(op Expression, expectedSql string, expectedArgs ...any) {
|
||||||
sql, args, err := op.ToSql()
|
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("startsWith", StartsWith{"title": "Low Rider"}, "media_file.title LIKE ?", "Low Rider%"),
|
||||||
Entry("endsWith", EndsWith{"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 [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("before", Before{"lastPlayed": rangeStart}, "annotation.play_date < ?", rangeStart),
|
||||||
Entry("after", After{"lastPlayed": rangeStart}, "annotation.play_date > ?", rangeStart),
|
Entry("after", After{"lastPlayed": rangeStart}, "annotation.play_date > ?", rangeStart),
|
||||||
// TODO These may be flaky
|
// 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())),
|
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) {
|
func(op Expression, jsonString string) {
|
||||||
obj := And{op}
|
obj := And{op}
|
||||||
newJs, err := json.Marshal(obj)
|
newJs, err := json.Marshal(obj)
|
||||||
|
|
Loading…
Reference in New Issue