From d356848ffc8f35734769552f01555945acac3ea1 Mon Sep 17 00:00:00 2001 From: CarolineZhang666 Date: Tue, 18 May 2021 23:11:30 +0800 Subject: [PATCH] Deprecated BeeMap and replace all usage with --- .gitignore | 1 + CHANGELOG.md | 2 +- core/config/env/env.go | 27 ++++++++++++--------------- core/utils/safemap.go | 3 ++- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 0306c438..7d98359d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,6 @@ _beeTmp2/ pkg/_beeTmp/ pkg/_beeTmp2/ test/tmp/ +core/config/env/pkg/ profile.out diff --git a/CHANGELOG.md b/CHANGELOG.md index 18b5a8e7..0a8bb518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,7 @@ - Proposal: Add Bind() method for `web.Controller` [4491](https://github.com/beego/beego/issues/4579) - Optimize AddAutoPrefix: only register one router in case-insensitive mode. [4582](https://github.com/beego/beego/pull/4582) - Init exceptMethod by using reflection. [4583](https://github.com/beego/beego/pull/4583) - +- Deprecated BeeMap and replace all usage with `sync.map` [4616](https://github.com/beego/beego/pull/4616) ## Fix Sonar - [4608](https://github.com/beego/beego/pull/4608) - [4473](https://github.com/beego/beego/pull/4473) diff --git a/core/config/env/env.go b/core/config/env/env.go index fbf06c5d..beb91138 100644 --- a/core/config/env/env.go +++ b/core/config/env/env.go @@ -20,24 +20,22 @@ import ( "fmt" "os" "strings" - - "github.com/beego/beego/v2/core/utils" + "sync" ) -var env *utils.BeeMap +var env sync.Map func init() { - env = utils.NewBeeMap() for _, e := range os.Environ() { splits := strings.Split(e, "=") - env.Set(splits[0], os.Getenv(splits[0])) + env.Store(splits[0], os.Getenv(splits[0])) } } // Get returns a value for a given key. // If the key does not exist, the default value will be returned. func Get(key string, defVal string) string { - if val := env.Get(key); val != nil { + if val,ok:= env.Load(key);ok { return val.(string) } return defVal @@ -46,7 +44,7 @@ func Get(key string, defVal string) string { // MustGet returns a value by key. // If the key does not exist, it will return an error. func MustGet(key string) (string, error) { - if val := env.Get(key); val != nil { + if val,ok := env.Load(key); ok{ return val.(string), nil } return "", fmt.Errorf("no env variable with %s", key) @@ -55,7 +53,7 @@ func MustGet(key string) (string, error) { // Set sets a value in the ENV copy. // This does not affect the child process environment. func Set(key string, value string) { - env.Set(key, value) + env.Store(key, value) } // MustSet sets a value in the ENV copy and the child process environment. @@ -65,23 +63,22 @@ func MustSet(key string, value string) error { if err != nil { return err } - env.Set(key, value) + env.Store(key, value) return nil } // GetAll returns all keys/values in the current child process environment. func GetAll() map[string]string { - items := env.Items() - envs := make(map[string]string, env.Count()) - - for key, val := range items { + envs := make(map[string]string, 32) + env.Range(func(key, value interface{}) bool { switch key := key.(type) { case string: - switch val := val.(type) { + switch val := value.(type) { case string: envs[key] = val } } - } + return true + }) return envs } diff --git a/core/utils/safemap.go b/core/utils/safemap.go index 1793030a..05763786 100644 --- a/core/utils/safemap.go +++ b/core/utils/safemap.go @@ -18,7 +18,8 @@ import ( "sync" ) -// BeeMap is a map with lock + +//deprecated type BeeMap struct { lock *sync.RWMutex bm map[interface{}]interface{}