From 272271f588d265892421d00480c5af7e3d804b82 Mon Sep 17 00:00:00 2001 From: ysqi Date: Wed, 27 Apr 2016 23:57:22 +0800 Subject: [PATCH 1/5] Change comment router file info --- parser.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/parser.go b/parser.go index 46d02320..b19b2705 100644 --- a/parser.go +++ b/parser.go @@ -24,6 +24,7 @@ import ( "io/ioutil" "os" "path" + "path/filepath" "sort" "strings" @@ -56,7 +57,7 @@ func init() { func parserPkg(pkgRealpath, pkgpath string) error { rep := strings.NewReplacer("/", "_", ".", "_") - commentFilename = coomentPrefix + rep.Replace(pkgpath) + ".go" + commentFilename = coomentPrefix + rep.Replace(strings.Replace(pkgRealpath, AppPath, "", -1)) + ".go" if !compareFile(pkgRealpath) { Info(pkgRealpath + " no changed") return nil @@ -86,7 +87,7 @@ func parserPkg(pkgRealpath, pkgpath string) error { } } } - genRouterCode() + genRouterCode(pkgRealpath) savetoFile(pkgRealpath) return nil } @@ -129,8 +130,8 @@ func parserComments(comments *ast.CommentGroup, funcName, controllerName, pkgpat return nil } -func genRouterCode() { - os.Mkdir(path.Join(AppPath, "routers"), 0755) +func genRouterCode(pkgRealpath string) { + os.Mkdir(getRouterDir(pkgRealpath), 0755) Info("generate router from comments") var ( globalinfo string @@ -172,7 +173,7 @@ func genRouterCode() { } } if globalinfo != "" { - f, err := os.Create(path.Join(AppPath, "routers", commentFilename)) + f, err := os.Create(path.Join(getRouterDir(pkgRealpath), commentFilename)) if err != nil { panic(err) } @@ -182,7 +183,7 @@ func genRouterCode() { } func compareFile(pkgRealpath string) bool { - if !utils.FileExists(path.Join(AppPath, "routers", commentFilename)) { + if !utils.FileExists(path.Join(getRouterDir(pkgRealpath), commentFilename)) { return true } if utils.FileExists(lastupdateFilename) { @@ -229,3 +230,18 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) { } return lastupdate, nil } + +func getRouterDir(pkgRealpath string) string { + dir := filepath.Dir(pkgRealpath) + for { + d := path.Join(dir, "routers") + if dir == AppPath { + return d + } + if utils.FileExists(d) { + return d + } + // Parent dir. + dir = filepath.Dir(dir) + } +} From 2a2b433e1987509076a1c7713c8954589d55778b Mon Sep 17 00:00:00 2001 From: ysqi Date: Wed, 27 Apr 2016 23:57:36 +0800 Subject: [PATCH 2/5] go fmt --- namespace_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/namespace_test.go b/namespace_test.go index a92ae3ef..fc02b5fb 100644 --- a/namespace_test.go +++ b/namespace_test.go @@ -61,8 +61,8 @@ func TestNamespaceNest(t *testing.T) { ns.Namespace( NewNamespace("/admin"). Get("/order", func(ctx *context.Context) { - ctx.Output.Body([]byte("order")) - }), + ctx.Output.Body([]byte("order")) + }), ) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) @@ -79,8 +79,8 @@ func TestNamespaceNestParam(t *testing.T) { ns.Namespace( NewNamespace("/admin"). Get("/order/:id", func(ctx *context.Context) { - ctx.Output.Body([]byte(ctx.Input.Param(":id"))) - }), + ctx.Output.Body([]byte(ctx.Input.Param(":id"))) + }), ) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) @@ -124,8 +124,8 @@ func TestNamespaceFilter(t *testing.T) { ctx.Output.Body([]byte("this is Filter")) }). Get("/user/:id", func(ctx *context.Context) { - ctx.Output.Body([]byte(ctx.Input.Param(":id"))) - }) + ctx.Output.Body([]byte(ctx.Input.Param(":id"))) + }) AddNamespace(ns) BeeApp.Handlers.ServeHTTP(w, r) if w.Body.String() != "this is Filter" { From 77ff15ee33be54a36f0a36054220ad902c1622c2 Mon Sep 17 00:00:00 2001 From: ysqi Date: Thu, 5 May 2016 19:26:03 +0800 Subject: [PATCH 3/5] Implement Error interface for validation error --- validation/validation.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/validation/validation.go b/validation/validation.go index 2b020aa8..489dfa5e 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -73,6 +73,10 @@ func (e *Error) String() string { return e.Message } +// Implement Error interface. +// Return e.String() +func (e *Error) Error() string { return e.String() } + // Result is returned from every validation method. // It provides an indication of success, and a pointer to the Error (if any). type Result struct { From 8210fd12d17ce5e5e9e2d88f2d718b1d5d482855 Mon Sep 17 00:00:00 2001 From: ysqi Date: Thu, 5 May 2016 19:28:09 +0800 Subject: [PATCH 4/5] Fixed router fileName error in window --- parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.go b/parser.go index b19b2705..4d5b5653 100644 --- a/parser.go +++ b/parser.go @@ -56,7 +56,7 @@ func init() { } func parserPkg(pkgRealpath, pkgpath string) error { - rep := strings.NewReplacer("/", "_", ".", "_") + rep := strings.NewReplacer("\\", "_", "/", "_", ".", "_") commentFilename = coomentPrefix + rep.Replace(strings.Replace(pkgRealpath, AppPath, "", -1)) + ".go" if !compareFile(pkgRealpath) { Info(pkgRealpath + " no changed") From 7ceff43db6a11e426726ed1c66afcad44388f44b Mon Sep 17 00:00:00 2001 From: ysqi Date: Fri, 6 May 2016 13:26:48 +0800 Subject: [PATCH 5/5] Fixed error in window os --- parser.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/parser.go b/parser.go index fe719dce..3bf3cf6b 100644 --- a/parser.go +++ b/parser.go @@ -23,7 +23,6 @@ import ( "go/token" "io/ioutil" "os" - "path" "path/filepath" "sort" "strings" @@ -58,7 +57,8 @@ func init() { func parserPkg(pkgRealpath, pkgpath string) error { rep := strings.NewReplacer("\\", "_", "/", "_", ".", "_") - commentFilename = coomentPrefix + rep.Replace(strings.Replace(pkgRealpath, AppPath, "", -1)) + ".go" + commentFilename, _ = filepath.Rel(AppPath, pkgRealpath) + commentFilename = coomentPrefix + rep.Replace(commentFilename) + ".go" if !compareFile(pkgRealpath) { logs.Info(pkgRealpath + " no changed") return nil @@ -174,7 +174,7 @@ func genRouterCode(pkgRealpath string) { } } if globalinfo != "" { - f, err := os.Create(path.Join(getRouterDir(pkgRealpath), commentFilename)) + f, err := os.Create(filepath.Join(getRouterDir(pkgRealpath), commentFilename)) if err != nil { panic(err) } @@ -184,7 +184,7 @@ func genRouterCode(pkgRealpath string) { } func compareFile(pkgRealpath string) bool { - if !utils.FileExists(path.Join(getRouterDir(pkgRealpath), commentFilename)) { + if !utils.FileExists(filepath.Join(getRouterDir(pkgRealpath), commentFilename)) { return true } if utils.FileExists(lastupdateFilename) { @@ -235,11 +235,12 @@ func getpathTime(pkgRealpath string) (lastupdate int64, err error) { func getRouterDir(pkgRealpath string) string { dir := filepath.Dir(pkgRealpath) for { - d := path.Join(dir, "routers") - if dir == AppPath { + d := filepath.Join(dir, "routers") + if utils.FileExists(d) { return d } - if utils.FileExists(d) { + + if r, _ := filepath.Rel(dir, AppPath); r == "." { return d } // Parent dir.