-
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 5, 2021
1 parent
bca97b6
commit f5c2669
Showing
7 changed files
with
210 additions
and
66 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
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,51 @@ | ||
package codegen | ||
|
||
import ( | ||
"github.com/gozelus/zelus_rest/cli/tpls" | ||
"html/template" | ||
"io" | ||
"strings" | ||
) | ||
|
||
type serviceInfo struct { | ||
Name string | ||
Handlers []*handler | ||
PkgName string | ||
TypesPkgName string | ||
} | ||
|
||
type ServiceGenner struct { | ||
controller *controller | ||
serviceInfo *serviceInfo | ||
} | ||
|
||
func NewServiceGener(c *controller) *ServiceGenner { | ||
s := &ServiceGenner{ | ||
controller: c, | ||
serviceInfo: initServiceInfo(c), | ||
} | ||
return s | ||
} | ||
|
||
// file 要写入的文件 | ||
// controller 要服务的 controller | ||
func (s *ServiceGenner) GenCode(file io.Writer) error { | ||
var t *template.Template | ||
var err error | ||
if t, err = template.New("service new").Parse(tpls.ServiceTpl); err != nil { | ||
return err | ||
} | ||
if err := t.Execute(file, s.serviceInfo); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func initServiceInfo(controller *controller) *serviceInfo { | ||
return &serviceInfo{ | ||
Name: controller.Name, | ||
Handlers: controller.Handlers, | ||
PkgName: strings.Split(controller.PkgName, "_")[0] + "_services", | ||
TypesPkgName: controller.TypesPkgName, | ||
} | ||
} |
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,15 @@ | ||
package tpls | ||
|
||
var ServiceTpl = `package {{ .PkgName }} | ||
type {{ .Name }}Service struct { | ||
// 以后放入要依赖度的对象 | ||
} | ||
func New{{ .Name }}Service() *{{ .Name }}Service { | ||
return &{{ .Name }}Service{} | ||
} | ||
{{ range .Handlers }} | ||
func (s *{{ $.Name }}Service) {{ .Name }}(ctx rest.Context, request *{{ $.TypesPkgName }}.{{ .RequestType }}) (*{{ $.TypesPkgName }}.{{ .ResponseType }}, error) { | ||
return nil, errors.New("no imp") | ||
} | ||
{{ end }} | ||
` |