From 77bddf64c1991acf97c7c8896203bc9872012918 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 19 Jun 2024 11:04:03 -0600 Subject: Rename Paramable to Entity --- pkg/db/client.go | 43 ------------------------------------------- pkg/db/storage.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 43 deletions(-) delete mode 100644 pkg/db/client.go create mode 100644 pkg/db/storage.go (limited to 'pkg/db') diff --git a/pkg/db/client.go b/pkg/db/client.go deleted file mode 100644 index 4f43403..0000000 --- a/pkg/db/client.go +++ /dev/null @@ -1,43 +0,0 @@ -package db - -import ( - "bytes" - "fmt" - "io/ioutil" - "os" - "path/filepath" - - "github.com/xlgmokha/x/pkg/env" - "github.com/xlgmokha/x/pkg/serde" - "github.com/xlgmokha/x/pkg/x" -) - -type Paramable interface { - ToParam() string -} - -type Storage[T Paramable] struct { - dir string -} - -func New[T Paramable](dir string) *Storage[T] { - fullPath := x.Must(filepath.Abs(dir)) - x.Check(os.MkdirAll(fullPath, 0700)) - - return &Storage[T]{ - dir: fullPath, - } -} - -func (db *Storage[T]) Save(item T) error { - w := new(bytes.Buffer) - x.Check(serde.To(w, item, serde.YAML)) - if env.Fetch("DUMP", "") != "" { - fmt.Println(w.String()) - } - return ioutil.WriteFile( - fmt.Sprintf("%v/%v.yaml", db.dir, item.ToParam()), - w.Bytes(), - 0700, - ) -} diff --git a/pkg/db/storage.go b/pkg/db/storage.go new file mode 100644 index 0000000..1c1c428 --- /dev/null +++ b/pkg/db/storage.go @@ -0,0 +1,44 @@ +package db + +import ( + "bytes" + "fmt" + "io/ioutil" + "os" + "path/filepath" + + "github.com/xlgmokha/x/pkg/env" + "github.com/xlgmokha/x/pkg/serde" + "github.com/xlgmokha/x/pkg/x" +) + +type Entity interface { + Identifier() string +} + +type Storage[T Entity] struct { + dir string +} + +func New[T Entity](dir string) *Storage[T] { + fullPath := x.Must(filepath.Abs(dir)) + x.Check(os.MkdirAll(fullPath, 0700)) + + return &Storage[T]{ + dir: fullPath, + } +} + +func (db *Storage[T]) Save(item T) error { + w := new(bytes.Buffer) + x.Check(serde.To(w, item, serde.YAML)) + if env.Fetch("DUMP", "") != "" { + fmt.Println(w.String()) + } + + return ioutil.WriteFile(db.filePathFor(item), w.Bytes(), 0700) +} + +func (db *Storage[T]) filePathFor(item T) string { + return fmt.Sprintf("%v/%v.yaml", db.dir, item.Identifier()) +} -- cgit v1.2.3