From 2dcdd592d557118d5f70a5cb0994f72c24f91787 Mon Sep 17 00:00:00 2001 From: kekskurse Date: Sat, 11 May 2024 15:28:50 +0200 Subject: [PATCH] chore(ejs) Init --- ejs.go | 130 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ searchGJson.go | 23 +++++++++ 3 files changed, 156 insertions(+) create mode 100644 ejs.go create mode 100644 go.mod create mode 100644 searchGJson.go diff --git a/ejs.go b/ejs.go new file mode 100644 index 0000000..bd366d1 --- /dev/null +++ b/ejs.go @@ -0,0 +1,130 @@ +package ejs + +import ( + "encoding/json" + "fmt" + "os" + "strings" +) + +type Store struct { + path string +} + +type Search interface { + match([]byte) bool +} + +func NewStore(path string) (Store, error) { + s := Store{} + s.path = path + + err := s.createFolderIfNeeded(path) + if err != nil { + return Store{}, err + } + + return s, nil +} + +func (s Store) Save(dataset, id string, data any) error { + err := s.createFolderIfNeeded(fmt.Sprintf("%v/%v", s.path, dataset)) + if err != nil { + return err + } + + jsonData, err := json.Marshal(data) + if err != nil { + return err + } + + os.WriteFile(fmt.Sprintf("%v/%v/%v.json", s.path, dataset, id), jsonData, 0600) + return nil +} + +func (s Store) List(dataset string) ([]string, error) { + entries, err := os.ReadDir(fmt.Sprintf("%v/%v/", s.path, dataset)) + if err != nil { + return []string{}, err + } + + var entrylist []string + for _, e := range entries { + name := e.Name() + name = strings.TrimRight(name, ".json") + entrylist = append(entrylist, name) + } + + return entrylist, nil +} + +func (s Store) Get(dataset, id string, data any) error { + content, err := os.ReadFile(fmt.Sprintf("%v/%v/%v.json", s.path, dataset, id)) + if err != nil { + return err + } + + err = json.Unmarshal(content, data) + if err != nil { + return err + } + + return nil +} + +func (s Store) Scan(dataset string, cbFunc func(id string)) error { + ids, err := s.List(dataset) + if err != nil { + return err + } + for _, id := range ids { + cbFunc(id) + } + return nil +} + +func (s Store) Search(dataset string, searchFilter ...Search) ([]string, error) { + ids, err := s.List(dataset) + if err != nil { + return []string{}, err + } + + found := []string{} + + for _, id := range ids { + content, err := os.ReadFile(fmt.Sprintf("%v/%v/%v.json", s.path, dataset, id)) + if err != nil { + return []string{}, err + } + + match := true + for _, search := range searchFilter { + if !search.match(content) { + match = false + } + } + + if match { + found = append(found, id) + } + } + + return found, nil +} + +func (s Store) createFolderIfNeeded(path string) error { + _, err := os.Stat(path) + if err == nil { + return nil + } + if !os.IsNotExist(err) { + return err + } + + err = os.Mkdir(path, 0700) + if err != nil { + return err + } + + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d0aa868 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.keks.cloud/kekskurse/ejs + +go 1.22.2 diff --git a/searchGJson.go b/searchGJson.go new file mode 100644 index 0000000..1555a70 --- /dev/null +++ b/searchGJson.go @@ -0,0 +1,23 @@ +package ejs + +import ( + "github.com/tidwall/gjson" +) + +type SearchGJsoniString struct { + JsonPath string + CompareMethode string + Value string +} + +func (s SearchGJsoniString) match(data []byte) bool { + value := gjson.Get(string(data), s.JsonPath) + switch s.CompareMethode { + case "=": + if value.String() == s.Value { + return true + } + return false + } + return false +}