19119e99 by astaxie

move utils to utils libs & func move to templatefunc

1 parent d603a671
...@@ -3,6 +3,7 @@ package beego ...@@ -3,6 +3,7 @@ package beego
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "crypto/hmac" 5 "crypto/hmac"
6 "crypto/rand"
6 "crypto/sha1" 7 "crypto/sha1"
7 "encoding/base64" 8 "encoding/base64"
8 "errors" 9 "errors"
...@@ -370,7 +371,7 @@ func (c *Controller) XsrfToken() string { ...@@ -370,7 +371,7 @@ func (c *Controller) XsrfToken() string {
370 } else { 371 } else {
371 expire = int64(XSRFExpire) 372 expire = int64(XSRFExpire)
372 } 373 }
373 token = GetRandomString(15) 374 token = getRandomString(15)
374 c.SetSecureCookie(XSRFKEY, "_xsrf", token, expire) 375 c.SetSecureCookie(XSRFKEY, "_xsrf", token, expire)
375 } 376 }
376 c._xsrf_token = token 377 c._xsrf_token = token
...@@ -405,3 +406,14 @@ func (c *Controller) GoToFunc(funcname string) { ...@@ -405,3 +406,14 @@ func (c *Controller) GoToFunc(funcname string) {
405 } 406 }
406 c.gotofunc = funcname 407 c.gotofunc = funcname
407 } 408 }
409
410 //utils func for controller internal
411 func getRandomString(n int) string {
412 const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
413 var bytes = make([]byte, n)
414 rand.Read(bytes)
415 for i, b := range bytes {
416 bytes[i] = alphanum[b%byte(len(alphanum))]
417 }
418 return string(bytes)
419 }
......
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
15 beecontext "github.com/astaxie/beego/context" 15 beecontext "github.com/astaxie/beego/context"
16 "github.com/astaxie/beego/middleware" 16 "github.com/astaxie/beego/middleware"
17 "github.com/astaxie/beego/toolbox" 17 "github.com/astaxie/beego/toolbox"
18 "github.com/astaxie/beego/utils"
18 ) 19 )
19 20
20 const ( 21 const (
...@@ -159,7 +160,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM ...@@ -159,7 +160,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
159 } 160 }
160 comma := strings.Split(colon[0], ",") 161 comma := strings.Split(colon[0], ",")
161 for _, m := range comma { 162 for _, m := range comma {
162 if m == "*" || inSlice(strings.ToLower(m), HTTPMETHOD) { 163 if m == "*" || utils.InSlice(strings.ToLower(m), HTTPMETHOD) {
163 if val := reflectVal.MethodByName(colon[1]); val.IsValid() { 164 if val := reflectVal.MethodByName(colon[1]); val.IsValid() {
164 methods[strings.ToLower(m)] = colon[1] 165 methods[strings.ToLower(m)] = colon[1]
165 } else { 166 } else {
...@@ -272,7 +273,7 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { ...@@ -272,7 +273,7 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
272 for _, route := range p.fixrouters { 273 for _, route := range p.fixrouters {
273 if route.controllerType.Name() == controllName { 274 if route.controllerType.Name() == controllName {
274 var finded bool 275 var finded bool
275 if inSlice(strings.ToLower(methodName), HTTPMETHOD) { 276 if utils.InSlice(strings.ToLower(methodName), HTTPMETHOD) {
276 if route.hasMethod { 277 if route.hasMethod {
277 if m, ok := route.methods[strings.ToLower(methodName)]; ok && m != methodName { 278 if m, ok := route.methods[strings.ToLower(methodName)]; ok && m != methodName {
278 finded = false 279 finded = false
...@@ -303,7 +304,7 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { ...@@ -303,7 +304,7 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
303 for _, route := range p.routers { 304 for _, route := range p.routers {
304 if route.controllerType.Name() == controllName { 305 if route.controllerType.Name() == controllName {
305 var finded bool 306 var finded bool
306 if inSlice(strings.ToLower(methodName), HTTPMETHOD) { 307 if utils.InSlice(strings.ToLower(methodName), HTTPMETHOD) {
307 if route.hasMethod { 308 if route.hasMethod {
308 if m, ok := route.methods[strings.ToLower(methodName)]; ok && m != methodName { 309 if m, ok := route.methods[strings.ToLower(methodName)]; ok && m != methodName {
309 finded = false 310 finded = false
...@@ -419,7 +420,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -419,7 +420,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
419 context.Output = beecontext.NewOutput(rw) 420 context.Output = beecontext.NewOutput(rw)
420 } 421 }
421 422
422 if !inSlice(strings.ToLower(r.Method), HTTPMETHOD) { 423 if !utils.InSlice(strings.ToLower(r.Method), HTTPMETHOD) {
423 http.Error(w, "Method Not Allowed", 405) 424 http.Error(w, "Method Not Allowed", 405)
424 goto Admin 425 goto Admin
425 } 426 }
......
...@@ -9,9 +9,10 @@ import ( ...@@ -9,9 +9,10 @@ import (
9 "io/ioutil" 9 "io/ioutil"
10 "os" 10 "os"
11 "path/filepath" 11 "path/filepath"
12 "reflect"
13 "regexp" 12 "regexp"
14 "strings" 13 "strings"
14
15 "github.com/astaxie/beego/utils"
15 ) 16 )
16 17
17 var ( 18 var (
...@@ -144,7 +145,7 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp ...@@ -144,7 +145,7 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
144 } else { 145 } else {
145 fileabspath = filepath.Join(root, file) 146 fileabspath = filepath.Join(root, file)
146 } 147 }
147 if e, _ := FileExists(fileabspath); !e { 148 if e := utils.FileExists(fileabspath); !e {
148 panic("can't find template file" + file) 149 panic("can't find template file" + file)
149 } 150 }
150 data, err := ioutil.ReadFile(fileabspath) 151 data, err := ioutil.ReadFile(fileabspath)
...@@ -238,156 +239,3 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others ...@@ -238,156 +239,3 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others
238 } 239 }
239 return 240 return
240 } 241 }
241
242 // go1.2 added template funcs. begin
243 var (
244 errBadComparisonType = errors.New("invalid type for comparison")
245 errBadComparison = errors.New("incompatible types for comparison")
246 errNoComparison = errors.New("missing argument for comparison")
247 )
248
249 type kind int
250
251 const (
252 invalidKind kind = iota
253 boolKind
254 complexKind
255 intKind
256 floatKind
257 integerKind
258 stringKind
259 uintKind
260 )
261
262 func basicKind(v reflect.Value) (kind, error) {
263 switch v.Kind() {
264 case reflect.Bool:
265 return boolKind, nil
266 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
267 return intKind, nil
268 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
269 return uintKind, nil
270 case reflect.Float32, reflect.Float64:
271 return floatKind, nil
272 case reflect.Complex64, reflect.Complex128:
273 return complexKind, nil
274 case reflect.String:
275 return stringKind, nil
276 }
277 return invalidKind, errBadComparisonType
278 }
279
280 // eq evaluates the comparison a == b || a == c || ...
281 func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) {
282 v1 := reflect.ValueOf(arg1)
283 k1, err := basicKind(v1)
284 if err != nil {
285 return false, err
286 }
287 if len(arg2) == 0 {
288 return false, errNoComparison
289 }
290 for _, arg := range arg2 {
291 v2 := reflect.ValueOf(arg)
292 k2, err := basicKind(v2)
293 if err != nil {
294 return false, err
295 }
296 if k1 != k2 {
297 return false, errBadComparison
298 }
299 truth := false
300 switch k1 {
301 case boolKind:
302 truth = v1.Bool() == v2.Bool()
303 case complexKind:
304 truth = v1.Complex() == v2.Complex()
305 case floatKind:
306 truth = v1.Float() == v2.Float()
307 case intKind:
308 truth = v1.Int() == v2.Int()
309 case stringKind:
310 truth = v1.String() == v2.String()
311 case uintKind:
312 truth = v1.Uint() == v2.Uint()
313 default:
314 panic("invalid kind")
315 }
316 if truth {
317 return true, nil
318 }
319 }
320 return false, nil
321 }
322
323 // ne evaluates the comparison a != b.
324 func ne(arg1, arg2 interface{}) (bool, error) {
325 // != is the inverse of ==.
326 equal, err := eq(arg1, arg2)
327 return !equal, err
328 }
329
330 // lt evaluates the comparison a < b.
331 func lt(arg1, arg2 interface{}) (bool, error) {
332 v1 := reflect.ValueOf(arg1)
333 k1, err := basicKind(v1)
334 if err != nil {
335 return false, err
336 }
337 v2 := reflect.ValueOf(arg2)
338 k2, err := basicKind(v2)
339 if err != nil {
340 return false, err
341 }
342 if k1 != k2 {
343 return false, errBadComparison
344 }
345 truth := false
346 switch k1 {
347 case boolKind, complexKind:
348 return false, errBadComparisonType
349 case floatKind:
350 truth = v1.Float() < v2.Float()
351 case intKind:
352 truth = v1.Int() < v2.Int()
353 case stringKind:
354 truth = v1.String() < v2.String()
355 case uintKind:
356 truth = v1.Uint() < v2.Uint()
357 default:
358 panic("invalid kind")
359 }
360 return truth, nil
361 }
362
363 // le evaluates the comparison <= b.
364 func le(arg1, arg2 interface{}) (bool, error) {
365 // <= is < or ==.
366 lessThan, err := lt(arg1, arg2)
367 if lessThan || err != nil {
368 return lessThan, err
369 }
370 return eq(arg1, arg2)
371 }
372
373 // gt evaluates the comparison a > b.
374 func gt(arg1, arg2 interface{}) (bool, error) {
375 // > is the inverse of <=.
376 lessOrEqual, err := le(arg1, arg2)
377 if err != nil {
378 return false, err
379 }
380 return !lessOrEqual, nil
381 }
382
383 // ge evaluates the comparison a >= b.
384 func ge(arg1, arg2 interface{}) (bool, error) {
385 // >= is the inverse of <.
386 lessThan, err := lt(arg1, arg2)
387 if err != nil {
388 return false, err
389 }
390 return !lessThan, nil
391 }
392
393 // go1.2 added template funcs. end
......
...@@ -7,18 +7,6 @@ import ( ...@@ -7,18 +7,6 @@ import (
7 "time" 7 "time"
8 ) 8 )
9 9
10 func TestWebTime(t *testing.T) {
11 ts := "Fri, 26 Jul 2013 12:27:42 CST"
12 l, _ := time.LoadLocation("GST")
13 tt, _ := time.ParseInLocation(time.RFC1123, ts, l)
14 if ts != webTime(tt) {
15 t.Error("should be equal")
16 }
17 if "Fri, 26 Jul 2013 12:27:42 GMT" != webTime(tt.UTC()) {
18 t.Error("should be equal")
19 }
20 }
21
22 func TestSubstr(t *testing.T) { 10 func TestSubstr(t *testing.T) {
23 s := `012345` 11 s := `012345`
24 if Substr(s, 0, 2) != "01" { 12 if Substr(s, 0, 2) != "01" {
...@@ -92,16 +80,6 @@ func TestHtmlunquote(t *testing.T) { ...@@ -92,16 +80,6 @@ func TestHtmlunquote(t *testing.T) {
92 } 80 }
93 } 81 }
94 82
95 func TestInSlice(t *testing.T) {
96 sl := []string{"A", "b"}
97 if !inSlice("A", sl) {
98 t.Error("should be true")
99 }
100 if inSlice("B", sl) {
101 t.Error("should be false")
102 }
103 }
104
105 func TestParseForm(t *testing.T) { 83 func TestParseForm(t *testing.T) {
106 type user struct { 84 type user struct {
107 Id int `form:"-"` 85 Id int `form:"-"`
......
1 package utils
2
3 func InSlice(v string, sl []string) bool {
4 for _, vv := range sl {
5 if vv == v {
6 return true
7 }
8 }
9 return false
10 }
1 package utils
2
3 import (
4 "testing"
5 )
6
7 func TestInSlice(t *testing.T) {
8 sl := []string{"A", "b"}
9 if !InSlice("A", sl) {
10 t.Error("should be true")
11 }
12 if InSlice("B", sl) {
13 t.Error("should be false")
14 }
15 }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!