summaryrefslogtreecommitdiff
path: root/internal/types
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-05-06 20:32:35 +0300
committerPaul Buetow <paul@buetow.org>2024-05-06 20:32:35 +0300
commit8c2e75df51d1a74d8bca1d3d1a423f777159ba82 (patch)
treeb340984869230f92d0aea54d104efc5618c54954 /internal/types
parentd06809e50aa20b7f2e12ed43374aebb39e77a220 (diff)
refactor project file structure
Diffstat (limited to 'internal/types')
-rw-r--r--internal/types/entry.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/internal/types/entry.go b/internal/types/entry.go
new file mode 100644
index 0000000..a274e2c
--- /dev/null
+++ b/internal/types/entry.go
@@ -0,0 +1,34 @@
+package types
+
+import (
+ "crypto/sha256"
+ "encoding/json"
+ "fmt"
+)
+
+type Shared struct {
+ Name string `json:"id"`
+ Is bool `json:"is,omitempty"`
+}
+
+type Entry struct {
+ Body string `json:"body"`
+ Shared []Shared `json:"shared,omitempty"`
+ Epoch int `json:"epoch,omitempty"`
+ ID string `json:"id,omitempty"`
+}
+
+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)
+ }
+ if entry.ID == "" {
+ entry.ID = fmt.Sprintf("%x", sha256.Sum256(bytes))
+ }
+ return entry, nil
+}
+
+func (e Entry) Serialize() ([]byte, error) {
+ return json.Marshal(e)
+}