抛出异常

This commit is contained in:
liangzy 2020-04-03 11:21:51 +08:00
parent 1869fc6de7
commit b88d45686a
4 changed files with 11 additions and 31 deletions

1
conf/dev/test.yaml Normal file
View File

@ -0,0 +1 @@
Aa: "hello world"

1
conf/test.yaml Normal file
View File

@ -0,0 +1 @@
Aa: "hello world"

View File

@ -1,38 +1,12 @@
package grconfig package grconfig
import ( import (
"fmt"
"log" "log"
"lot.gaore.com/library/common" "lot.gaore.com/library/grconfig"
"testing" "testing"
) )
type Config struct { func TestNew(t *testing.T) {
ServerUrl string `yaml:"server_url"` gr := grconfig.New("conf/")
AccessKey string `yaml:"access_key"` log.Println(gr.String("test.a"))
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"))
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"log"
"strings" "strings"
) )
@ -36,6 +37,9 @@ func (conf *ConfigerChannelYAML) Item(item string, t interface{}) (err error) {
func parseYAML(buf bytes.Buffer) interface{} { func parseYAML(buf bytes.Buffer) interface{} {
var v 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 return v
} }