From a673a85d4afaf1166152c0e6c5b68dfe580a5d2e Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 30 May 2014 23:48:23 -0500 Subject: [PATCH] added tests config/json_test that test missing key usecases. created a template function to fetch AppConfig values --- config/json_test.go | 24 ++++++++++++++++++++++++ template.go | 1 + templatefunc.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/config/json_test.go b/config/json_test.go index 4e83470f..e0046084 100644 --- a/config/json_test.go +++ b/config/json_test.go @@ -100,4 +100,28 @@ func TestJson(t *testing.T) { t.Fatal("get host err") } } + + if _, err := jsonconf.Int("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an Int") + } + + if _, err := jsonconf.Int64("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an Int64") + } + + if _, err := jsonconf.Float("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting a Float") + } + + if _, err := jsonconf.DIY("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting an interface{}") + } + + if val := jsonconf.String("unknown"); val != "" { + t.Error("unknown keys should return an empty string when expecting a String") + } + + if _, err := jsonconf.Bool("unknown"); err == nil { + t.Error("unknown keys should return an error when expecting a Bool") + } } diff --git a/template.go b/template.go index 95deb443..9411930c 100644 --- a/template.go +++ b/template.go @@ -44,6 +44,7 @@ func init() { beegoTplFuncMap["renderform"] = RenderForm beegoTplFuncMap["assets_js"] = AssetsJs beegoTplFuncMap["assets_css"] = AssetsCss + beegoTplFuncMap["config"] = Config // go1.2 added template funcs // Comparisons diff --git a/templatefunc.go b/templatefunc.go index 62584781..f89d12e3 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -131,6 +131,43 @@ func Compare(a, b interface{}) (equal bool) { return } +func Config(returnType, key string, defaultVal interface{}) (value interface{}, err error) { + switch returnType { + case "String": + value = AppConfig.String(key) + case "Bool": + value, err = AppConfig.Bool(key) + case "Int": + value, err = AppConfig.Int(key) + case "Int64": + value, err = AppConfig.Int64(key) + case "Float": + value, err = AppConfig.Float(key) + case "DIY": + value, err = AppConfig.DIY(key) + default: + err = errors.New("Config keys must be of type String, Bool, Int, Int64, Float, or DIY!") + } + + if err != nil { + if reflect.TypeOf(returnType) != reflect.TypeOf(defaultVal) { + err = errors.New("defaultVal type does not match returnType!") + } else { + value, err = defaultVal, nil + } + } else if reflect.TypeOf(value).Kind() == reflect.String { + if value == "" { + if reflect.TypeOf(defaultVal).Kind() != reflect.String { + err = errors.New("defaultVal type must be a String if the returnType is a String") + } else { + value = defaultVal.(string) + } + } + } + + return +} + // Convert string to template.HTML type. func Str2html(raw string) template.HTML { return template.HTML(raw)