Skip to content

Commit 565cb85

Browse files
committed
规范代码
1 parent 4c4e34c commit 565cb85

File tree

7 files changed

+58
-36
lines changed

7 files changed

+58
-36
lines changed

Makefile

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
all: crab
22

33
FILES := $$(find . -name '*.go' | grep -vE 'vendor')
4-
SOURCE_PATH := orm validation cache server log util
5-
4+
SOURCE_PATH := orm validation cache log util
65

76
golint:
87
go get github.com/golang/lint/golint
@@ -22,12 +21,7 @@ clean:
2221
fmt:
2322
@for path in $(SOURCE_PATH); do echo "gofmt -s -l -w $$path"; gofmt -s -l -w $$path; done;
2423

25-
26-
crab:
27-
go build -o bin/$@ -ldflags '$(LDFLAGS)' ./main.go
28-
29-
30-
test:
24+
crab: lint
3125
@for path in $(SOURCE_PATH); do echo "go test ./$$path"; go test "./"$$path; done;
3226

3327

cache/cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ func TestCacheInactive(t *testing.T) {
3535
c := NewCache(2)
3636

3737
c.Add("1", &data)
38-
val := c.Get("1")
38+
c.Get("1")
3939

4040
time.Sleep(time.Second * 2)
4141

42-
c.Get("1")
42+
val := c.Get("1")
4343
if val != nil {
4444
t.Fatalf("expect not found")
4545
}

example/log.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"time"
5+
6+
"github.com/dearcode/crab/log"
7+
)
8+
9+
func main() {
10+
log.Debug("default log begin")
11+
log.Infof("%v test log", time.Now())
12+
13+
l := log.NewLogger()
14+
l.Debug("logger 1111111111")
15+
l.Info("logger 2222222222")
16+
l.Warningf("logger 33333 %v", time.Now())
17+
l.Errorf("logger color %v xxxxxx", time.Now().UnixNano())
18+
19+
//关闭颜色显示
20+
l.SetColor(false)
21+
22+
l.Errorf("logger no color %v yyyyyy", time.Now().UnixNano())
23+
log.Infof("%v default has color test log", time.Now())
24+
25+
//指定输出文件
26+
l.SetOutputFile("./vvv.log").SetRolling(true)
27+
l.Info(time.Now())
28+
29+
}

log/log_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestLog(t *testing.T) {
1212
l := NewLogger()
1313
l.Debug("logger 1111111111")
1414
l.Info("logger 2222222222")
15-
l.Warningf("logger 33333 %v", time.Now())
15+
l.Warningf("logger 33333 %v", time.Now())
1616
l.Errorf("logger color %v xxxxxx", time.Now().UnixNano())
1717
l.SetColor(false)
1818
l.Errorf("logger no color %v yyyyyy", time.Now().UnixNano())

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/dearcode/crab/http/client"
1010
"github.com/dearcode/crab/http/server"
11-
_ "github.com/dearcode/crab/server"
1211
)
1312

