Skip to content
Snippets Groups Projects
Commit 50a1609c authored by pantianying's avatar pantianying
Browse files

code optimization

parent 9edd7946
No related branches found
No related tags found
No related merge requests found
......@@ -113,7 +113,7 @@ conditions:
assert.Nil(t, err)
assert.NotNil(t, appRouter)
rule, err := Parse(testYML)
rule, err := getRule(testYML)
assert.Nil(t, err)
appRouter.generateConditions(rule)
......
......@@ -44,7 +44,7 @@ type FileConditionRouter struct {
// NewFileConditionRouter Create file condition router instance with content ( from config file)
func NewFileConditionRouter(content []byte) (*FileConditionRouter, error) {
fileRouter := &FileConditionRouter{}
rule, err := Parse(string(content))
rule, err := getRule(string(content))
if err != nil {
return nil, perrors.Errorf("yaml.Unmarshal() failed , error:%v", perrors.WithStack(err))
}
......
......@@ -102,7 +102,7 @@ func (l *listenableRouter) Process(event *config_center.ConfigChangeEvent) {
return
}
routerRule, err := Parse(content)
routerRule, err := getRule(content)
if err != nil {
logger.Errorf("Parse condition router rule fail,error:[%s] ", err)
return
......
......@@ -27,7 +27,7 @@ import (
)
import (
matcher "github.com/apache/dubbo-go/cluster/router/match"
matcher "github.com/apache/dubbo-go/cluster/router/util"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/logger"
......
......@@ -17,12 +17,9 @@
package condition
import (
"gopkg.in/yaml.v2"
)
import (
"github.com/apache/dubbo-go/cluster/router"
"github.com/apache/dubbo-go/common/yaml"
)
// RouterRule RouterRule config read from config file or config center
......@@ -44,9 +41,9 @@ type RouterRule struct {
* =>
* 1.1.1.1
*/
func Parse(rawRule string) (*RouterRule, error) {
func getRule(rawRule string) (*RouterRule, error) {
r := &RouterRule{}
err := yaml.Unmarshal([]byte(rawRule), r)
err := yaml.UnmarshalYML([]byte(rawRule), r)
if err != nil {
return r, err
}
......
......@@ -24,7 +24,7 @@ import (
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
func TestGetRule(t *testing.T) {
testyml := `
scope: application
runtime: true
......@@ -36,7 +36,7 @@ conditions:
ip=127.0.0.1
=>
1.1.1.1`
rule, e := Parse(testyml)
rule, e := getRule(testyml)
assert.Nil(t, e)
assert.NotNil(t, rule)
......
......@@ -45,7 +45,7 @@ type FileTagRouter struct {
// NewFileTagRouter Create file tag router instance with content ( from config file)
func NewFileTagRouter(content []byte) (*FileTagRouter, error) {
fileRouter := &FileTagRouter{}
rule, err := Parse(string(content))
rule, err := getRule(string(content))
if err != nil {
return nil, perrors.Errorf("yaml.Unmarshal() failed , error:%v", perrors.WithStack(err))
}
......
......@@ -17,12 +17,9 @@
package tag
import (
"gopkg.in/yaml.v2"
)
import (
"github.com/apache/dubbo-go/cluster/router"
"github.com/apache/dubbo-go/common/yaml"
)
// RouterRule RouterRule config read from config file or config center
......@@ -30,9 +27,9 @@ type RouterRule struct {
router.BaseRouterRule `yaml:",inline""`
}
func Parse(rawRule string) (*RouterRule, error) {
func getRule(rawRule string) (*RouterRule, error) {
r := &RouterRule{}
err := yaml.Unmarshal([]byte(rawRule), r)
err := yaml.UnmarshalYML([]byte(rawRule), r)
if err != nil {
return r, err
}
......
......@@ -25,13 +25,13 @@ import (
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
func TestGetRule(t *testing.T) {
yml := `
scope: application
runtime: true
force: true
`
rule, e := Parse(yml)
rule, e := getRule(yml)
assert.Nil(t, e)
assert.NotNil(t, rule)
assert.Equal(t, true, rule.Force)
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package match
package util
import (
"strings"
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package match
package util
import (
"testing"
......
......@@ -48,3 +48,7 @@ func UnmarshalYMLConfig(confProFile string, out interface{}) ([]byte, error) {
}
return confFileStream, yaml.Unmarshal(confFileStream, out)
}
func UnmarshalYML(data []byte, out interface{}) error {
return yaml.Unmarshal(data, out)
}
......@@ -46,6 +46,18 @@ func TestUnmarshalYMLConfig_Error(t *testing.T) {
assert.Error(t, err)
}
func TestUnmarshalYML(t *testing.T) {
c := &Config{}
b, err := LoadYMLConfig("./testdata/config.yml")
assert.NoError(t, err)
err = UnmarshalYML(b, c)
assert.NoError(t, err)
assert.Equal(t, "strTest", c.StrTest)
assert.Equal(t, 11, c.IntTest)
assert.Equal(t, false, c.BooleanTest)
assert.Equal(t, "childStrTest", c.ChildConfig.StrTest)
}
type Config struct {
StrTest string `yaml:"strTest" default:"default" json:"strTest,omitempty" property:"strTest"`
IntTest int `default:"109" yaml:"intTest" json:"intTest,omitempty" property:"intTest"`
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment