diff --git a/collection/array.go b/collection/array.go deleted file mode 100644 index 454ae3d..0000000 --- a/collection/array.go +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2024 Jelly Terra -// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0 -// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/. - -package collection - -import "sync" - -type Array[E any] struct { - RawArray []E -} - -func (a *Array[E]) Len() uint { return uint(len(a.RawArray)) } - -func (a *Array[E]) Cap() uint { return uint(len(a.RawArray)) } - -func (a *Array[E]) Append(e E) { a.RawArray = append(a.RawArray, e) } - -type ComparableArray[E comparable] struct { - Array[E] -} - -func (a *ComparableArray[E]) Contains(e E) bool { - for _, each := range a.RawArray { - if each == e { - return true - } - } - - return false -} - -type SyncArray[E any] struct { - Array[E] - - lock sync.RWMutex -} - -func (a *SyncArray[E]) Len() uint { - a.lock.RLock() - defer a.lock.RUnlock() - return uint(len(a.RawArray)) -} - -func (a *SyncArray[E]) Cap() uint { - a.lock.RLock() - defer a.lock.RUnlock() - return uint(cap(a.RawArray)) -} - -func (a *SyncArray[E]) Append(e E) { - a.lock.Lock() - defer a.lock.Unlock() - a.RawArray = append(a.RawArray, e) -} - -type ComparableSyncArray[E comparable] struct { - SyncArray[E] -} - -func (a *ComparableSyncArray[E]) Contains(e E) bool { - a.lock.RLock() - defer a.lock.RUnlock() - - for _, each := range a.RawArray { - if each == e { - return true - } - } - - return false -} diff --git a/collection/map.go b/collection/map.go deleted file mode 100644 index 5112685..0000000 --- a/collection/map.go +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2024 Jelly Terra -// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0 -// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/. - -package collection - -import "sync" - -func ValuesOfRawMap[K comparable, V any](m map[K]V) []V { - values := make([]V, len(m)) - i := 0 - for _, v := range m { - values[i] = v - i++ - } - return values -} - -func MergeRawMap[K comparable, V any](set map[K]V, subset map[K]V) { - for k, v := range subset { - set[k] = v - } -} - -type MapTrait[K comparable, V any] interface { - Get(K) (V, bool) - Set(K, V) - Foreach(func(K, V) error) error -} - -type Map[K comparable, V any] struct { - RawMap map[K]V -} - -func (m Map[K, V]) Get(k K) (V, bool) { - v, ok := m.RawMap[k] - return v, ok -} - -func (m Map[K, V]) Set(k K, v V) { - m.RawMap[k] = v -} - -func (m Map[K, V]) Foreach(f func(K, V) error) error { - for k, v := range m.RawMap { - if err := f(k, v); err != nil { - return err - } - } - return nil -} - -func NewMap[K comparable, V any]() Map[K, V] { - return Map[K, V]{ - RawMap: map[K]V{}, - } -} - -type SyncMap[K comparable, V any] struct { - Map[K, V] - lock sync.RWMutex -} - -func (s *SyncMap[K, V]) Set(k K, v V) { - s.lock.Lock() - defer s.lock.Unlock() - - if s.RawMap == nil { - s.RawMap = map[K]V{} - } - - s.RawMap[k] = v -} - -func (s *SyncMap[K, V]) Get(k K) (V, bool) { - s.lock.RLock() - defer s.lock.RUnlock() - v, ok := s.RawMap[k] - return v, ok -} - -func (s *SyncMap[K, V]) Foreach(f func(K, V) error) error { - s.lock.RLock() - defer s.lock.RUnlock() - for k, v := range s.RawMap { - if err := f(k, v); err != nil { - return err - } - } - return nil -} - -type MapList[K comparable, V any] struct { - Maps []MapTrait[K, V] -} - -func (l *MapList[K, V]) Get(k K) (v V, ok bool) { - for i := len(l.Maps); i != 0; i++ { - if v, ok = l.Maps[i-1].Get(k); ok { - return v, ok - } - } - return -} - -func (l *MapList[K, V]) Set(k K, v V) { - l.Maps[len(l.Maps)-1].Set(k, v) -} - -func (l *MapList[K, V]) Append(m MapTrait[K, V]) MapList[K, V] { - return MapList[K, V]{ - Maps: append(l.Maps, m), - } -} - -func (l *MapList[K, V]) MergeAll() map[K]V { - m := map[K]V{} - for _, l := range l.Maps { - _ = l.Foreach(func(k K, v V) error { - m[k] = v - return nil - }) - } - return m -} diff --git a/go.mod b/go.mod index ebd3944..b3902ed 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/fsnotify/fsnotify v1.7.0 github.com/gomarkdown/markdown v0.0.0-20240419095408-642f0ee99ae2 github.com/gorilla/websocket v1.5.3 + github.com/jellyterra/collection-go v0.0.1 ) require ( diff --git a/go.sum b/go.sum index 5be7191..27db17e 100644 --- a/go.sum +++ b/go.sum @@ -47,6 +47,8 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/jellyterra/collection-go v0.0.1 h1:pJhz3YSqSupliQPgeVtGuDTSCZ9qq/Q7V4kDf09fkiI= +github.com/jellyterra/collection-go v0.0.1/go.mod h1:oKzk4H4F2OEoKwNnbVV01P/QacuP7liUNgkmK0YtXj4= github.com/mna/pigeon v1.1.0 h1:EjlvVbkGnNGemf8OrjeJX0nH8orujY/HkJgzJtd7kxc= github.com/mna/pigeon v1.1.0/go.mod h1:rkFeDZ0gc+YbnrXPw0q2RlI0QRuKBBPu67fgYIyGRNg= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= diff --git a/structure/hierarchy.go b/structure/hierarchy.go index e310bc5..f90017f 100644 --- a/structure/hierarchy.go +++ b/structure/hierarchy.go @@ -5,7 +5,7 @@ package structure import ( - "github.com/webpagine/pagine/v2/collection" + "github.com/jellyterra/collection-go" "github.com/webpagine/pagine/v2/util" "github.com/webpagine/pagine/v2/vfs" "maps" @@ -69,9 +69,9 @@ func ExecuteLevels(env *Env, root, dest vfs.DirFS, inherit MetadataSet) (Level, unitManifest UnitManifest metadata MetadataSet - units collection.SyncArray[Unit] - reports collection.SyncArray[LevelReport] - levels collection.SyncArray[Level] + units collection.SyncVector[Unit] + reports collection.SyncVector[LevelReport] + levels collection.SyncVector[Level] ) err := func() error { @@ -104,7 +104,7 @@ func ExecuteLevels(env *Env, root, dest vfs.DirFS, inherit MetadataSet) (Level, } unit.Report.TemplateErrors, unit.Report.Error = unit.Generate(env, root, dest, metadata, unitItem.Define) - units.Append(unit) + units.Push(unit) }() } case os.IsNotExist(err): @@ -132,13 +132,13 @@ func ExecuteLevels(env *Env, root, dest vfs.DirFS, inherit MetadataSet) (Level, level, err := ExecuteLevels(env, sub, dest, metadata) if err != nil { - reports.Append(LevelReport{ + reports.Push(LevelReport{ Level: &level, Err: err, }) return } - levels.Append(level) + levels.Push(level) }() } @@ -153,8 +153,8 @@ func ExecuteLevels(env *Env, root, dest vfs.DirFS, inherit MetadataSet) (Level, return Level{ Root: root, Data: metadata, - Units: units.RawArray, - Levels: levels.RawArray, - Reports: reports.RawArray, + Units: units.It.Raw, + Levels: levels.It.Raw, + Reports: reports.It.Raw, }, nil } diff --git a/structure/unit.go b/structure/unit.go index 611ca0c..9ed41ea 100644 --- a/structure/unit.go +++ b/structure/unit.go @@ -6,7 +6,7 @@ package structure import ( "bytes" - "github.com/webpagine/pagine/v2/collection" + "github.com/jellyterra/collection-go" "github.com/webpagine/pagine/v2/global" "github.com/webpagine/pagine/v2/render" "github.com/webpagine/pagine/v2/vfs" @@ -36,7 +36,7 @@ type Unit struct { } func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define map[string]any) ([]error, error) { - var errors collection.Array[error] + var errors collection.Vector[error] templateName, templateKey := ParseTemplatePair(u.Template) @@ -58,7 +58,7 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define renderFromPath := func(r render.Renderer, pathStr any) string { result, err := render.FromPath(r, root, pathStr.(string)) if err != nil { - errors.Append(err) + errors.Push(err) return "" } return result @@ -88,7 +88,7 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define "apply": func(pathStr any, data any) any { path := filepath.Join(root.Path, pathStr.(string)) if _, ok := appliedTemplates[path]; ok { - errors.Append(&RecursiveInvokeError{Templates: nil}) + errors.Push(&RecursiveInvokeError{Templates: nil}) return nil } appliedTemplates[path] = struct{}{} @@ -96,13 +96,13 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define t, err := template.New(filepath.Base(pathStr.(string))).Funcs(funcMap).ParseFS(root, pathStr.(string)) if err != nil { - errors.Append(err) + errors.Push(err) return "" } b := bytes.NewBuffer(nil) err = t.Execute(b, data) if err != nil { - errors.Append(err) + errors.Push(err) return "" } return b.String() @@ -110,7 +110,7 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define "applyFromEnv": func(nameStr any, data any) any { path := nameStr.(string) if _, ok := appliedTemplates[path]; ok { - errors.Append(&RecursiveInvokeError{Templates: nil}) + errors.Push(&RecursiveInvokeError{Templates: nil}) return nil } appliedTemplates[path] = struct{}{} @@ -118,18 +118,18 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define split := strings.Split(nameStr.(string), ":") if len(split) != 2 { - errors.Append(&TemplateUndefinedError{Name: nameStr.(string)}) + errors.Push(&TemplateUndefinedError{Name: nameStr.(string)}) return nil } t, ok := env.Templates[split[0]] if !ok { - errors.Append(&TemplateUndefinedError{Name: split[0]}) + errors.Push(&TemplateUndefinedError{Name: split[0]}) return nil } buf := bytes.NewBuffer(nil) err := t.ExecuteTemplate(buf, funcMap, split[1], data) if err != nil { - errors.Append(err) + errors.Push(err) return nil } return buf.String() @@ -137,7 +137,7 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define "embed": func(pathStr any) any { b, err := root.ReadFile(pathStr.(string)) if err != nil { - errors.Append(err) + errors.Push(err) return "" } return string(b) @@ -145,7 +145,7 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define "render": func(pathStr any) any { r, ok := render.Renderers[filepath.Ext(pathStr.(string))[1:]] if !ok { - errors.Append(&render.NotFoundError{Path: pathStr.(string)}) + errors.Push(&render.NotFoundError{Path: pathStr.(string)}) return "" } return renderFromPath(r, pathStr) @@ -167,5 +167,5 @@ func (u *Unit) Generate(env *Env, root, dest vfs.DirFS, data MetadataSet, define return nil, err } - return errors.RawArray, nil + return errors.Raw, nil }