refactor: rename concurrent to maps

This commit is contained in:
Jan De Dobbeleer 2024-07-03 10:49:27 +02:00 committed by Jan De Dobbeleer
parent 822e2b5b48
commit df4a81e2f6
9 changed files with 70 additions and 67 deletions

View file

@ -1,9 +1,11 @@
package cache package cache
import "github.com/jandedobbeleer/oh-my-posh/src/concurrent" import (
"github.com/jandedobbeleer/oh-my-posh/src/maps"
)
type Command struct { type Command struct {
Commands *concurrent.Map Commands *maps.Concurrent
} }
func (c *Command) Set(command, path string) { func (c *Command) Set(command, path string) {

8
src/cache/file.go vendored
View file

@ -6,7 +6,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/jandedobbeleer/oh-my-posh/src/concurrent" "github.com/jandedobbeleer/oh-my-posh/src/maps"
) )
const ( const (
@ -14,13 +14,13 @@ const (
) )
type File struct { type File struct {
cache *concurrent.Map cache *maps.Concurrent
cachePath string cachePath string
dirty bool dirty bool
} }
func (fc *File) Init(cachePath string) { func (fc *File) Init(cachePath string) {
fc.cache = concurrent.NewMap() fc.cache = maps.NewConcurrent()
fc.cachePath = cachePath fc.cachePath = cachePath
cacheFilePath := filepath.Join(fc.cachePath, CacheFile) cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
content, err := os.ReadFile(cacheFilePath) content, err := os.ReadFile(cacheFilePath)
@ -47,7 +47,7 @@ func (fc *File) Close() {
return return
} }
cache := fc.cache.ToSimpleMap() cache := fc.cache.ToSimple()
if dump, err := json.MarshalIndent(cache, "", " "); err == nil { if dump, err := json.MarshalIndent(cache, "", " "); err == nil {
cacheFilePath := filepath.Join(fc.cachePath, CacheFile) cacheFilePath := filepath.Join(fc.cachePath, CacheFile)

View file

@ -3,7 +3,7 @@ package cache
import ( import (
"sync" "sync"
"github.com/jandedobbeleer/oh-my-posh/src/concurrent" "github.com/jandedobbeleer/oh-my-posh/src/maps"
) )
type Template struct { type Template struct {
@ -17,13 +17,13 @@ type Template struct {
HostName string HostName string
Code int Code int
Env map[string]string Env map[string]string
Var concurrent.SimpleMap Var maps.Simple
OS string OS string
WSL bool WSL bool
PromptCount int PromptCount int
SHLVL int SHLVL int
Segments *concurrent.Map Segments *maps.Concurrent
SegmentsCache concurrent.SimpleMap SegmentsCache maps.Simple
Initialized bool Initialized bool
sync.RWMutex sync.RWMutex

View file

@ -1,46 +0,0 @@
package concurrent
import "sync"
func NewMap() *Map {
var cm Map
return &cm
}
type Map sync.Map
func (cm *Map) Set(key string, value any) {
(*sync.Map)(cm).Store(key, value)
}
func (cm *Map) Get(key string) (any, bool) {
return (*sync.Map)(cm).Load(key)
}
func (cm *Map) Delete(key string) {
(*sync.Map)(cm).Delete(key)
}
func (cm *Map) Contains(key string) bool {
_, ok := (*sync.Map)(cm).Load(key)
return ok
}
func (cm *Map) ToSimpleMap() SimpleMap {
list := make(map[string]any)
(*sync.Map)(cm).Range(func(key, value any) bool {
list[key.(string)] = value
return true
})
return list
}
type SimpleMap map[string]any
func (m SimpleMap) ConcurrentMap() *Map {
var cm Map
for k, v := range m {
cm.Set(k, v)
}
return &cm
}

36
src/maps/concurrent.go Normal file
View file

@ -0,0 +1,36 @@
package maps
import "sync"
func NewConcurrent() *Concurrent {
var cm Concurrent
return &cm
}
type Concurrent sync.Map
func (cm *Concurrent) Set(key string, value any) {
(*sync.Map)(cm).Store(key, value)
}
func (cm *Concurrent) Get(key string) (any, bool) {
return (*sync.Map)(cm).Load(key)
}
func (cm *Concurrent) Delete(key string) {
(*sync.Map)(cm).Delete(key)
}
func (cm *Concurrent) Contains(key string) bool {
_, ok := (*sync.Map)(cm).Load(key)
return ok
}
func (cm *Concurrent) ToSimple() Simple {
list := make(map[string]any)
(*sync.Map)(cm).Range(func(key, value any) bool {
list[key.(string)] = value
return true
})
return list
}

11
src/maps/simple.go Normal file
View file

@ -0,0 +1,11 @@
package maps
type Simple map[string]any
func (m Simple) ToConcurrent() *Concurrent {
var cm Concurrent
for k, v := range m {
cm.Set(k, v)
}
return &cm
}

View file

@ -18,8 +18,8 @@ import (
"time" "time"
"github.com/jandedobbeleer/oh-my-posh/src/cache" "github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/concurrent"
"github.com/jandedobbeleer/oh-my-posh/src/log" "github.com/jandedobbeleer/oh-my-posh/src/log"
"github.com/jandedobbeleer/oh-my-posh/src/maps"
"github.com/jandedobbeleer/oh-my-posh/src/regex" "github.com/jandedobbeleer/oh-my-posh/src/regex"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/battery" "github.com/jandedobbeleer/oh-my-posh/src/runtime/battery"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/cmd" "github.com/jandedobbeleer/oh-my-posh/src/runtime/cmd"
@ -196,7 +196,7 @@ type Environment interface {
type Terminal struct { type Terminal struct {
CmdFlags *Flags CmdFlags *Flags
Var concurrent.SimpleMap Var maps.Simple
cwd string cwd string
host string host string
@ -207,7 +207,7 @@ type Terminal struct {
sync.RWMutex sync.RWMutex
lsDirMap concurrent.Map lsDirMap maps.Concurrent
} }
func (term *Terminal) Init() { func (term *Terminal) Init() {
@ -228,7 +228,7 @@ func (term *Terminal) Init() {
term.fileCache.Init(term.CachePath()) term.fileCache.Init(term.CachePath())
term.resolveConfigPath() term.resolveConfigPath()
term.cmdCache = &cache.Command{ term.cmdCache = &cache.Command{
Commands: concurrent.NewMap(), Commands: maps.NewConcurrent(),
} }
term.tmplCache = &cache.Template{} term.tmplCache = &cache.Template{}
@ -710,7 +710,7 @@ func (term *Terminal) saveTemplateCache() {
} }
tmplCache := term.TemplateCache() tmplCache := term.TemplateCache()
tmplCache.SegmentsCache = tmplCache.Segments.ToSimpleMap() tmplCache.SegmentsCache = tmplCache.Segments.ToSimple()
templateCache, err := json.Marshal(tmplCache) templateCache, err := json.Marshal(tmplCache)
if err == nil { if err == nil {
@ -740,7 +740,7 @@ func (term *Terminal) LoadTemplateCache() {
return return
} }
tmplCache.Segments = tmplCache.SegmentsCache.ConcurrentMap() tmplCache.Segments = tmplCache.SegmentsCache.ToConcurrent()
tmplCache.Initialized = true tmplCache.Initialized = true
term.tmplCache = &tmplCache term.tmplCache = &tmplCache
@ -765,7 +765,7 @@ func (term *Terminal) TemplateCache() *cache.Template {
tmplCache.ShellVersion = term.CmdFlags.ShellVersion tmplCache.ShellVersion = term.CmdFlags.ShellVersion
tmplCache.Code, _ = term.StatusCodes() tmplCache.Code, _ = term.StatusCodes()
tmplCache.WSL = term.IsWsl() tmplCache.WSL = term.IsWsl()
tmplCache.Segments = concurrent.NewMap() tmplCache.Segments = maps.NewConcurrent()
tmplCache.PromptCount = term.CmdFlags.PromptCount tmplCache.PromptCount = term.CmdFlags.PromptCount
tmplCache.Env = make(map[string]string) tmplCache.Env = make(map[string]string)
tmplCache.Var = make(map[string]any) tmplCache.Var = make(map[string]any)

View file

@ -179,7 +179,7 @@ func (t *Text) cleanTemplate() {
// as we can't provide a clean way to access the list // as we can't provide a clean way to access the list
// of segments, we need to replace the property with // of segments, we need to replace the property with
// the list of segments so they can be accessed directly // the list of segments so they can be accessed directly
property = strings.Replace(property, ".Segments", ".Segments.SimpleMap", 1) property = strings.Replace(property, ".Segments", ".Segments.ToSimple", 1)
result += property result += property
} else { } else {
// check if we have the same property in Data // check if we have the same property in Data

View file

@ -4,7 +4,7 @@ import (
"testing" "testing"
"github.com/jandedobbeleer/oh-my-posh/src/cache" "github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/concurrent" "github.com/jandedobbeleer/oh-my-posh/src/maps"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -330,7 +330,7 @@ func TestCleanTemplate(t *testing.T) {
}, },
{ {
Case: "Replace a direct call to .Segments with .Segments.List", Case: "Replace a direct call to .Segments with .Segments.List",
Expected: `{{.Segments.SimpleMap.Git.Repo}}`, Expected: `{{.Segments.ToSimple.Git.Repo}}`,
Template: `{{.Segments.Git.Repo}}`, Template: `{{.Segments.Git.Repo}}`,
}, },
} }
@ -355,7 +355,7 @@ func TestSegmentContains(t *testing.T) {
} }
env := &mock.Environment{} env := &mock.Environment{}
segments := concurrent.NewMap() segments := maps.NewConcurrent()
segments.Set("Git", "foo") segments.Set("Git", "foo")
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
env.On("TemplateCache").Return(&cache.Template{ env.On("TemplateCache").Return(&cache.Template{