summaryrefslogtreecommitdiff
path: root/internal/types
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-05-11 18:53:16 +0300
committerPaul Buetow <paul@buetow.org>2024-05-11 18:53:16 +0300
commitb97854c8ec2665887121d773ab994a09ca768adb (patch)
treef673318257f0a172f8cf7784feea2633ccd1ac4a /internal/types
parent560b685505a43aa1a1365c1c3f7b3d0733676847 (diff)
also return the checksums for each element of the repository
Diffstat (limited to 'internal/types')
-rw-r--r--internal/types/entry.go71
-rw-r--r--internal/types/entry_test.go22
2 files changed, 83 insertions, 10 deletions
diff --git a/internal/types/entry.go b/internal/types/entry.go
index a274e2c..067d849 100644
--- a/internal/types/entry.go
+++ b/internal/types/entry.go
@@ -4,31 +4,82 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
+ "os"
+ "strings"
+ "sync"
)
+// Tells me whether the entry was shared to the sm platform named Name
type Shared struct {
Name string `json:"id"`
Is bool `json:"is,omitempty"`
}
+func (s Shared) String() string {
+ return fmt.Sprintf("Name:%s\nIs:%v\n", s.Name, s.Is)
+}
+
type Entry struct {
- Body string `json:"body"`
- Shared []Shared `json:"shared,omitempty"`
- Epoch int `json:"epoch,omitempty"`
- ID string `json:"id,omitempty"`
+ 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
}
func NewEntry(bytes []byte) (Entry, error) {
- var entry Entry
- if err := json.Unmarshal(bytes, &entry); err != nil {
- return entry, fmt.Errorf("unable to deserialise payload: %w", err)
+ var ent Entry
+ if err := json.Unmarshal(bytes, &ent); err != nil {
+ return ent, fmt.Errorf("unable to deserialise payload: %w", err)
}
- if entry.ID == "" {
- entry.ID = fmt.Sprintf("%x", sha256.Sum256(bytes))
+ ent.mu = &sync.Mutex{}
+ ent.dirty = true
+ if ent.ID == "" {
+ ent.ID = fmt.Sprintf("%x", sha256.Sum256(bytes))
+ }
+ return ent, nil
+}
+
+func NewEntryFromFile(filePath string) (Entry, error) {
+ bytes, err := os.ReadFile(filePath)
+ if err != err {
+ return Entry{}, err
}
- return entry, nil
+ return NewEntry(bytes)
}
func (e Entry) Serialize() ([]byte, error) {
return json.Marshal(e)
}
+
+func (e Entry) String() string {
+ var sb strings.Builder
+
+ sb.WriteString("ID:")
+ sb.WriteString(e.ID)
+ sb.WriteString("\n")
+ sb.WriteString(fmt.Sprintf("Epoch:%d\n", e.Epoch))
+ for _, shared := range e.Shared {
+ sb.WriteString(shared.String())
+ }
+ sb.WriteString("Body:")
+ sb.WriteString(e.Body)
+ sb.WriteString("\n")
+
+ return sb.String()
+}
+
+func (e *Entry) Checksum() string {
+ e.mu.Lock()
+ defer e.mu.Unlock()
+
+ if !e.dirty {
+ return e.checksum
+ }
+
+ e.checksum = fmt.Sprintf("%x", sha256.Sum256([]byte(e.String())))
+ e.dirty = false
+ return e.checksum
+}
diff --git a/internal/types/entry_test.go b/internal/types/entry_test.go
new file mode 100644
index 0000000..65e74a3
--- /dev/null
+++ b/internal/types/entry_test.go
@@ -0,0 +1,22 @@
+package types
+
+import "testing"
+
+func TestEntryChecksum(t *testing.T) {
+ t.Parallel()
+
+ ent, err := NewEntry([]byte(`{"Body": "Body text here"}`))
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ expected := "8618a63380fe6d365422cae6ef143a88bb6bd78df567fea3822074cc748f52f8"
+ got := ent.Checksum()
+
+ if expected != got {
+ t.Errorf("expected checksum '%s' but got '%s'", expected, got)
+ return
+ }
+ t.Log(ent.Checksum())
+}