From 8bfee444fd1e81276381ab2e2c72c7fc867cade0 Mon Sep 17 00:00:00 2001 From: chenrui Date: Tue, 31 Aug 2021 15:20:11 +0800 Subject: [PATCH] fix(core/config/xml): prompt error when config format is incorrect --- core/config/xml/xml.go | 12 +++++++++++- core/config/xml/xml_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/config/xml/xml.go b/core/config/xml/xml.go index 067d4811..699dbe28 100644 --- a/core/config/xml/xml.go +++ b/core/config/xml/xml.go @@ -70,7 +70,17 @@ func (xc *Config) ParseData(data []byte) (config.Configer, error) { return nil, err } - x.data = config.ExpandValueEnvForMap(d["config"].(map[string]interface{})) + v := d["config"] + if v == nil { + return nil, fmt.Errorf("xml parse should incluce in tags") + } + + confVal, ok := v.(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("xml parse tags should incluce sub tags") + } + + x.data = config.ExpandValueEnvForMap(confVal) return x, nil } diff --git a/core/config/xml/xml_test.go b/core/config/xml/xml_test.go index c71488fe..6ab00726 100644 --- a/core/config/xml/xml_test.go +++ b/core/config/xml/xml_test.go @@ -150,6 +150,25 @@ func TestXML(t *testing.T) { assert.Equal(t, "MySection", sec.Name) } +func TestXMLMissConfig(t *testing.T) { + xmlcontext1 := ` + + beeapi + ` + + c := &Config{} + _, err := c.ParseData([]byte(xmlcontext1)) + assert.Equal(t, "xml parse should incluce in tags", err.Error()) + + xmlcontext2 := ` + + + ` + + _, err = c.ParseData([]byte(xmlcontext2)) + assert.Equal(t, "xml parse tags should incluce sub tags", err.Error()) +} + type Section struct { Name string `xml:"name"` }