Skip to content

Commit

Permalink
Some clean-up in criteria package
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Oct 4, 2022
1 parent 12b4a48 commit bbd3882
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
3 changes: 1 addition & 2 deletions model/criteria/criteria.go
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions model/criteria/fields.go
@@ -1,9 +1,7 @@
package criteria

import (
"fmt"
"strings"
"time"

"github.com/navidrome/navidrome/log"
)
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions model/criteria/json.go
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"strings"
"time"
)

type unmarshalConjunctionType []Expression
Expand Down Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions model/criteria/operators_test.go
Expand Up @@ -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()
Expand All @@ -29,15 +30,15 @@ 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
Entry("inTheLast", InTheLast{"lastPlayed": 30}, "annotation.play_date > ?", 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) {
obj := And{op}
newJs, err := json.Marshal(obj)
Expand Down

0 comments on commit bbd3882

Please sign in to comment.