summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-08-15 11:13:22 +0300
committerPaul Buetow <paul@buetow.org>2024-08-15 11:13:22 +0300
commit921c62587903c9aed342c196089e6af55dabf84f (patch)
treefab44603a36561335c5dd2298f147620982ba2e1 /internal
parentb7b3a4b5baa6bb8055a1fd0695b261e971cffd4d (diff)
return changed boolean var, no need to keep it as a member value
Diffstat (limited to 'internal')
-rw-r--r--internal/server/repository/repository.go5
-rw-r--r--internal/types/entry.go16
-rw-r--r--internal/types/entry_test.go9
3 files changed, 13 insertions, 17 deletions
diff --git a/internal/server/repository/repository.go b/internal/server/repository/repository.go
index 97f7397..e8e2ee1 100644
--- a/internal/server/repository/repository.go
+++ b/internal/server/repository/repository.go
@@ -197,10 +197,11 @@ func (r Repository) Merge(otherEnt types.Entry) error {
}
}
- ent, _ = ent.Update(otherEnt)
+ var changed bool
+ ent, changed, _ = ent.Update(otherEnt)
r.entries[otherEnt.ID] = ent
- if !ent.Changed {
+ if !changed {
// Hasn't changed, so no need to write anything to file.
return nil
}
diff --git a/internal/types/entry.go b/internal/types/entry.go
index fe15270..6ac3533 100644
--- a/internal/types/entry.go
+++ b/internal/types/entry.go
@@ -34,9 +34,6 @@ type Entry struct {
checksum string
checksumDirty bool
mu *sync.Mutex
-
- // To identify whether this entry was changed.
- Changed bool `json:"-"`
}
func NewEntry(bytes []byte) (Entry, error) {
@@ -119,9 +116,9 @@ func (e Entry) Equals(other Entry) bool {
* updated. If entry is missing, it will be added. If entry is there, the shared
* Is status will eventually flip to true but never to false.
*/
-func (e Entry) Update(other Entry) (Entry, error) {
+func (e Entry) Update(other Entry) (Entry, bool, error) {
if e.ID != other.ID {
- return e, fmt.Errorf("can update entry only with other entry with same ID: this(%s) other(%s)", e, other)
+ return e, false, fmt.Errorf("can update entry only with other entry with same ID: this(%s) other(%s)", e, other)
}
var changed bool
@@ -161,10 +158,9 @@ func (e Entry) Update(other Entry) (Entry, error) {
if changed {
e.checksumDirty = true
- e.Changed = true
}
- return e, nil
+ return e, changed, nil
}
func (e Entry) JSONMarshal() ([]byte, error) {
@@ -175,8 +171,10 @@ func (e Entry) String() string {
return e.checksumBase()
}
-// Used to calculate the checksum, better don't change the output, otherwise
-// repository database will get confused with entry checksum mismatches.
+/**
+ * Used to calculate the checksum, better don't change the output, otherwise
+ * repository database will get confused with entry checksum mismatches.
+ */
func (e Entry) checksumBase() string {
var sb strings.Builder
diff --git a/internal/types/entry_test.go b/internal/types/entry_test.go
index 4f26804..a403928 100644
--- a/internal/types/entry_test.go
+++ b/internal/types/entry_test.go
@@ -63,16 +63,13 @@ func TestUpdate(t *testing.T) {
t.Error(err)
}
- if ent1.Changed {
- t.Error("didn't expect the entry to be changed before the update", ent1)
- }
-
- ent1, _ = ent1.Update(ent2)
+ var changed bool
+ ent1, changed, _ = ent1.Update(ent2)
if len(ent1.Shared) != 3 {
t.Error("expected 3 entries after update", ent1)
}
- if !ent1.Changed {
+ if !changed {
t.Error("expected the entry to be changed after update")
}