added tests config/json_test that test missing key usecases. created a template …
…function to fetch AppConfig values
Showing
3 changed files
with
62 additions
and
0 deletions
| ... | @@ -100,4 +100,28 @@ func TestJson(t *testing.T) { | ... | @@ -100,4 +100,28 @@ func TestJson(t *testing.T) { |
| 100 | t.Fatal("get host err") | 100 | t.Fatal("get host err") |
| 101 | } | 101 | } |
| 102 | } | 102 | } |
| 103 | |||
| 104 | if _, err := jsonconf.Int("unknown"); err == nil { | ||
| 105 | t.Error("unknown keys should return an error when expecting an Int") | ||
| 106 | } | ||
| 107 | |||
| 108 | if _, err := jsonconf.Int64("unknown"); err == nil { | ||
| 109 | t.Error("unknown keys should return an error when expecting an Int64") | ||
| 110 | } | ||
| 111 | |||
| 112 | if _, err := jsonconf.Float("unknown"); err == nil { | ||
| 113 | t.Error("unknown keys should return an error when expecting a Float") | ||
| 114 | } | ||
| 115 | |||
| 116 | if _, err := jsonconf.DIY("unknown"); err == nil { | ||
| 117 | t.Error("unknown keys should return an error when expecting an interface{}") | ||
| 118 | } | ||
| 119 | |||
| 120 | if val := jsonconf.String("unknown"); val != "" { | ||
| 121 | t.Error("unknown keys should return an empty string when expecting a String") | ||
| 122 | } | ||
| 123 | |||
| 124 | if _, err := jsonconf.Bool("unknown"); err == nil { | ||
| 125 | t.Error("unknown keys should return an error when expecting a Bool") | ||
| 126 | } | ||
| 103 | } | 127 | } | ... | ... |
| ... | @@ -44,6 +44,7 @@ func init() { | ... | @@ -44,6 +44,7 @@ func init() { |
| 44 | beegoTplFuncMap["renderform"] = RenderForm | 44 | beegoTplFuncMap["renderform"] = RenderForm |
| 45 | beegoTplFuncMap["assets_js"] = AssetsJs | 45 | beegoTplFuncMap["assets_js"] = AssetsJs |
| 46 | beegoTplFuncMap["assets_css"] = AssetsCss | 46 | beegoTplFuncMap["assets_css"] = AssetsCss |
| 47 | beegoTplFuncMap["config"] = Config | ||
| 47 | 48 | ||
| 48 | // go1.2 added template funcs | 49 | // go1.2 added template funcs |
| 49 | // Comparisons | 50 | // Comparisons | ... | ... |
| ... | @@ -131,6 +131,43 @@ func Compare(a, b interface{}) (equal bool) { | ... | @@ -131,6 +131,43 @@ func Compare(a, b interface{}) (equal bool) { |
| 131 | return | 131 | return |
| 132 | } | 132 | } |
| 133 | 133 | ||
| 134 | func Config(returnType, key string, defaultVal interface{}) (value interface{}, err error) { | ||
| 135 | switch returnType { | ||
| 136 | case "String": | ||
| 137 | value = AppConfig.String(key) | ||
| 138 | case "Bool": | ||
| 139 | value, err = AppConfig.Bool(key) | ||
| 140 | case "Int": | ||
| 141 | value, err = AppConfig.Int(key) | ||
| 142 | case "Int64": | ||
| 143 | value, err = AppConfig.Int64(key) | ||
| 144 | case "Float": | ||
| 145 | value, err = AppConfig.Float(key) | ||
| 146 | case "DIY": | ||
| 147 | value, err = AppConfig.DIY(key) | ||
| 148 | default: | ||
| 149 | err = errors.New("Config keys must be of type String, Bool, Int, Int64, Float, or DIY!") | ||
| 150 | } | ||
| 151 | |||
| 152 | if err != nil { | ||
| 153 | if reflect.TypeOf(returnType) != reflect.TypeOf(defaultVal) { | ||
| 154 | err = errors.New("defaultVal type does not match returnType!") | ||
| 155 | } else { | ||
| 156 | value, err = defaultVal, nil | ||
| 157 | } | ||
| 158 | } else if reflect.TypeOf(value).Kind() == reflect.String { | ||
| 159 | if value == "" { | ||
| 160 | if reflect.TypeOf(defaultVal).Kind() != reflect.String { | ||
| 161 | err = errors.New("defaultVal type must be a String if the returnType is a String") | ||
| 162 | } else { | ||
| 163 | value = defaultVal.(string) | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | return | ||
| 169 | } | ||
| 170 | |||
| 134 | // Convert string to template.HTML type. | 171 | // Convert string to template.HTML type. |
| 135 | func Str2html(raw string) template.HTML { | 172 | func Str2html(raw string) template.HTML { |
| 136 | return template.HTML(raw) | 173 | return template.HTML(raw) | ... | ... |
-
Please register or sign in to post a comment