Skip to content

Commit

Permalink
fixed bug:Fix the problem that some types of yaml configuration canno…
Browse files Browse the repository at this point in the history
…t be converted, resulting in configuration loss (#146)

* fixed bug 

修复读取ssl配置时 因配置bool值导致配置读取不全的bug
#122
go-chassis/go-chassis#991

* Add UT for type needs to be converted

为yaml配置需要转换的类型增加UT 例如 bool -> string
  • Loading branch information
five111 authored Jul 21, 2021
1 parent 4cf1d9c commit 7fd6ae1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
40 changes: 40 additions & 0 deletions archaius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,43 @@ func TestClean(t *testing.T) {
s := archaius.Get("age")
assert.Equal(t, nil, s)
}
func TestInitConfigBool2String(t *testing.T) {
b := []byte(`
ssl:
rest.Provider.cipherPlugin: aes
rest.Provider.verifyPeer: false
rest.Provider.cipherSuits: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
rest.Provider.protocol: TLSv1.2
rest.Provider.caFile: ca.cer
rest.Provider.certFile: cert_chain.pem
rest.Provider.keyFile: private.pem
rest.Provider.certPwdFile: PwdFile.yaml
`)
d, _ := os.Getwd()
testFile := filepath.Join(d, "tls_bool.yaml")
f1, err := os.Create(testFile)
assert.NoError(t, err)
err = archaius.Init(archaius.WithMemorySource())
assert.NoError(t, err)
defer f1.Close()
defer os.Remove(testFile)
_, err = io.WriteString(f1, string(b))
assert.NoError(t, err)
type Ssl struct {
Ssl map[string]string `yaml:"ssl"`
}
err = archaius.AddFile(testFile)
assert.NoError(t, err)
//p := &Person{}
sslConfig := &Ssl{}
err = archaius.UnmarshalConfig(sslConfig)
assert.NoError(t, err)
assert.Equal(t, "aes", sslConfig.Ssl["rest.Provider.cipherPlugin"])
assert.Equal(t, "false", sslConfig.Ssl["rest.Provider.verifyPeer"])
assert.Equal(t, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", sslConfig.Ssl["rest.Provider.cipherSuits"])
assert.Equal(t, "TLSv1.2", sslConfig.Ssl["rest.Provider.protocol"])
assert.Equal(t, "ca.cer", sslConfig.Ssl["rest.Provider.caFile"])
assert.Equal(t, "cert_chain.pem", sslConfig.Ssl["rest.Provider.certFile"])
assert.Equal(t, "private.pem", sslConfig.Ssl["rest.Provider.keyFile"])
assert.Equal(t, "PwdFile.yaml", sslConfig.Ssl["rest.Provider.certPwdFile"])
}
17 changes: 7 additions & 10 deletions source/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,18 +327,15 @@ func (m *Manager) populateMap(prefix string, mapType reflect.Type, rValues refle

// maybe next map type
if mapValueType != setVal.Type() {
return rValue, nil

}

returnCongValue, err := m.toRvalueType(setVal.Interface(), reflect.New(mapValueType).Elem())
if err != nil {
return rValue, fmt.Errorf(fmtValueNotMatched,
prefix+key, mapValueType, setVal.String())
returnCongValue, err := m.toRvalueType(setVal.Interface(), reflect.New(mapValueType).Elem())
if err != nil {
return rValue, fmt.Errorf(fmtValueNotMatched,
prefix+key, mapValueType, setVal.String())
}
setVal = returnCongValue
}

if rValue.CanSet() {
rValue.SetMapIndex(reflect.ValueOf(key[1:]), returnCongValue)
rValue.SetMapIndex(reflect.ValueOf(key[1:]), setVal)
}
default:
splitKey := strings.Split(key, `.`)
Expand Down

0 comments on commit 7fd6ae1

Please sign in to comment.