mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 10:30:55 +00:00
use BeeMap instead of a regular map
This commit is contained in:
parent
957c0630c0
commit
126dbdae2f
52
config/env/env.go
vendored
52
config/env/env.go
vendored
@ -18,31 +18,25 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
"github.com/astaxie/beego/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var env struct {
|
var env *utils.BeeMap
|
||||||
data map[string]string
|
|
||||||
lock *sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
env.data = make(map[string]string)
|
env = utils.NewBeeMap()
|
||||||
env.lock = &sync.RWMutex{}
|
|
||||||
for _, e := range os.Environ() {
|
for _, e := range os.Environ() {
|
||||||
splits := strings.Split(e, "=")
|
splits := strings.Split(e, "=")
|
||||||
env.data[splits[0]] = os.Getenv(splits[0])
|
env.Set(splits[0], os.Getenv(splits[0]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns a value by key.
|
// Get returns a value by 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 {
|
||||||
env.lock.RLock()
|
if val := env.Get(key); val != nil {
|
||||||
defer env.lock.RUnlock()
|
return val.(string)
|
||||||
|
|
||||||
if val, ok := env.data[key]; ok {
|
|
||||||
return val
|
|
||||||
}
|
}
|
||||||
return defVal
|
return defVal
|
||||||
}
|
}
|
||||||
@ -50,11 +44,8 @@ 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) {
|
||||||
env.lock.RLock()
|
if val := env.Get(key); val != nil {
|
||||||
defer env.lock.RUnlock()
|
return val.(string), nil
|
||||||
|
|
||||||
if val, ok := env.data[key]; ok {
|
|
||||||
return val, nil
|
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("no env variable with %s", key)
|
return "", fmt.Errorf("no env variable with %s", key)
|
||||||
}
|
}
|
||||||
@ -62,28 +53,33 @@ 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.lock.Lock()
|
env.Set(key, value)
|
||||||
defer env.lock.Unlock()
|
|
||||||
env.data[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.
|
||||||
// It returns an error in case the set operation failed.
|
// It returns an error in case the set operation failed.
|
||||||
func MustSet(key string, value string) error {
|
func MustSet(key string, value string) error {
|
||||||
env.lock.Lock()
|
|
||||||
defer env.lock.Unlock()
|
|
||||||
|
|
||||||
err := os.Setenv(key, value)
|
err := os.Setenv(key, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
env.data[key] = value
|
env.Set(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 {
|
||||||
env.lock.RLock()
|
items := env.Items()
|
||||||
defer env.lock.RUnlock()
|
envs := make(map[string]string, env.Count())
|
||||||
return env.data
|
|
||||||
|
for key, val := range items {
|
||||||
|
switch key := key.(type) {
|
||||||
|
case string:
|
||||||
|
switch val := val.(type) {
|
||||||
|
case string:
|
||||||
|
envs[key] = val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return envs
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user