-
-
Notifications
You must be signed in to change notification settings - Fork 803
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maintain saved filters in full export/import (#5465)
* Remove ellipsis from full export button
- Loading branch information
1 parent
41d1b45
commit a18c538
Showing
11 changed files
with
484 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
func loadFile[T any](filePath string) (*T, error) { | ||
var ret T | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
var json = jsoniter.ConfigCompatibleWithStandardLibrary | ||
jsonParser := json.NewDecoder(file) | ||
err = jsonParser.Decode(&ret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ret, nil | ||
} | ||
|
||
func saveFile[T any](filePath string, obj *T) error { | ||
if obj == nil { | ||
return fmt.Errorf("object must not be nil") | ||
} | ||
return marshalToFile(filePath, obj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"github.com/stashapp/stash/pkg/fsutil" | ||
"github.com/stashapp/stash/pkg/models" | ||
) | ||
|
||
type SavedFilter struct { | ||
Mode models.FilterMode `db:"mode" json:"mode"` | ||
Name string `db:"name" json:"name"` | ||
FindFilter *models.FindFilterType `json:"find_filter"` | ||
ObjectFilter map[string]interface{} `json:"object_filter"` | ||
UIOptions map[string]interface{} `json:"ui_options"` | ||
} | ||
|
||
func (s SavedFilter) Filename() string { | ||
ret := fsutil.SanitiseBasename(s.Name + "_" + s.Mode.String()) | ||
return ret + ".json" | ||
} | ||
|
||
func LoadSavedFilterFile(filePath string) (*SavedFilter, error) { | ||
return loadFile[SavedFilter](filePath) | ||
} | ||
|
||
func SaveSavedFilterFile(filePath string, image *SavedFilter) error { | ||
return saveFile[SavedFilter](filePath, image) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package savedfilter | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/stashapp/stash/pkg/models" | ||
"github.com/stashapp/stash/pkg/models/jsonschema" | ||
) | ||
|
||
// ToJSON converts a SavedFilter object into its JSON equivalent. | ||
func ToJSON(ctx context.Context, filter *models.SavedFilter) (*jsonschema.SavedFilter, error) { | ||
return &jsonschema.SavedFilter{ | ||
Name: filter.Name, | ||
Mode: filter.Mode, | ||
FindFilter: filter.FindFilter, | ||
ObjectFilter: filter.ObjectFilter, | ||
UIOptions: filter.UIOptions, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package savedfilter | ||
|
||
import ( | ||
"github.com/stashapp/stash/pkg/models" | ||
"github.com/stashapp/stash/pkg/models/jsonschema" | ||
"github.com/stashapp/stash/pkg/models/mocks" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"testing" | ||
) | ||
|
||
const ( | ||
savedFilterID = 1 | ||
noImageID = 2 | ||
errImageID = 3 | ||
errAliasID = 4 | ||
withParentsID = 5 | ||
errParentsID = 6 | ||
) | ||
|
||
const ( | ||
filterName = "testFilter" | ||
mode = models.FilterModeGalleries | ||
) | ||
|
||
var ( | ||
findFilter = models.FindFilterType{} | ||
objectFilter = make(map[string]interface{}) | ||
uiOptions = make(map[string]interface{}) | ||
) | ||
|
||
func createSavedFilter(id int) models.SavedFilter { | ||
return models.SavedFilter{ | ||
ID: id, | ||
Name: filterName, | ||
Mode: mode, | ||
FindFilter: &findFilter, | ||
ObjectFilter: objectFilter, | ||
UIOptions: uiOptions, | ||
} | ||
} | ||
|
||
func createJSONSavedFilter() *jsonschema.SavedFilter { | ||
return &jsonschema.SavedFilter{ | ||
Name: filterName, | ||
Mode: mode, | ||
FindFilter: &findFilter, | ||
ObjectFilter: objectFilter, | ||
UIOptions: uiOptions, | ||
} | ||
} | ||
|
||
type testScenario struct { | ||
savedFilter models.SavedFilter | ||
expected *jsonschema.SavedFilter | ||
err bool | ||
} | ||
|
||
var scenarios []testScenario | ||
|
||
func initTestTable() { | ||
scenarios = []testScenario{ | ||
{ | ||
createSavedFilter(savedFilterID), | ||
createJSONSavedFilter(), | ||
false, | ||
}, | ||
} | ||
} | ||
|
||
func TestToJSON(t *testing.T) { | ||
initTestTable() | ||
|
||
db := mocks.NewDatabase() | ||
|
||
for i, s := range scenarios { | ||
savedFilter := s.savedFilter | ||
json, err := ToJSON(testCtx, &savedFilter) | ||
|
||
switch { | ||
case !s.err && err != nil: | ||
t.Errorf("[%d] unexpected error: %s", i, err.Error()) | ||
case s.err && err == nil: | ||
t.Errorf("[%d] expected error not returned", i) | ||
default: | ||
assert.Equal(t, s.expected, json, "[%d]", i) | ||
} | ||
} | ||
|
||
db.AssertExpectations(t) | ||
} |
Oops, something went wrong.