1413
type regexpTest struct {

validation/validation_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,21 @@ func TestAlphaNumeric(t *testing.T) {
161161
func TestMatch(t *testing.T) {
162162
valid := Validation{}
163163

164-
if valid.Match("suchuangji@gmail", regexp.MustCompile("^\\w+@\\w+\\.\\w+$"), "match").Ok {
164+
if valid.Match("suchuangji@gmail", regexp.MustCompile(`^\w+@\w+\.\w+$`), "match").Ok {
165165
t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be false")
166166
}
167-
if !valid.Match("[email protected]", regexp.MustCompile("^\\w+@\\w+\\.\\w+$"), "match").Ok {
167+
if !valid.Match("[email protected]", regexp.MustCompile(`^\w+@\w+\.\w+$`), "match").Ok {
168168
t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be true")
169169
}
170170
}
171171

172172
func TestNoMatch(t *testing.T) {
173173
valid := Validation{}
174174

175-
if valid.NoMatch("123@gmail", regexp.MustCompile("[^\\w\\d]"), "nomatch").Ok {
175+
if valid.NoMatch("123@gmail", regexp.MustCompile(`[^\w\d]`), "nomatch").Ok {
176176
t.Error("\"123@gmail\" not match \"[^\\w\\d]\" should be false")
177177
}
178-
if !valid.NoMatch("123gmail", regexp.MustCompile("[^\\w\\d]"), "match").Ok {
178+
if !valid.NoMatch("123gmail", regexp.MustCompile(`[^\w\d]`), "match").Ok {
179179
t.Error("\"123@gmail\" not match \"[^\\w\\d@]\" should be true")
180180
}
181181
}
@@ -351,7 +351,7 @@ func TestRecursiveValid(t *testing.T) {
351351
}
352352
valid := Validation{}
353353

354-
u := Account{Password: "abc123_", U: User{}}
354+
u := Account{Password: "", U: User{}}
355355
b, _ := valid.Valid(u)
356356
if b {
357357
t.Error("validation should not be passed")

validation/validators.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (r Required) IsSatisfied(obj interface{}) bool {
131131

132132
// DefaultMessage return the default error message
133133
func (r Required) DefaultMessage() string {
134-
return fmt.Sprint(MessageTmpls["Required"])
134+
return MessageTmpls["Required"]
135135
}
136136

137137
// GetKey return the r.Key
@@ -350,7 +350,7 @@ func (a Alpha) IsSatisfied(obj interface{}) bool {
350350

351351
// DefaultMessage return the default Length error message
352352
func (a Alpha) DefaultMessage() string {
353-
return fmt.Sprint(MessageTmpls["Alpha"])
353+
return MessageTmpls["Alpha"]
354354
}
355355

356356
// GetKey return the m.Key
@@ -383,7 +383,7 @@ func (n Numeric) IsSatisfied(obj interface{}) bool {
383383

384384
// DefaultMessage return the default Length error message
385385
func (n Numeric) DefaultMessage() string {
386-
return fmt.Sprint(MessageTmpls["Numeric"])
386+
return MessageTmpls["Numeric"]
387387
}
388388

389389
// GetKey return the n.Key
@@ -416,7 +416,7 @@ func (a AlphaNumeric) IsSatisfied(obj interface{}) bool {
416416

417417
// DefaultMessage return the default Length error message
418418
func (a AlphaNumeric) DefaultMessage() string {
419-
return fmt.Sprint(MessageTmpls["AlphaNumeric"])
419+
return MessageTmpls["AlphaNumeric"]
420420
}
421421

422422
// GetKey return the a.Key
@@ -481,7 +481,7 @@ func (n NoMatch) GetLimitValue() interface{} {
481481
return n.Regexp.String()
482482
}
483483

484-
var alphaDashPattern = regexp.MustCompile("[^\\d\\w-_]")
484+
var alphaDashPattern = regexp.MustCompile(`[^\d\w-_]`)
485485

486486
// AlphaDash check not Alpha
487487
type AlphaDash struct {
@@ -491,7 +491,7 @@ type AlphaDash struct {
491491

492492
// DefaultMessage return the default AlphaDash error message
493493
func (a AlphaDash) DefaultMessage() string {
494-
return fmt.Sprint(MessageTmpls["AlphaDash"])
494+
return MessageTmpls["AlphaDash"]
495495
}
496496

497497
// GetKey return the n.Key
@@ -504,7 +504,7 @@ func (a AlphaDash) GetLimitValue() interface{} {
504504
return nil
505505
}
506506

507-
var emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?")
507+
var emailPattern = regexp.MustCompile(`[\w!#$%&'*+/=?^_{|}~-]+(?:\.[\w!#$%&'*+/=?^_{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[a-zA-Z0-9](?:[\w-]*[\w])?`)
508508

509509
// Email check struct
510510
type Email struct {
@@ -514,7 +514,7 @@ type Email struct {
514514

515515
// DefaultMessage return the default Email error message
516516
func (e Email) DefaultMessage() string {
517-
return fmt.Sprint(MessageTmpls["Email"])
517+
return MessageTmpls["Email"]
518518
}
519519

520520
// GetKey return the n.Key
@@ -527,7 +527,7 @@ func (e Email) GetLimitValue() interface{} {
527527
return nil
528528
}
529529

530-
var ipPattern = regexp.MustCompile("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$")
530+
var ipPattern = regexp.MustCompile(`^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$`)
531531

532532
// IP check struct
533533
type IP struct {
@@ -537,7 +537,7 @@ type IP struct {
537537

538538
// DefaultMessage return the default IP error message
539539
func (i IP) DefaultMessage() string {
540-
return fmt.Sprint(MessageTmpls["IP"])
540+
return MessageTmpls["IP"]
541541
}
542542

543543
// GetKey return the i.Key
@@ -550,7 +550,7 @@ func (i IP) GetLimitValue() interface{} {
550550
return nil
551551
}
552552

553-
var base64Pattern = regexp.MustCompile("^(?:[A-Za-z0-99+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$")
553+
var base64Pattern = regexp.MustCompile(`^(?:[A-Za-z0-99+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`)
554554

555555
// Base64 check struct
556556
type Base64 struct {
@@ -560,7 +560,7 @@ type Base64 struct {
560560

561561
// DefaultMessage return the default Base64 error message
562562
func (b Base64) DefaultMessage() string {
563-
return fmt.Sprint(MessageTmpls["Base64"])
563+
return MessageTmpls["Base64"]
564564
}
565565

566566
// GetKey return the b.Key
@@ -574,7 +574,7 @@ func (b Base64) GetLimitValue() interface{} {
574574
}
575575

576576
// just for chinese mobile phone number
577-
var mobilePattern = regexp.MustCompile("^((\\+86)|(86))?(1(([35][0-9])|[8][0-9]|[7][06789]|[4][579]))\\d{8}$")
577+
var mobilePattern = regexp.MustCompile(`^((\+86)|(86))?(1(([35][0-9])|[8][0-9]|[7][06789]|[4][579]))\d{8}$`)
578578

579579
// Mobile check struct
580580
type Mobile struct {
@@ -584,7 +584,7 @@ type Mobile struct {
584584

585585
// DefaultMessage return the default Mobile error message
586586
func (m Mobile) DefaultMessage() string {
587-
return fmt.Sprint(MessageTmpls["Mobile"])
587+
return MessageTmpls["Mobile"]
588588
}
589589

590590
// GetKey return the m.Key
@@ -598,7 +598,7 @@ func (m Mobile) GetLimitValue() interface{} {
598598
}
599599

600600
// just for chinese telephone number
601-
var telPattern = regexp.MustCompile("^(0\\d{2,3}(\\-)?)?\\d{7,8}$")
601+
var telPattern = regexp.MustCompile(`^(0\d{2,3}(\-)?)?\d{7,8}$`)
602602

603603
// Tel check telephone struct
604604
type Tel struct {
@@ -608,7 +608,7 @@ type Tel struct {
608608

609609
// DefaultMessage return the default Tel error message
610610
func (t Tel) DefaultMessage() string {
611-
return fmt.Sprint(MessageTmpls["Tel"])
611+
return MessageTmpls["Tel"]
612612
}
613613

614614
// GetKey return the t.Key
@@ -635,7 +635,7 @@ func (p Phone) IsSatisfied(obj interface{}) bool {
635635

636636
// DefaultMessage return the default Phone error message
637637
func (p Phone) DefaultMessage() string {
638-
return fmt.Sprint(MessageTmpls["Phone"])
638+
return MessageTmpls["Phone"]
639639
}
640640

641641
// GetKey return the p.Key
@@ -649,7 +649,7 @@ func (p Phone) GetLimitValue() interface{} {
649649
}
650650

651651
// just for chinese zipcode
652-
var zipCodePattern = regexp.MustCompile("^[1-9]\\d{5}$")
652+
var zipCodePattern = regexp.MustCompile(`^[1-9]\d{5}$`)
653653

654654
// ZipCode check the zip struct
655655
type ZipCode struct {
@@ -659,7 +659,7 @@ type ZipCode struct {
659659

660660
// DefaultMessage return the default Zip error message
661661
func (z ZipCode) DefaultMessage() string {
662-
return fmt.Sprint(MessageTmpls["ZipCode"])
662+
return MessageTmpls["ZipCode"]
663663
}
664664

665665
// GetKey return the z.Key

0 commit comments

Comments
 (0)