|
@@ -2991,6 +2991,97 @@ func TestUnmarshaler_InheritFromGrandparent(t *testing.T) {
|
|
|
assert.Equal(t, "localhost:8080", s.Middle.Value.Discovery)
|
|
|
}
|
|
|
|
|
|
+func TestUnmarshaler_InheritSequence(t *testing.T) {
|
|
|
+ var testConf = []byte(`
|
|
|
+Nacos:
|
|
|
+ NamespaceId: "123"
|
|
|
+RpcConf:
|
|
|
+ Nacos:
|
|
|
+ NamespaceId: "456"
|
|
|
+ Name: hello
|
|
|
+`)
|
|
|
+
|
|
|
+ type (
|
|
|
+ NacosConf struct {
|
|
|
+ NamespaceId string
|
|
|
+ }
|
|
|
+
|
|
|
+ RpcConf struct {
|
|
|
+ Nacos NacosConf `json:",inherit"`
|
|
|
+ Name string
|
|
|
+ }
|
|
|
+
|
|
|
+ Config1 struct {
|
|
|
+ RpcConf RpcConf
|
|
|
+ Nacos NacosConf
|
|
|
+ }
|
|
|
+
|
|
|
+ Config2 struct {
|
|
|
+ RpcConf RpcConf
|
|
|
+ Nacos NacosConf
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+ var c1 Config1
|
|
|
+ assert.NoError(t, UnmarshalYamlBytes(testConf, &c1))
|
|
|
+ assert.Equal(t, "123", c1.Nacos.NamespaceId)
|
|
|
+ assert.Equal(t, "456", c1.RpcConf.Nacos.NamespaceId)
|
|
|
+
|
|
|
+ var c2 Config2
|
|
|
+ assert.NoError(t, UnmarshalYamlBytes(testConf, &c2))
|
|
|
+ assert.Equal(t, "123", c1.Nacos.NamespaceId)
|
|
|
+ assert.Equal(t, "456", c1.RpcConf.Nacos.NamespaceId)
|
|
|
+}
|
|
|
+
|
|
|
+func TestUnmarshaler_InheritNested(t *testing.T) {
|
|
|
+ var testConf = []byte(`
|
|
|
+Nacos:
|
|
|
+ Value1: "123"
|
|
|
+Server:
|
|
|
+ Nacos:
|
|
|
+ Value2: "456"
|
|
|
+ Rpc:
|
|
|
+ Nacos:
|
|
|
+ Value3: "789"
|
|
|
+ Name: hello
|
|
|
+`)
|
|
|
+
|
|
|
+ type (
|
|
|
+ NacosConf struct {
|
|
|
+ Value1 string `json:",optional"`
|
|
|
+ Value2 string `json:",optional"`
|
|
|
+ Value3 string `json:",optional"`
|
|
|
+ }
|
|
|
+
|
|
|
+ RpcConf struct {
|
|
|
+ Nacos NacosConf `json:",inherit"`
|
|
|
+ Name string
|
|
|
+ }
|
|
|
+
|
|
|
+ ServerConf struct {
|
|
|
+ Nacos NacosConf `json:",inherit"`
|
|
|
+ Rpc RpcConf
|
|
|
+ }
|
|
|
+
|
|
|
+ Config struct {
|
|
|
+ Server ServerConf
|
|
|
+ Nacos NacosConf
|
|
|
+ }
|
|
|
+ )
|
|
|
+
|
|
|
+ var c Config
|
|
|
+ assert.NoError(t, UnmarshalYamlBytes(testConf, &c))
|
|
|
+ assert.Equal(t, "123", c.Nacos.Value1)
|
|
|
+ assert.Empty(t, c.Nacos.Value2)
|
|
|
+ assert.Empty(t, c.Nacos.Value3)
|
|
|
+ assert.Equal(t, "123", c.Server.Nacos.Value1)
|
|
|
+ assert.Equal(t, "456", c.Server.Nacos.Value2)
|
|
|
+ assert.Empty(t, c.Nacos.Value3)
|
|
|
+ assert.Equal(t, "123", c.Server.Rpc.Nacos.Value1)
|
|
|
+ assert.Equal(t, "456", c.Server.Rpc.Nacos.Value2)
|
|
|
+ assert.Equal(t, "789", c.Server.Rpc.Nacos.Value3)
|
|
|
+}
|
|
|
+
|
|
|
func TestUnmarshalValuer(t *testing.T) {
|
|
|
unmarshaler := NewUnmarshaler(jsonTagKey)
|
|
|
var foo string
|