Skip to content

Commit 19119e9

Browse files
committed
move utils to utils libs & func move to templatefunc
1 parent d603a67 commit 19119e9

File tree

7 files changed

+555
-591
lines changed

7 files changed

+555
-591
lines changed

Diff for: controller.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package beego
33
import (
44
"bytes"
55
"crypto/hmac"
6+
"crypto/rand"
67
"crypto/sha1"
78
"encoding/base64"
89
"errors"
@@ -370,7 +371,7 @@ func (c *Controller) XsrfToken() string {
370371
} else {
371372
expire = int64(XSRFExpire)
372373
}
373-
token = GetRandomString(15)
374+
token = getRandomString(15)
374375
c.SetSecureCookie(XSRFKEY, "_xsrf", token, expire)
375376
}
376377
c._xsrf_token = token
@@ -405,3 +406,14 @@ func (c *Controller) GoToFunc(funcname string) {
405406
}
406407
c.gotofunc = funcname
407408
}
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+
}

Diff for: router.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
beecontext "github.com/astaxie/beego/context"
1616
"github.com/astaxie/beego/middleware"
1717
"github.com/astaxie/beego/toolbox"
18+
"github.com/astaxie/beego/utils"
1819
)
1920

2021
const (
@@ -159,7 +160,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
159160
}
160161
comma := strings.Split(colon[0], ",")
161162
for _, m := range comma {
162-
if m == "*" || inSlice(strings.ToLower(m), HTTPMETHOD) {
163+
if m == "*" || utils.InSlice(strings.ToLower(m), HTTPMETHOD) {
163164
if val := reflectVal.MethodByName(colon[1]); val.IsValid() {
164165
methods[strings.ToLower(m)] = colon[1]
165166
} else {
@@ -272,7 +273,7 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
272273
for _, route := range p.fixrouters {
273274
if route.controllerType.Name() == controllName {
274275
var finded bool
275-
if inSlice(strings.ToLower(methodName), HTTPMETHOD) {
276+
if utils.InSlice(strings.ToLower(methodName), HTTPMETHOD) {
276277
if route.hasMethod {
277278
if m, ok := route.methods[strings.ToLower(methodName)]; ok && m != methodName {
278279
finded = false
@@ -303,7 +304,7 @@ func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string {
303304
for _, route := range p.routers {
304305
if route.controllerType.Name() == controllName {
305306
var finded bool
306-
if inSlice(strings.ToLower(methodName), HTTPMETHOD) {
307+
if utils.InSlice(strings.ToLower(methodName), HTTPMETHOD) {
307308
if route.hasMethod {
308309
if m, ok := route.methods[strings.ToLower(methodName)]; ok && m != methodName {
309310
finded = false
@@ -419,7 +420,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
419420
context.Output = beecontext.NewOutput(rw)
420421
}
421422

422-
if !inSlice(strings.ToLower(r.Method), HTTPMETHOD) {
423+
if !utils.InSlice(strings.ToLower(r.Method), HTTPMETHOD) {
423424
http.Error(w, "Method Not Allowed", 405)
424425
goto Admin
425426
}

Diff for: template.go

+3-155
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"io/ioutil"
1010
"os"
1111
"path/filepath"
12-
"reflect"
1312
"regexp"
1413
"strings"
14+
15+
"github.com/astaxie/beego/utils"
1516
)
1617

1718
var (
@@ -144,7 +145,7 @@ func getTplDeep(root, file, parent string, t *template.Template) (*template.Temp
144145
} else {
145146
fileabspath = filepath.Join(root, file)
146147
}
147-
if e, _ := FileExists(fileabspath); !e {
148+
if e := utils.FileExists(fileabspath); !e {
148149
panic("can't find template file" + file)
149150
}
150151
data, err := ioutil.ReadFile(fileabspath)
@@ -238,156 +239,3 @@ func _getTemplate(t0 *template.Template, root string, submods [][]string, others
238239
}
239240
return
240241
}
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

0 commit comments

Comments
 (0)