Merge pull request #4616 from CarolineZhang666/sync.map
Deprecated BeeMap and replace all usage with
This commit is contained in:
commit
42c0b9a988
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,5 +10,6 @@ _beeTmp2/
|
|||||||
pkg/_beeTmp/
|
pkg/_beeTmp/
|
||||||
pkg/_beeTmp2/
|
pkg/_beeTmp2/
|
||||||
test/tmp/
|
test/tmp/
|
||||||
|
core/config/env/pkg/
|
||||||
|
|
||||||
profile.out
|
profile.out
|
||||||
|
|||||||
@ -40,7 +40,7 @@
|
|||||||
- Proposal: Add Bind() method for `web.Controller` [4491](https://github.com/beego/beego/issues/4579)
|
- 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)
|
- 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)
|
- 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
|
## Fix Sonar
|
||||||
- [4608](https://github.com/beego/beego/pull/4608)
|
- [4608](https://github.com/beego/beego/pull/4608)
|
||||||
- [4473](https://github.com/beego/beego/pull/4473)
|
- [4473](https://github.com/beego/beego/pull/4473)
|
||||||
|
|||||||
27
core/config/env/env.go
vendored
27
core/config/env/env.go
vendored
@ -20,24 +20,22 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"github.com/beego/beego/v2/core/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var env *utils.BeeMap
|
var env sync.Map
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
env = utils.NewBeeMap()
|
|
||||||
for _, e := range os.Environ() {
|
for _, e := range os.Environ() {
|
||||||
splits := strings.Split(e, "=")
|
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.
|
// Get returns a value for a given key.
|
||||||
// If the key does not exist, the default value will be returned.
|
// If the key does not exist, the default value will be returned.
|
||||||
func Get(key string, defVal string) string {
|
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 val.(string)
|
||||||
}
|
}
|
||||||
return defVal
|
return defVal
|
||||||
@ -46,7 +44,7 @@ func Get(key string, defVal string) string {
|
|||||||
// MustGet returns a value by key.
|
// MustGet returns a value by key.
|
||||||
// If the key does not exist, it will return an error.
|
// If the key does not exist, it will return an error.
|
||||||
func MustGet(key string) (string, 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 val.(string), nil
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("no env variable with %s", key)
|
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.
|
// Set sets a value in the ENV copy.
|
||||||
// This does not affect the child process environment.
|
// This does not affect the child process environment.
|
||||||
func Set(key string, value string) {
|
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.
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
env.Set(key, value)
|
env.Store(key, value)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAll returns all keys/values in the current child process environment.
|
// GetAll returns all keys/values in the current child process environment.
|
||||||
func GetAll() map[string]string {
|
func GetAll() map[string]string {
|
||||||
items := env.Items()
|
envs := make(map[string]string, 32)
|
||||||
envs := make(map[string]string, env.Count())
|
env.Range(func(key, value interface{}) bool {
|
||||||
|
|
||||||
for key, val := range items {
|
|
||||||
switch key := key.(type) {
|
switch key := key.(type) {
|
||||||
case string:
|
case string:
|
||||||
switch val := val.(type) {
|
switch val := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
envs[key] = val
|
envs[key] = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return true
|
||||||
|
})
|
||||||
return envs
|
return envs
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BeeMap is a map with lock
|
|
||||||
|
//deprecated
|
||||||
type BeeMap struct {
|
type BeeMap struct {
|
||||||
lock *sync.RWMutex
|
lock *sync.RWMutex
|
||||||
bm map[interface{}]interface{}
|
bm map[interface{}]interface{}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user