diff options
| author | Paul Buetow <paul@buetow.org> | 2024-05-19 13:16:28 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-05-19 13:16:28 +0300 |
| commit | c06422b0727a4c6520d715e4be9a49a612afacfb (patch) | |
| tree | 95565f8ac555f04b87bc2cb4482984d8cc649615 /internal/types | |
| parent | 3e55b77c042c8436f93347f26cdd3196c1cb7f25 (diff) | |
initial equality test for the entries
Diffstat (limited to 'internal/types')
| -rw-r--r-- | internal/types/entry.go | 63 | ||||
| -rw-r--r-- | internal/types/entry_test.go | 6 |
2 files changed, 56 insertions, 13 deletions
diff --git a/internal/types/entry.go b/internal/types/entry.go index b3358b0..8d8339f 100644 --- a/internal/types/entry.go +++ b/internal/types/entry.go @@ -21,14 +21,25 @@ func (s Shared) String() string { return fmt.Sprintf("Name:%s\nIs:%v\n", s.Name, s.Is) } +func (s Shared) Equals(other Shared) bool { + switch { + case s.Name != other.Name: + return false + case s.Is != other.Is: + return false + default: + return true + } +} + type Entry struct { - Body string `json:"body"` - Shared []Shared `json:"shared,omitempty"` - Epoch int `json:"epoch,omitempty"` - ID string `json:"id,omitempty"` - mu *sync.Mutex - dirty bool - checksum string + Body string `json:"body"` + Shared []Shared `json:"shared,omitempty"` + Epoch int `json:"epoch,omitempty"` + ID string `json:"id,omitempty"` + mu *sync.Mutex + checksum string + checksumDirty bool } func NewEntry(bytes []byte) (Entry, error) { @@ -60,12 +71,38 @@ func NewEntryFromFile(filePath string) (Entry, error) { func (e *Entry) initialize() { e.mu = &sync.Mutex{} - e.dirty = true + e.checksumDirty = true } -func (e Entry) Updated(other Entry) Entry { - panic("not yet implemented") - //return e +func (e Entry) Update(other Entry) (Entry, bool) { + panic("not yet impelemented") +} + +func (e Entry) Equals(other Entry) bool { + switch { + case e.Body != other.Body: + return false + case e.Epoch != other.Epoch: + return false + case e.ID != other.ID: + return false + case len(e.Shared) != len(other.Shared): + return false + } + + otherShared := make(map[string]Shared) + for _, shared := range other.Shared { + otherShared[shared.Name] = shared + } + + for _, shared := range e.Shared { + otherShared, ok := otherShared[shared.Name] + if !ok || !shared.Equals(otherShared) { + return false + } + } + + return true } func (e Entry) Serialize() ([]byte, error) { @@ -102,11 +139,11 @@ func (e *Entry) Checksum() string { e.mu.Lock() defer e.mu.Unlock() - if !e.dirty { + if !e.checksumDirty { return e.checksum } e.checksum = fmt.Sprintf("%x", sha256.Sum256([]byte(e.String()))) - e.dirty = false + e.checksumDirty = false return e.checksum } diff --git a/internal/types/entry_test.go b/internal/types/entry_test.go index 65e74a3..b8e2846 100644 --- a/internal/types/entry_test.go +++ b/internal/types/entry_test.go @@ -20,3 +20,9 @@ func TestEntryChecksum(t *testing.T) { } t.Log(ent.Checksum()) } + +func TestEqual(t *testing.T) { + t.Parallel() + + // TODO: Write a unit test here. +} |
