Remove goyaml2 dependencies

This commit is contained in:
Ming Deng 2021-08-30 22:36:26 +08:00
parent 7ea6609db0
commit 28a6821b32
7 changed files with 115 additions and 85 deletions

View File

@ -60,6 +60,7 @@
- fix bug:reflect.ValueOf(nil) in getFlatParams [4715](https://github.com/beego/beego/pull/4715) - fix bug:reflect.ValueOf(nil) in getFlatParams [4715](https://github.com/beego/beego/pull/4715)
- Fix 4736: set a fixed value "/" to the "Path" of "_xsrf" cookie. [4736](https://github.com/beego/beego/issues/4735) [4739](https://github.com/beego/beego/issues/4739) - Fix 4736: set a fixed value "/" to the "Path" of "_xsrf" cookie. [4736](https://github.com/beego/beego/issues/4735) [4739](https://github.com/beego/beego/issues/4739)
- Fix 4734: do not reset id in Delete function. [4738](https://github.com/beego/beego/pull/4738) [4742](https://github.com/beego/beego/pull/4742) - Fix 4734: do not reset id in Delete function. [4738](https://github.com/beego/beego/pull/4738) [4742](https://github.com/beego/beego/pull/4742)
- Fix 4699: Remove Remove goyaml2 dependency. [4755](https://github.com/beego/beego/pull/4755)
## Fix Sonar ## Fix Sonar

View File

@ -187,11 +187,11 @@ func (c *BaseConfiger) Strings(key string) ([]string, error) {
return strings.Split(res, ";"), nil return strings.Split(res, ";"), nil
} }
func (c *BaseConfiger) Sub(key string) (Configer, error) { func (*BaseConfiger) Sub(string) (Configer, error) {
return nil, errors.New("unsupported operation") return nil, errors.New("unsupported operation")
} }
func (c *BaseConfiger) OnChange(key string, fn func(value string)) { func (*BaseConfiger) OnChange(_ string, _ func(value string)) {
// do nothing // do nothing
} }
@ -249,6 +249,12 @@ func ExpandValueEnvForMap(m map[string]interface{}) map[string]interface{} {
value[k2] = ExpandValueEnv(v2) value[k2] = ExpandValueEnv(v2)
} }
m[k] = value m[k] = value
case map[interface{}]interface{}:
tmp := make(map[string]interface{}, len(value))
for k2, v2 := range value {
tmp[k2.(string)] = v2
}
m[k] = ExpandValueEnvForMap(tmp)
} }
} }
return m return m

View File

