Skip to content

Commit

Permalink
Make Model.SortByUniqueKey public
Browse files Browse the repository at this point in the history
Sorting slices of Models by uniqueKey is
something that is also useful in other packages.
  • Loading branch information
AndreasSko committed Mar 19, 2021
1 parent 999d306 commit 06f860a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions model/Database.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,30 @@ func (db *Database) Equals(other *Database) bool {

// Sort all tables by UniqueKey and update IDs in other tables
for _, db := range []*Database{dbCp, otherCp} {
locIDChanges := sortByUniqueKey(&db.Location)
locIDChanges := SortByUniqueKey(&db.Location)
UpdateIDs(db.Bookmark, "LocationID", locIDChanges)
UpdateIDs(db.Bookmark, "PublicationLocationID", locIDChanges)
UpdateIDs(db.InputField, "LocationID", locIDChanges)
UpdateIDs(db.Note, "LocationID", locIDChanges)
UpdateIDs(db.TagMap, "LocationID", locIDChanges)
UpdateIDs(db.UserMark, "LocationID", locIDChanges)

sortByUniqueKey(&db.Bookmark)
sortByUniqueKey(&db.InputField)
SortByUniqueKey(&db.Bookmark)
SortByUniqueKey(&db.InputField)

tagIDChanges := sortByUniqueKey(&db.Tag)
tagIDChanges := SortByUniqueKey(&db.Tag)
UpdateIDs(db.TagMap, "TagID", tagIDChanges)

umIDChanges := sortByUniqueKey(&db.UserMark)
umIDChanges := SortByUniqueKey(&db.UserMark)
UpdateIDs(db.BlockRange, "UserMarkID", umIDChanges)
UpdateIDs(db.Note, "UserMarkID", umIDChanges)

sortByUniqueKey(&db.BlockRange)
SortByUniqueKey(&db.BlockRange)

noteIDChanges := sortByUniqueKey(&db.Note)
noteIDChanges := SortByUniqueKey(&db.Note)
UpdateIDs(db.TagMap, "NoteID", noteIDChanges)

sortByUniqueKey(&db.TagMap)
SortByUniqueKey(&db.TagMap)
}

// Check if all entries are equal.
Expand Down
4 changes: 2 additions & 2 deletions model/Model.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ Loop:
return buf.String()
}

// sortByUniqueKey sorts the given pointer to a slice of Model by UniqueKey,
// SortByUniqueKey sorts the given pointer to a slice of Model by UniqueKey,
// removes unnecessary nil-entries (except at position 0),
// and also updates the IDs accordingly. It tracks these changes
// by a map, for which the key represents the old ID,
// and value represents the new ID.
func sortByUniqueKey(slice interface{}) map[int]int {
func SortByUniqueKey(slice interface{}) map[int]int {
changes := map[int]int{}

if reflect.TypeOf(slice).Kind() != reflect.Ptr || reflect.TypeOf(slice).Elem().Kind() != reflect.Slice {
Expand Down
4 changes: 2 additions & 2 deletions model/Model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ func Test_sortByUniqueKey(t *testing.T) {
3: 1,
}

locIDChanges := sortByUniqueKey(&locations)
locIDChanges := SortByUniqueKey(&locations)
assert.Equal(t, expectedLocations, locations)
assert.Equal(t, expectedLocIDChanges, locIDChanges)

noteIDChanges := sortByUniqueKey(&notes)
noteIDChanges := SortByUniqueKey(&notes)
assert.Equal(t, expectedNotes, notes)
assert.Equal(t, expectedNoteIDChanges, noteIDChanges)
}

0 comments on commit 06f860a

Please sign in to comment.