From b88d45686a2b74cae5a7e5b2a3ae637a930a9042 Mon Sep 17 00:00:00 2001 From: liangzy Date: Fri, 3 Apr 2020 11:21:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/dev/test.yaml | 1 + conf/test.yaml | 1 + grconfig_test.go | 34 ++++------------------------------ yaml.go | 6 +++++- 4 files changed, 11 insertions(+), 31 deletions(-) create mode 100644 conf/dev/test.yaml create mode 100644 conf/test.yaml diff --git a/conf/dev/test.yaml b/conf/dev/test.yaml new file mode 100644 index 0000000..1e6c2cc --- /dev/null +++ b/conf/dev/test.yaml @@ -0,0 +1 @@ +Aa: "hello world" \ No newline at end of file diff --git a/conf/test.yaml b/conf/test.yaml new file mode 100644 index 0000000..1e6c2cc --- /dev/null +++ b/conf/test.yaml @@ -0,0 +1 @@ +Aa: "hello world" \ No newline at end of file diff --git a/grconfig_test.go b/grconfig_test.go index fb3c90e..2b6c240 100644 --- a/grconfig_test.go +++ b/grconfig_test.go @@ -1,38 +1,12 @@ package grconfig import ( - "fmt" "log" - "lot.gaore.com/library/common" + "lot.gaore.com/library/grconfig" "testing" ) -type Config struct { - ServerUrl string `yaml:"server_url"` - AccessKey string `yaml:"access_key"` - SecretKey string `yaml:"secret_key"` - InstanceId string `yaml:"instance_id"` -} - -func TestNewConfig(t *testing.T) { - var m1 Config - var m2 Config - c := New(common.GetCwd("conf")) - err := c.Item("mqtt.default", &m1) - log.Println(fmt.Sprintf("%+v", m1.ServerUrl)) - if err != nil { - t.Error(err) - } - - err = c.Item("mqtt.backup", &m2) - log.Println(fmt.Sprintf("%+v", m2.ServerUrl)) - if err != nil { - t.Error(err) - } - - if c.String("mqtt.default.instance_id") == "" { - t.Error("Empty String") - } - - log.Println(c.String("mqtt.default.instance_id")) +func TestNew(t *testing.T) { + gr := grconfig.New("conf/") + log.Println(gr.String("test.a")) } diff --git a/yaml.go b/yaml.go index 52c2ee7..0ba931f 100644 --- a/yaml.go +++ b/yaml.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "gopkg.in/yaml.v2" + "log" "strings" ) @@ -36,6 +37,9 @@ func (conf *ConfigerChannelYAML) Item(item string, t interface{}) (err error) { func parseYAML(buf bytes.Buffer) interface{} { var v interface{} - yaml.Unmarshal(buf.Bytes(), &v) + if err := yaml.Unmarshal(buf.Bytes(), &v); err != nil { + log.Println(err) + panic(err) + } return v }