@ -13,11 +13,6 @@
// limitations under the License. // limitations under the License.
// Package yaml for config provider // Package yaml for config provider
//
// depend on github.com/beego/goyaml2
//
// go install github.com/beego/goyaml2
//
// Usage: // Usage:
// import( // import(
// _ "github.com/beego/beego/v2/core/config/yaml" // _ "github.com/beego/beego/v2/core/config/yaml"
@ -30,17 +25,13 @@
package yaml package yaml
import ( import (
"bytes"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strings" "strings"
"sync" "sync"
"github.com/beego/goyaml2"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/beego/beego/v2/core/config" "github.com/beego/beego/v2/core/config"
@ -51,7 +42,7 @@ import (
type Config struct{} type Config struct{}
// Parse returns a ConfigContainer with parsed yaml config map. // Parse returns a ConfigContainer with parsed yaml config map.
func (yaml *Config) Parse(filename string) (y config.Configer, err error) { func (*Config) Parse(filename string) (y config.Configer, err error) {
cnf, err := ReadYmlReader(filename) cnf, err := ReadYmlReader(filename)
if err != nil { if err != nil {
return return
@ -63,7 +54,7 @@ func (yaml *Config) Parse(filename string) (y config.Configer, err error) {
} }
// ParseData parse yaml data // ParseData parse yaml data
func (yaml *Config) ParseData(data []byte) (config.Configer, error) { func (*Config) ParseData(data []byte) (config.Configer, error) {
cnf, err := parseYML(data) cnf, err := parseYML(data)
if err != nil { if err != nil {
return nil, err return nil, err
@ -75,7 +66,6 @@ func (yaml *Config) ParseData(data []byte) (config.Configer, error) {
} }
// ReadYmlReader Read yaml file to map. // ReadYmlReader Read yaml file to map.
// if json like, use json package, unless goyaml2 package.
func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
buf, err := ioutil.ReadFile(path) buf, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
@ -86,37 +76,14 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
} }
// parseYML parse yaml formatted []byte to map. // parseYML parse yaml formatted []byte to map.
func parseYML(buf []byte) (cnf map[string]interface{}, err error) { func parseYML(buf []byte) (map[string]interface{}, error) {
if len(buf) < 3 { cnf := make(map[string]interface{})
return err := yaml.Unmarshal(buf, cnf)
}
if string(buf[0:1]) == "{" {
log.Println("Look like a Json, try json umarshal")
err = json.Unmarshal(buf, &cnf)
if err == nil {
log.Println("It is Json Map")
return
}
}
data, err := goyaml2.Read(bytes.NewReader(buf))
if err != nil { if err != nil {
log.Println("Goyaml2 ERR>", string(buf), err) return nil, err
return
}
if data == nil {
log.Println("Goyaml2 output nil? Pls report bug\n" + string(buf))
return
}
cnf, ok := data.(map[string]interface{})
if !ok {
log.Println("Not a Map? >> ", string(buf), data)
cnf = nil
} }
cnf = config.ExpandValueEnvForMap(cnf) cnf = config.ExpandValueEnvForMap(cnf)
return return cnf, err
} }
// ConfigContainer is a config which represents the yaml configuration. // ConfigContainer is a config which represents the yaml configuration.
@ -126,8 +93,8 @@ type ConfigContainer struct {
} }
// Unmarshaler is similar to Sub // Unmarshaler is similar to Sub
func (c *ConfigContainer) Unmarshaler(prefix string, obj interface{}, opt ...config.DecodeOption) error { func (c *ConfigContainer) Unmarshaler(prefix string, obj interface{}, _ ...config.DecodeOption) error {
sub, err := c.sub(prefix) sub, err := c.subMap(prefix)
if err != nil { if err != nil {
return err return err
} }
@ -140,7 +107,7 @@ func (c *ConfigContainer) Unmarshaler(prefix string, obj interface{}, opt ...con
} }
func (c *ConfigContainer) Sub(key string) (config.Configer, error) { func (c *ConfigContainer) Sub(key string) (config.Configer, error) {
sub, err := c.sub(key) sub, err := c.subMap(key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -149,21 +116,19 @@ func (c *ConfigContainer) Sub(key string) (config.Configer, error) {
}, nil }, nil
} }
func (c *ConfigContainer) sub(key string) (map[string]interface{}, error) { func (c *ConfigContainer) subMap(key string) (map[string]interface{}, error) {
tmpData := c.data tmpData := c.data
keys := strings.Split(key, ".") keys := strings.Split(key, ".")
for idx, k := range keys { for idx, k := range keys {
if v, ok := tmpData[k]; ok { if v, ok := tmpData[k]; ok {
switch v.(type) { switch val := v.(type) {
case map[string]interface{}: case map[string]interface{}:
{ tmpData = val
tmpData = v.(map[string]interface{})
if idx == len(keys)-1 { if idx == len(keys)-1 {
return tmpData, nil return tmpData, nil
} }
}
default: default:
return nil, errors.New(fmt.Sprintf("the key is invalid: %s", key)) return nil, fmt.Errorf("the key is invalid: %s", key)
} }
} }
} }
@ -171,7 +136,7 @@ func (c *ConfigContainer) sub(key string) (map[string]interface{}, error) {
return tmpData, nil return tmpData, nil
} }
func (c *ConfigContainer) OnChange(key string, fn func(value string)) { func (*ConfigContainer) OnChange(_ string, _ func(value string)) {
// do nothing // do nothing
logs.Warn("Unsupported operation: OnChange") logs.Warn("Unsupported operation: OnChange")
} }
@ -219,12 +184,18 @@ func (c *ConfigContainer) DefaultInt(key string, defaultVal int) int {
// Int64 returns the int64 value for a given key. // Int64 returns the int64 value for a given key.
func (c *ConfigContainer) Int64(key string) (int64, error) { func (c *ConfigContainer) Int64(key string) (int64, error) {
if v, err := c.getData(key); err != nil { v, err := c.getData(key)
if err != nil {
return 0, err return 0, err
} else if vv, ok := v.(int64); ok {
return vv, nil
} }
return 0, errors.New("not bool value") switch val := v.(type) {
case int:
return int64(val), nil
case int64:
return val, nil
default:
return 0, errors.New("not int or int64 value")
}
} }
// DefaultInt64 returns the int64 value for a given key. // DefaultInt64 returns the int64 value for a given key.
@ -303,7 +274,18 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultVal []string) []stri
// GetSection returns map for the given section // GetSection returns map for the given section
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) { func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok { if v, ok := c.data[section]; ok {
return v.(map[string]string), nil switch val := v.(type) {
case map[string]interface{}:
res := make(map[string]string, len(val))
for k2, v2 := range val {
res[k2] = fmt.Sprintf("%v", v2)
}
return res, nil
case map[string]string:
return val, nil
default:
return nil, fmt.Errorf("unexpected type: %v", v)
}
} }
return nil, errors.New("not exist section") return nil, errors.New("not exist section")
} }
@ -316,7 +298,11 @@ func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
return err return err
} }
defer f.Close() defer f.Close()
err = goyaml2.Write(f, c.data) buf, err := yaml.Marshal(c.data)
if err != nil {
return err
}
_, err = f.Write(buf)
return err return err
} }
@ -334,39 +320,30 @@ func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
} }
func (c *ConfigContainer) getData(key string) (interface{}, error) { func (c *ConfigContainer) getData(key string) (interface{}, error) {
if len(key) == 0 { if key == "" {
return nil, errors.New("key is empty") return nil, errors.New("key is empty")
} }
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()
keys := strings.Split(c.key(key), ".") keys := strings.Split(key, ".")
tmpData := c.data tmpData := c.data
for idx, k := range keys { for idx, k := range keys {
if v, ok := tmpData[k]; ok { if v, ok := tmpData[k]; ok {
switch v.(type) { switch val := v.(type) {
case map[string]interface{}: case map[string]interface{}:
{ tmpData = val
tmpData = v.(map[string]interface{})
if idx == len(keys)-1 { if idx == len(keys)-1 {
return tmpData, nil return tmpData, nil
} }
}
default: default:
{
return v, nil return v, nil
} }
}
} }
} }
return nil, fmt.Errorf("not exist key %q", key) return nil, fmt.Errorf("not exist key %q", key)
} }
func (c *ConfigContainer) key(key string) string {
return key
}
func init() { func init() {
config.Register("yaml", &Config{}) config.Register("yaml", &Config{})
} }

