move utils to utils libs & func move to templatefunc
Showing
7 changed files
with
214 additions
and
250 deletions
| ... | @@ -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 | ... | ... |
| 1 | package beego | 1 | package beego |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "crypto/rand" | 4 | "errors" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | "html/template" | 6 | "html/template" |
| 7 | "net/url" | 7 | "net/url" |
| 8 | "os" | ||
| 9 | "reflect" | 8 | "reflect" |
| 10 | "regexp" | 9 | "regexp" |
| 11 | "strconv" | 10 | "strconv" |
| ... | @@ -13,14 +12,7 @@ import ( | ... | @@ -13,14 +12,7 @@ import ( |
| 13 | "time" | 12 | "time" |
| 14 | ) | 13 | ) |
| 15 | 14 | ||
| 16 | func webTime(t time.Time) string { | 15 | // Substr() return the substr from start to length |
| 17 | ftime := t.Format(time.RFC1123) | ||
| 18 | if strings.HasSuffix(ftime, "UTC") { | ||
| 19 | ftime = ftime[0:len(ftime)-3] + "GMT" | ||
| 20 | } | ||
| 21 | return ftime | ||
| 22 | } | ||
| 23 | |||
| 24 | func Substr(s string, start, length int) string { | 16 | func Substr(s string, start, length int) string { |
| 25 | bt := []rune(s) | 17 | bt := []rune(s) |
| 26 | if start < 0 { | 18 | if start < 0 { |
| ... | @@ -182,13 +174,37 @@ func Htmlunquote(src string) string { | ... | @@ -182,13 +174,37 @@ func Htmlunquote(src string) string { |
| 182 | return strings.TrimSpace(text) | 174 | return strings.TrimSpace(text) |
| 183 | } | 175 | } |
| 184 | 176 | ||
| 185 | func inSlice(v string, sl []string) bool { | 177 | // This will reference the index function local to the current blueprint: |
| 186 | for _, vv := range sl { | 178 | // UrlFor(".index") |
| 187 | if vv == v { | 179 | // ... print UrlFor("index") |
| 188 | return true | 180 | // ... print UrlFor("login") |
| 189 | } | 181 | // ... print UrlFor("login", "next","/"") |
| 190 | } | 182 | // ... print UrlFor("profile", "username","John Doe") |
| 191 | return false | 183 | // ... |
| 184 | // / | ||
| 185 | // /login | ||
| 186 | // /login?next=/ | ||
| 187 | // /user/John%20Doe | ||
| 188 | func UrlFor(endpoint string, values ...string) string { | ||
| 189 | return BeeApp.UrlFor(endpoint, values...) | ||
| 190 | } | ||
| 191 | |||
| 192 | //This can be changed to a better name | ||
| 193 | func AssetsJs(src string) template.HTML { | ||
| 194 | text := string(src) | ||
| 195 | |||
| 196 | text = "<script src=\"" + src + "\"></script>" | ||
| 197 | |||
| 198 | return template.HTML(text) | ||
| 199 | } | ||
| 200 | |||
| 201 | //This can be changed to a better name | ||
| 202 | func AssetsCss(src string) template.HTML { | ||
| 203 | text := string(src) | ||
| 204 | |||
| 205 | text = "<link href=\"" + src + "\" rel=\"stylesheet\" />" | ||
| 206 | |||
| 207 | return template.HTML(text) | ||
| 192 | } | 208 | } |
| 193 | 209 | ||
| 194 | // parse form values to struct via tag | 210 | // parse form values to struct via tag |
| ... | @@ -339,71 +355,155 @@ func isStructPtr(t reflect.Type) bool { | ... | @@ -339,71 +355,155 @@ func isStructPtr(t reflect.Type) bool { |
| 339 | return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct | 355 | return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct |
| 340 | } | 356 | } |
| 341 | 357 | ||
| 342 | func stringsToJson(str string) string { | 358 | // go1.2 added template funcs. begin |
| 343 | rs := []rune(str) | 359 | var ( |
| 344 | jsons := "" | 360 | errBadComparisonType = errors.New("invalid type for comparison") |
| 345 | for _, r := range rs { | 361 | errBadComparison = errors.New("incompatible types for comparison") |
| 346 | rint := int(r) | 362 | errNoComparison = errors.New("missing argument for comparison") |
| 347 | if rint < 128 { | 363 | ) |
| 348 | jsons += string(r) | 364 | |
| 349 | } else { | 365 | type kind int |
| 350 | jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json | 366 | |
| 351 | } | 367 | const ( |
| 368 | invalidKind kind = iota | ||
| 369 | boolKind | ||
| 370 | complexKind | ||
| 371 | intKind | ||
| 372 | floatKind | ||
| 373 | integerKind | ||
| 374 | stringKind | ||
| 375 | uintKind | ||
| 376 | ) | ||
| 377 | |||
| 378 | func basicKind(v reflect.Value) (kind, error) { | ||
| 379 | switch v.Kind() { | ||
| 380 | case reflect.Bool: | ||
| 381 | return boolKind, nil | ||
| 382 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||
| 383 | return intKind, nil | ||
| 384 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | ||
| 385 | return uintKind, nil | ||
| 386 | case reflect.Float32, reflect.Float64: | ||
| 387 | return floatKind, nil | ||
| 388 | case reflect.Complex64, reflect.Complex128: | ||
| 389 | return complexKind, nil | ||
| 390 | case reflect.String: | ||
| 391 | return stringKind, nil | ||
| 352 | } | 392 | } |
| 353 | return jsons | 393 | return invalidKind, errBadComparisonType |
| 354 | } | 394 | } |
| 355 | 395 | ||
| 356 | func FileExists(path string) (bool, error) { | 396 | // eq evaluates the comparison a == b || a == c || ... |
| 357 | _, err := os.Stat(path) | 397 | func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) { |
| 358 | if err == nil { | 398 | v1 := reflect.ValueOf(arg1) |
| 399 | k1, err := basicKind(v1) | ||
| 400 | if err != nil { | ||
| 401 | return false, err | ||
| 402 | } | ||
| 403 | if len(arg2) == 0 { | ||
| 404 | return false, errNoComparison | ||
| 405 | } | ||
| 406 | for _, arg := range arg2 { | ||
| 407 | v2 := reflect.ValueOf(arg) | ||
| 408 | k2, err := basicKind(v2) | ||
| 409 | if err != nil { | ||
| 410 | return false, err | ||
| 411 | } | ||
| 412 | if k1 != k2 { | ||
| 413 | return false, errBadComparison | ||
| 414 | } | ||
| 415 | truth := false | ||
| 416 | switch k1 { | ||
| 417 | case boolKind: | ||
| 418 | truth = v1.Bool() == v2.Bool() | ||
| 419 | case complexKind: | ||
| 420 | truth = v1.Complex() == v2.Complex() | ||
| 421 | case floatKind: | ||
| 422 | truth = v1.Float() == v2.Float() | ||
| 423 | case intKind: | ||
| 424 | truth = v1.Int() == v2.Int() | ||
| 425 | case stringKind: | ||
| 426 | truth = v1.String() == v2.String() | ||
| 427 | case uintKind: | ||
| 428 | truth = v1.Uint() == v2.Uint() | ||
| 429 | default: | ||
| 430 | panic("invalid kind") | ||
| 431 | } | ||
| 432 | if truth { | ||
| 359 | return true, nil | 433 | return true, nil |
| 360 | } | 434 | } |
| 361 | if os.IsNotExist(err) { | 435 | } |
| 362 | return false, nil | 436 | return false, nil |
| 437 | } | ||
| 438 | |||
| 439 | // ne evaluates the comparison a != b. | ||
| 440 | func ne(arg1, arg2 interface{}) (bool, error) { | ||
| 441 | // != is the inverse of ==. | ||
| 442 | equal, err := eq(arg1, arg2) | ||
| 443 | return !equal, err | ||
| 444 | } | ||
| 445 | |||
| 446 | // lt evaluates the comparison a < b. | ||
| 447 | func lt(arg1, arg2 interface{}) (bool, error) { | ||
| 448 | v1 := reflect.ValueOf(arg1) | ||
| 449 | k1, err := basicKind(v1) | ||
| 450 | if err != nil { | ||
| 451 | return false, err | ||
| 363 | } | 452 | } |
| 453 | v2 := reflect.ValueOf(arg2) | ||
| 454 | k2, err := basicKind(v2) | ||
| 455 | if err != nil { | ||
| 364 | return false, err | 456 | return false, err |
| 457 | } | ||
| 458 | if k1 != k2 { | ||
| 459 | return false, errBadComparison | ||
| 460 | } | ||
| 461 | truth := false | ||
| 462 | switch k1 { | ||
| 463 | case boolKind, complexKind: | ||
| 464 | return false, errBadComparisonType | ||
| 465 | case floatKind: | ||
| 466 | truth = v1.Float() < v2.Float() | ||
| 467 | case intKind: | ||
| 468 | truth = v1.Int() < v2.Int() | ||
| 469 | case stringKind: | ||
| 470 | truth = v1.String() < v2.String() | ||
| 471 | case uintKind: | ||
| 472 | truth = v1.Uint() < v2.Uint() | ||
| 473 | default: | ||
| 474 | panic("invalid kind") | ||
| 475 | } | ||
| 476 | return truth, nil | ||
| 365 | } | 477 | } |
| 366 | 478 | ||
| 367 | func GetRandomString(n int) string { | 479 | // le evaluates the comparison <= b. |
| 368 | const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | 480 | func le(arg1, arg2 interface{}) (bool, error) { |
| 369 | var bytes = make([]byte, n) | 481 | // <= is < or ==. |
| 370 | rand.Read(bytes) | 482 | lessThan, err := lt(arg1, arg2) |
| 371 | for i, b := range bytes { | 483 | if lessThan || err != nil { |
| 372 | bytes[i] = alphanum[b%byte(len(alphanum))] | 484 | return lessThan, err |
| 373 | } | 485 | } |
| 374 | return string(bytes) | 486 | return eq(arg1, arg2) |
| 375 | } | 487 | } |
| 376 | 488 | ||
| 377 | // This will reference the index function local to the current blueprint: | 489 | // gt evaluates the comparison a > b. |
| 378 | // UrlFor(".index") | 490 | func gt(arg1, arg2 interface{}) (bool, error) { |
| 379 | // ... print UrlFor("index") | 491 | // > is the inverse of <=. |
| 380 | // ... print UrlFor("login") | 492 | lessOrEqual, err := le(arg1, arg2) |
| 381 | // ... print UrlFor("login", "next","/"") | 493 | if err != nil { |
| 382 | // ... print UrlFor("profile", "username","John Doe") | 494 | return false, err |
| 383 | // ... | 495 | } |
| 384 | // / | 496 | return !lessOrEqual, nil |
| 385 | // /login | ||
| 386 | // /login?next=/ | ||
| 387 | // /user/John%20Doe | ||
| 388 | func UrlFor(endpoint string, values ...string) string { | ||
| 389 | return BeeApp.UrlFor(endpoint, values...) | ||
| 390 | } | 497 | } |
| 391 | 498 | ||
| 392 | 499 | // ge evaluates the comparison a >= b. | |
| 393 | //This can be changed to a better name | 500 | func ge(arg1, arg2 interface{}) (bool, error) { |
| 394 | func AssetsJs(src string) template.HTML { | 501 | // >= is the inverse of <. |
| 395 | text := string(src) | 502 | lessThan, err := lt(arg1, arg2) |
| 396 | 503 | if err != nil { | |
| 397 | text = "<script src=\""+src+"\"></script>" | 504 | return false, err |
| 398 | 505 | } | |
| 399 | return template.HTML(text) | 506 | return !lessThan, nil |
| 400 | } | 507 | } |
| 401 | 508 | ||
| 402 | //This can be changed to a better name | 509 | // go1.2 added template funcs. end |
| 403 | func AssetsCss(src string) template.HTML { | ||
| 404 | text := string(src) | ||
| 405 | |||
| 406 | text = "<link href=\""+src+"\" rel=\"stylesheet\" />" | ||
| 407 | |||
| 408 | return template.HTML(text) | ||
| 409 | } | ... | ... |
| ... | @@ -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:"-"` | ... | ... |
utils/slice.go
0 → 100644
-
Please register or sign in to post a comment