From 06f860a9f84de03b13c44351b4372a931eecea6d Mon Sep 17 00:00:00 2001 From: Andreas Skorczyk Date: Thu, 18 Mar 2021 19:49:58 +0100 Subject: [PATCH] Make Model.SortByUniqueKey public Sorting slices of Models by uniqueKey is something that is also useful in other packages. --- model/Database.go | 16 ++++++++-------- model/Model.go | 4 ++-- model/Model_test.go | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/model/Database.go b/model/Database.go index 22bb46e..7ca88ca 100644 --- a/model/Database.go +++ b/model/Database.go @@ -104,7 +104,7 @@ 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) @@ -112,22 +112,22 @@ func (db *Database) Equals(other *Database) bool { 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. diff --git a/model/Model.go b/model/Model.go index e5e662e..2513e6f 100644 --- a/model/Model.go +++ b/model/Model.go @@ -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 { diff --git a/model/Model_test.go b/model/Model_test.go index 58b1b55..c7734d1 100644 --- a/model/Model_test.go +++ b/model/Model_test.go @@ -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(¬es) + noteIDChanges := SortByUniqueKey(¬es) assert.Equal(t, expectedNotes, notes) assert.Equal(t, expectedNoteIDChanges, noteIDChanges) }