View File

@ -58,6 +58,7 @@ func TestYaml(t *testing.T) {
"emptystrings": []string{}, "emptystrings": []string{},
} }
) )
f, err := os.Create("testyaml.conf") f, err := os.Create("testyaml.conf")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -69,11 +70,28 @@ func TestYaml(t *testing.T) {
} }
f.Close() f.Close()
defer os.Remove("testyaml.conf") defer os.Remove("testyaml.conf")
yamlconf, err := config.NewConfig("yaml", "testyaml.conf") yamlconf, err := config.NewConfig("yaml", "testyaml.conf")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
m, err := ReadYmlReader("testyaml.conf")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, m, yamlconf.(*ConfigContainer).data)
shadow, err := (&Config{}).ParseData([]byte(yamlcontext))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, shadow, yamlconf)
yamlconf.OnChange("abc", func(value string) {
fmt.Printf("on change, value is %s \n", value)
})
res, _ := yamlconf.String("appname") res, _ := yamlconf.String("appname")
if res != "beeapi" { if res != "beeapi" {
t.Fatal("appname not equal to beeapi") t.Fatal("appname not equal to beeapi")
@ -103,7 +121,7 @@ func TestYaml(t *testing.T) {
value, err = yamlconf.DIY(k) value, err = yamlconf.DIY(k)
} }
if err != nil { if err != nil {
t.Errorf("get key %q value fatal,%v err %s", k, v, err) t.Errorf("get key %q value fatal, %v err %s", k, v, err)
} else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) { } else if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) {
t.Errorf("get key %q value, want %v got %v .", k, v, value) t.Errorf("get key %q value, want %v got %v .", k, v, value)
} }
@ -119,7 +137,9 @@ func TestYaml(t *testing.T) {
} }
sub, err := yamlconf.Sub("user") sub, err := yamlconf.Sub("user")
assert.Nil(t, err) if err != nil {
t.Fatal(err)
}
assert.NotNil(t, sub) assert.NotNil(t, sub)
name, err := sub.String("name") name, err := sub.String("name")
assert.Nil(t, err) assert.Nil(t, err)
@ -142,6 +162,38 @@ func TestYaml(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "tom", user.Name) assert.Equal(t, "tom", user.Name)
assert.Equal(t, 13, user.Age) assert.Equal(t, 13, user.Age)
// default value
assert.Equal(t, "beeapi", yamlconf.DefaultString("appname", "invalid"))
assert.Equal(t, "invalid", yamlconf.DefaultString("i-appname", "invalid"))
assert.Equal(t, 8080, yamlconf.DefaultInt("httpport", 8090))
assert.Equal(t, 8090, yamlconf.DefaultInt("i-httpport", 8090))
assert.Equal(t, 3.1415976, yamlconf.DefaultFloat("PI", 3.14))
assert.Equal(t, 3.14, yamlconf.DefaultFloat("1-PI", 3.14))
assert.True(t, yamlconf.DefaultBool("copyrequestbody", false))
assert.True(t, yamlconf.DefaultBool("i-copyrequestbody", true))
assert.Equal(t, int64(8080), yamlconf.DefaultInt64("httpport", 8090))
assert.Equal(t, int64(8090), yamlconf.DefaultInt64("i-httpport", 8090))
assert.Equal(t, "tom", yamlconf.DefaultString("user.name", "invalid"))
assert.Equal(t, "invalid", yamlconf.DefaultString("user.1-name", "invalid"))
assert.Equal(t, []string{"tom"}, yamlconf.DefaultStrings("strings", []string{"tom"}))
appName, err := yamlconf.DIY("appname")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "beeapi", appName)
err = yamlconf.SaveConfigFile(f.Name())
if err != nil {
t.Fatal(err)
}
section, err := yamlconf.GetSection("user")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "tom", section["name"])
} }
type User struct { type User struct {

View File

@ -18,7 +18,7 @@ import (
"sync" "sync"
) )
// deprecated // Deprecated: using sync.Map
type BeeMap struct { type BeeMap struct {
lock *sync.RWMutex lock *sync.RWMutex
bm map[interface{}]interface{} bm map[interface{}]interface{}

2
go.mod
View File

@ -3,7 +3,6 @@ module github.com/beego/beego/v2
go 1.14 go 1.14
require ( require (
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542 github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/casbin/casbin v1.9.1 github.com/casbin/casbin v1.9.1
@ -32,7 +31,6 @@ require (
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
github.com/wendal/errors v0.0.0-20181209125328-7f31f4b264ec // indirect
go.etcd.io/etcd/client/v3 v3.5.0 go.etcd.io/etcd/client/v3 v3.5.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
google.golang.org/grpc v1.38.0 google.golang.org/grpc v1.38.0

4
go.sum
View File

@ -24,8 +24,6 @@ github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6l
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU= github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd h1:jZtX5jh5IOMu0fpOTC3ayh6QGSPJ/KWOv1lgPvbRw1M=
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ=
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542 h1:nYXb+3jF6Oq/j8R/y90XrKpreCxIalBWfeyeKymgOPk= github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542 h1:nYXb+3jF6Oq/j8R/y90XrKpreCxIalBWfeyeKymgOPk=
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU= github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
@ -370,8 +368,6 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1
github.com/ugorji/go v0.0.0-20171122102828-84cb69a8af83/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ= github.com/ugorji/go v0.0.0-20171122102828-84cb69a8af83/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/wendal/errors v0.0.0-20181209125328-7f31f4b264ec h1:bua919NvciYmjqfeZMsVkXTny1QvXMrri0X6NlqILRs=
github.com/wendal/errors v0.0.0-20181209125328-7f31f4b264ec/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=