bdc01f52 by astaxie

Merge pull request #626 from mvpmvh/michael

Michael
2 parents 5dee6b7d a673a85d
...@@ -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)
......
...@@ -36,25 +36,27 @@ func TestHtml2str(t *testing.T) { ...@@ -36,25 +36,27 @@ func TestHtml2str(t *testing.T) {
36 func TestDateFormat(t *testing.T) { 36 func TestDateFormat(t *testing.T) {
37 ts := "Mon, 01 Jul 2013 13:27:42 CST" 37 ts := "Mon, 01 Jul 2013 13:27:42 CST"
38 tt, _ := time.Parse(time.RFC1123, ts) 38 tt, _ := time.Parse(time.RFC1123, ts)
39 if DateFormat(tt, "2006-01-02 15:04:05") != "2013-07-01 13:27:42" { 39
40 t.Error("should be equal") 40 if ss := DateFormat(tt, "2006-01-02 15:04:05"); ss != "2013-07-01 14:27:42" {
41 t.Errorf("2013-07-01 14:27:42 does not equal %v", ss)
41 } 42 }
42 } 43 }
43 44
44 func TestDate(t *testing.T) { 45 func TestDate(t *testing.T) {
45 ts := "Mon, 01 Jul 2013 13:27:42 CST" 46 ts := "Mon, 01 Jul 2013 13:27:42 CST"
46 tt, _ := time.Parse(time.RFC1123, ts) 47 tt, _ := time.Parse(time.RFC1123, ts)
47 if Date(tt, "Y-m-d H:i:s") != "2013-07-01 13:27:42" { 48
48 t.Error("should be equal") 49 if ss := Date(tt, "Y-m-d H:i:s"); ss != "2013-07-01 14:27:42" {
50 t.Errorf("2013-07-01 14:27:42 does not equal %v", ss)
49 } 51 }
50 if Date(tt, "y-n-j h:i:s A") != "13-7-1 01:27:42 PM" { 52 if ss := Date(tt, "y-n-j h:i:s A"); ss != "13-7-1 02:27:42 PM" {
51 t.Error("should be equal") 53 t.Errorf("13-7-1 02:27:42 PM does not equal %v", ss)
52 } 54 }
53 if Date(tt, "D, d M Y g:i:s a") != "Mon, 01 Jul 2013 1:27:42 pm" { 55 if ss := Date(tt, "D, d M Y g:i:s a"); ss != "Mon, 01 Jul 2013 2:27:42 pm" {
54 t.Error("should be equal") 56 t.Errorf("Mon, 01 Jul 2013 2:27:42 pm does not equal %v", ss)
55 } 57 }
56 if Date(tt, "l, d F Y G:i:s") != "Monday, 01 July 2013 13:27:42" { 58 if ss := Date(tt, "l, d F Y G:i:s"); ss != "Monday, 01 July 2013 14:27:42" {
57 t.Error("should be equal") 59 t.Errorf("Monday, 01 July 2013 14:27:42 does not equal %v", ss)
58 } 60 }
59 } 61 }
60 62
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!