From cbdce79d25206573d1f79c9c647dcaa251b69463 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 30 Sep 2024 10:51:49 +0300 Subject: add entry "class" --- internal/entry/entry.go | 75 ++++++++++++++++++++++++++++++++++++++++++++ internal/entry/entry_test.go | 39 +++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 internal/entry/entry.go create mode 100644 internal/entry/entry_test.go (limited to 'internal/entry') diff --git a/internal/entry/entry.go b/internal/entry/entry.go new file mode 100644 index 0000000..4e837a0 --- /dev/null +++ b/internal/entry/entry.go @@ -0,0 +1,75 @@ +package entry + +import ( + "fmt" + "strings" + "time" + + "codeberg.org/snonux/gos/internal/format" +) + +type State int + +const ( + Unknown State = iota + Queued + Posted +) + +func (s State) String() string { + switch s { + case Unknown: + return "unknown" + case Queued: + return "queued" + case Posted: + return "posted" + default: + panic(fmt.Sprintf("unknown state: %d", int(s))) + } +} + +// The time this code was written a:round, actually. +const oldestValidTime = "20240922-102800" + +type Entry struct { + Path string + Time time.Time + State State +} + +// filePath format: /foo/foobarbaz.something.here.txt.STAMP.{posted,queued} +func New(filePath string) (Entry, error) { + ent := Entry{Path: filePath} + + // We want to get the STAMP! + parts := strings.Split(filePath, ".") + if len(parts) < 4 { + return ent, fmt.Errorf("not a valid entry path: %s", filePath) + } + + switch parts[len(parts)-1] { + case "queued": + ent.State = Queued + case "posted": + ent.State = Posted + default: + return ent, fmt.Errorf("can't parse state from path: %s", filePath) + } + + var err error + if ent.Time, err = time.Parse(format.Time, parts[len(parts)-2]); err != nil { + return ent, err + } + + oldestValid, err := time.Parse(format.Time, oldestValidTime) + if err != nil { + panic(err) + } + + if ent.Time.Before(oldestValid) { + return ent, fmt.Errorf("entry time does not seem legic, it is too old: %v", ent.Time) + } + + return ent, nil +} diff --git a/internal/entry/entry_test.go b/internal/entry/entry_test.go new file mode 100644 index 0000000..5e3a5d3 --- /dev/null +++ b/internal/entry/entry_test.go @@ -0,0 +1,39 @@ +package entry + +import ( + "fmt" + "testing" + "time" + + "codeberg.org/snonux/gos/internal/format" +) + +func TestEntry(t *testing.T) { + states := []State{Queued, Posted} + stamps := []string{"20240928-111835", "20241028-120135"} + + for _, state := range states { + for _, stamp := range stamps { + queuedPath := fmt.Sprintf("gosdir/db/platforms/linkedin/helloworld.txt.%s.%s", stamp, state) + + ent, err := New(queuedPath) + if err != nil { + t.Error(err) + } + if ent.Path != queuedPath { + t.Errorf("expected path %s but got %s", queuedPath, ent.Path) + } + if ent.State != state { + t.Errorf("expected state %s but got %s", state, ent.State) + } + + expectedTime, err := time.Parse(format.Time, stamp) + if err != nil { + t.Error(err) + } + if ent.Time != expectedTime { + t.Errorf("expected time to be %v but got %v", expectedTime, ent.Time) + } + } + } +} -- cgit v1.2.3