Deprecated BeeMap and replace all usage with

This commit is contained in:
CarolineZhang666 2021-05-18 23:11:30 +08:00
parent 2dc923eb46
commit d356848ffc
4 changed files with 16 additions and 17 deletions

1
.gitignore vendored
View File

@ -10,5 +10,6 @@ _beeTmp2/
pkg/_beeTmp/
pkg/_beeTmp2/
test/tmp/
core/config/env/pkg/
profile.out

View File

@ -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)

View File

@ -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
}

View File

@ -18,7 +18,8 @@ import (
"sync"
)
// BeeMap is a map with lock
//deprecated
type BeeMap struct {
lock *sync.RWMutex
bm map[interface{}]interface{}