-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rabbbit
committed
Jan 4, 2021
1 parent
6c853de
commit a323f41
Showing
15 changed files
with
285 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package actions | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fatih/color" | ||
"github.com/gozelus/zelus_rest/cli/codegen" | ||
"github.com/urfave/cli" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func mergeApiFile(apiFilePath string) (io.Reader, error) { | ||
var err error | ||
var apiFileMerge io.Reader | ||
apiFile, err := os.Open(apiFilePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if apiFileMerge, err = codegen.NewApiGenner(apiFile).Merge(); err != nil { | ||
return nil, err | ||
} | ||
return apiFileMerge, nil | ||
} | ||
|
||
func GenApis(ctx *cli.Context) error { | ||
dir := strings.TrimSpace(ctx.String(flagDir)) | ||
apiFilePath := strings.TrimSpace(ctx.String(flagFile)) | ||
|
||
// apiFileMerge 需要重新读取一次 | ||
var apiFileMergeCopy io.Reader | ||
var apiFileMerge io.Reader | ||
var err error | ||
|
||
if apiFileMergeCopy, err = mergeApiFile(apiFilePath); err != nil { | ||
return err | ||
} | ||
if apiFileMerge, err = mergeApiFile(apiFilePath); err != nil { | ||
return err | ||
} | ||
|
||
varsFile, err := os.Create(filepath.Join(dir, "vars.go")) | ||
fmt.Println(color.GreenString("%s create", filepath.Join(dir, "vars.go"))) | ||
if err != nil { | ||
return err | ||
} | ||
if err = codegen.NewTypesInfo(varsFile, apiFileMerge, "api").GenCode(); err != nil { | ||
return err | ||
} | ||
if err := codegen.NewControllerGenner(apiFileMergeCopy, dir, "api").GenCode(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package tpls | ||
|
||
var errorsStatusCodeTpl = ` | ||
package apiErrors | ||
import "fmt" | ||
var BadRequest = &StatusError{ | ||
Err: fmt.Errorf("bad request"), | ||
Message: "BadRequest", | ||
Code: 400, | ||
} | ||
` | ||
var errorsStatusErrorTpl = `package apiErrors | ||
import ( | ||
"fmt" | ||
rest "github.com/gozelus/zelus_rest" | ||
) | ||
type StatusError struct { | ||
Err error | ||
Message string | ||
Code int | ||
Reason string | ||
} | ||
func (s *StatusError) Error() string { | ||
return s.Message | ||
} | ||
func (s *StatusError) GetCode() int { | ||
return s.Code | ||
} | ||
func (s *StatusError) GetMessage() string { | ||
if len(s.Reason) > 0 { | ||
return fmt.Sprintf("%s for %s", s.Message, s.Reason) | ||
} | ||
return s.Message | ||
} | ||
func (s *StatusError) WithReason(reason string) *StatusError { | ||
s2 := &StatusError{} | ||
s2.Message = s.Message | ||
s2.Err = s.Err | ||
s2.Message = s.Message | ||
s2.Code = s.Code | ||
s2.Reason = reason | ||
return s2 | ||
} | ||
var _ rest.StatusError = &StatusError{} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,35 @@ | ||
package tpls | ||
|
||
var ControllerTpl = `package {{ .PkgName }} | ||
type {{ .Name }}Service interface { | ||
import ( | ||
"github.com/gozelus/zelus_rest" | ||
) | ||
type {{ .Name }}Service interface { {{ range .Handlers }} | ||
{{ .Name }}(ctx rest.Context, req *{{ $.TypesPkgName }}.{{ .RequestType }}) (*{{ $.TypesPkgName }}.{{ .ResponseType }}, error) {{ end }} | ||
} | ||
type {{ .Name }}Controller struct { | ||
service {{ .Name }}Service | ||
} | ||
func New{{ .Name }}Controller(service {{ .Name }}Service) *{{ .Name }}Controller { | ||
return &{{ .Name }}Controller{service : service} | ||
} | ||
{{ range .Handlers }} | ||
func (c *{{ $.Name }}Controller) {{ .Name }}(ctx rest.Context) { | ||
res := &{{ $.TypesPkgName }}.{{ .ResponseType }}{} | ||
req := &{{ $.TypesPkgName }}.{{ .RequestType }}{} | ||
var err error | ||
if err := ctx.{{if eq .Method "GET" }}JSONBodyBind{{ else }}JSONQueryBind{{ end }}(req); err != nil { | ||
ctx.RenderErrorJSON(nil, apiErrors.BadRequest.WithReason(err.Error())) | ||
return | ||
} | ||
if res, err = c.service.{{ .Name }}(ctx, req);err!=nil{ | ||
ctx.RenderErrorJSON(nil, err) | ||
return | ||
} | ||
ctx.RenderOkJSON(res) | ||
} | ||
{{ end }} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.