feat: conf inherit (#2568)

* feat: add ValuerWithParent

* feat: make etcd config inherit from parents

* chore: add more tests

* chore: add more tests

* chore: add more comments

* chore: refactor

* chore: add more comments

* fix: fix duplicated code and refactor

* fix: remove unnecessary code

* fix: fix test case for removing print

* feat: support partial inherit
This commit is contained in:
Kevin Wan
2022-11-08 15:27:48 +08:00
committed by GitHub
parent dac3600b53
commit 1677a4dceb
7 changed files with 472 additions and 49 deletions

View File

@@ -7,10 +7,10 @@ import (
"strings"
"testing"
"time"
"unicode"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/stringx"
)
@@ -38,6 +38,29 @@ func TestUnmarshalWithoutTagName(t *testing.T) {
assert.True(t, in.Optional)
}
func TestUnmarshalWithoutTagNameWithCanonicalKey(t *testing.T) {
type inner struct {
Name string `key:"name"`
}
m := map[string]interface{}{
"Name": "go-zero",
}
var in inner
unmarshaler := NewUnmarshaler(defaultKeyName, WithCanonicalKeyFunc(func(s string) string {
first := true
return strings.Map(func(r rune) rune {
if first {
first = false
return unicode.ToTitle(r)
}
return r
}, s)
}))
assert.Nil(t, unmarshaler.Unmarshal(m, &in))
assert.Equal(t, "go-zero", in.Name)
}
func TestUnmarshalBool(t *testing.T) {
type inner struct {
True bool `key:"yes"`
@@ -2718,6 +2741,256 @@ func TestUnmarshalNestedMapSimpleTypeMatch(t *testing.T) {
assert.Equal(t, "1", c.Anything["id"])
}
func TestUnmarshalInheritPrimitiveUseParent(t *testing.T) {
type (
component struct {
Name string `key:"name"`
Discovery string `key:"discovery,inherit"`
}
server struct {
Discovery string `key:"discovery"`
Component component `key:"component"`
}
)
var s server
assert.NoError(t, UnmarshalKey(map[string]interface{}{
"discovery": "localhost:8080",
"component": map[string]interface{}{
"name": "test",
},
}, &s))
assert.Equal(t, "localhost:8080", s.Discovery)
assert.Equal(t, "localhost:8080", s.Component.Discovery)
}
func TestUnmarshalInheritPrimitiveUseSelf(t *testing.T) {
type (
component struct {
Name string `key:"name"`
Discovery string `key:"discovery,inherit"`
}
server struct {
Discovery string `key:"discovery"`
Component component `key:"component"`
}
)
var s server
assert.NoError(t, UnmarshalKey(map[string]interface{}{
"discovery": "localhost:8080",
"component": map[string]interface{}{
"name": "test",
"discovery": "localhost:8888",
},
}, &s))
assert.Equal(t, "localhost:8080", s.Discovery)
assert.Equal(t, "localhost:8888", s.Component.Discovery)
}
func TestUnmarshalInheritPrimitiveNotExist(t *testing.T) {
type (
component struct {
Name string `key:"name"`
Discovery string `key:"discovery,inherit"`
}
server struct {
Component component `key:"component"`
}
)
var s server
assert.NotNil(t, UnmarshalKey(map[string]interface{}{
"component": map[string]interface{}{
"name": "test",
},
}, &s))
}
func TestUnmarshalInheritStructUseParent(t *testing.T) {
type (
discovery struct {
Host string `key:"host"`
Port int `key:"port"`
}
component struct {
Name string `key:"name"`
Discovery discovery `key:"discovery,inherit"`
}
server struct {
Discovery discovery `key:"discovery"`
Component component `key:"component"`
}
)
var s server
assert.NoError(t, UnmarshalKey(map[string]interface{}{
"discovery": map[string]interface{}{
"host": "localhost",
"port": 8080,
},
"component": map[string]interface{}{
"name": "test",
},
}, &s))
assert.Equal(t, "localhost", s.Discovery.Host)
assert.Equal(t, 8080, s.Discovery.Port)
assert.Equal(t, "localhost", s.Component.Discovery.Host)
assert.Equal(t, 8080, s.Component.Discovery.Port)
}
func TestUnmarshalInheritStructUseSelf(t *testing.T) {
type (
discovery struct {
Host string `key:"host"`
Port int `key:"port"`
}
component struct {
Name string `key:"name"`
Discovery discovery `key:"discovery,inherit"`
}
server struct {
Discovery discovery `key:"discovery"`
Component component `key:"component"`
}
)
var s server
assert.NoError(t, UnmarshalKey(map[string]interface{}{
"discovery": map[string]interface{}{
"host": "localhost",
"port": 8080,
},
"component": map[string]interface{}{
"name": "test",
"discovery": map[string]interface{}{
"host": "remotehost",
"port": 8888,
},
},
}, &s))
assert.Equal(t, "localhost", s.Discovery.Host)
assert.Equal(t, 8080, s.Discovery.Port)
assert.Equal(t, "remotehost", s.Component.Discovery.Host)
assert.Equal(t, 8888, s.Component.Discovery.Port)
}
func TestUnmarshalInheritStructNotExist(t *testing.T) {
type (
discovery struct {
Host string `key:"host"`
Port int `key:"port"`
}
component struct {
Name string `key:"name"`
Discovery discovery `key:"discovery,inherit"`
}
server struct {
Component component `key:"component"`
}
)
var s server
assert.NotNil(t, UnmarshalKey(map[string]interface{}{
"component": map[string]interface{}{
"name": "test",
},
}, &s))
}
func TestUnmarshalInheritStructUsePartial(t *testing.T) {
type (
discovery struct {
Host string `key:"host"`
Port int `key:"port"`
}
component struct {
Name string `key:"name"`
Discovery discovery `key:"discovery,inherit"`
}
server struct {
Discovery discovery `key:"discovery"`
Component component `key:"component"`
}
)
var s server
assert.NoError(t, UnmarshalKey(map[string]interface{}{
"discovery": map[string]interface{}{
"host": "localhost",
"port": 8080,
},
"component": map[string]interface{}{
"name": "test",
"discovery": map[string]interface{}{
"port": 8888,
},
},
}, &s))
assert.Equal(t, "localhost", s.Discovery.Host)
assert.Equal(t, 8080, s.Discovery.Port)
assert.Equal(t, "localhost", s.Component.Discovery.Host)
assert.Equal(t, 8888, s.Component.Discovery.Port)
}
func TestUnmarshalInheritStructUseSelfIncorrectType(t *testing.T) {
type (
discovery struct {
Host string `key:"host"`
Port int `key:"port"`
}
component struct {
Name string `key:"name"`
Discovery discovery `key:"discovery,inherit"`
}
server struct {
Discovery discovery `key:"discovery"`
Component component `key:"component"`
}
)
var s server
assert.NotNil(t, UnmarshalKey(map[string]interface{}{
"discovery": map[string]interface{}{
"host": "localhost",
},
"component": map[string]interface{}{
"name": "test",
"discovery": map[string]string{
"host": "remotehost",
},
},
}, &s))
}
func TestUnmarshaler_InheritFromGrandparent(t *testing.T) {
type (
component struct {
Name string `key:"name"`
Discovery string `key:"discovery,inherit"`
}
middle struct {
Value component `key:"value"`
}
server struct {
Discovery string `key:"discovery"`
Middle middle `key:"middle"`
}
)
var s server
assert.NoError(t, UnmarshalKey(map[string]interface{}{
"discovery": "localhost:8080",
"middle": map[string]interface{}{
"value": map[string]interface{}{
"name": "test",
},
},
}, &s))
assert.Equal(t, "localhost:8080", s.Discovery)
assert.Equal(t, "localhost:8080", s.Middle.Value.Discovery)
}
func TestUnmarshalValuer(t *testing.T) {
unmarshaler := NewUnmarshaler(jsonTagKey)
var foo string