diff --git a/common/yaml/testdata/config.yml b/common/yaml/testdata/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..b5c2ca8ad14310f3a173f0bad9ee25cd65e9a3b3 --- /dev/null +++ b/common/yaml/testdata/config.yml @@ -0,0 +1,7 @@ + +intTest: 11 +booleanTest: false +strTest: "strTest" + +child: + strTest: "childStrTest" \ No newline at end of file diff --git a/common/yaml/yaml.go b/common/yaml/yaml.go index 6c0829bdb4cc9690b3d315797ebce24fea5b4cd4..1d8ac65e90d0e041647c70db07b92b9df5d9644d 100644 --- a/common/yaml/yaml.go +++ b/common/yaml/yaml.go @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package yaml import ( diff --git a/common/yaml/yaml_test.go b/common/yaml/yaml_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8d0eefddc28ff8013fa8bc8074ef3abfa96f2b2d --- /dev/null +++ b/common/yaml/yaml_test.go @@ -0,0 +1,38 @@ +package yaml + +import ( + "path/filepath" + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + +func TestUnmarshalYMLConfig(t *testing.T) { + conPath, err := filepath.Abs("./testdata/config.yml") + assert.NoError(t, err) + c := &Config{} + assert.NoError(t, UnmarshalYMLConfig(conPath, c)) + 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) +} + +func TestUnmarshalYMLConfig_Error(t *testing.T) { + c := &Config{} + assert.Error(t, UnmarshalYMLConfig("./testdata/config", c)) + assert.Error(t, UnmarshalYMLConfig("", c)) +} + +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"` + BooleanTest bool `yaml:"booleanTest" default:"true" json:"booleanTest,omitempty"` + ChildConfig ChildConfig `yaml:"child" json:"child,omitempty"` +} + +type ChildConfig struct { + StrTest string `default:"strTest" default:"default" yaml:"strTest" json:"strTest,omitempty"` +} diff --git a/config/base_config_test.go b/config/base_config_test.go index 34a8852414e2e91929aeb016ee608fc447ac2f56..d16b2420922ece60ef2135729cd47d5aa73a3760 100644 --- a/config/base_config_test.go +++ b/config/base_config_test.go @@ -18,7 +18,6 @@ package config import ( "fmt" - "path/filepath" "reflect" "testing" ) @@ -28,7 +27,6 @@ import ( import ( "github.com/apache/dubbo-go/common/config" "github.com/apache/dubbo-go/common/extension" - "github.com/apache/dubbo-go/common/yaml" "github.com/apache/dubbo-go/config_center" _ "github.com/apache/dubbo-go/config_center/apollo" ) @@ -519,13 +517,3 @@ func Test_initializeStruct(t *testing.T) { return consumerConfig.References != nil }) } - -func TestUnmarshalYMLConfig(t *testing.T) { - conPath, err := filepath.Abs("./testdata/consumer_config_with_configcenter.yml") - assert.NoError(t, err) - c := &ConsumerConfig{} - assert.NoError(t, yaml.UnmarshalYMLConfig(conPath, c)) - assert.Equal(t, "default", c.ProxyFactory) - assert.Equal(t, "dubbo.properties", c.ConfigCenterConfig.ConfigFile) - assert.Equal(t, "100ms", c.Connect_Timeout) -}