Skip to content

Commit

Permalink
mir: add defualt unimplemented servant in generate code
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Jan 31, 2021
1 parent fbf5ec6 commit 57f98c1
Show file tree
Hide file tree
Showing 216 changed files with 3,994 additions and 1,075 deletions.
19 changes: 19 additions & 0 deletions internal/generator/templates/chi_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,22 @@ func Register{{.TypeName}}Servant(r chi.Router, s {{.TypeName}}) {
router.Connect("{{.Path}}", s.{{.MethodName}})
router.Trace("{{.Path}}", s.{{.MethodName}}){{end}}
{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func (*{{$unimplementedServant}}){{.Chain}}() chi.Middlewares {
return nil
}

{{end}}
{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusNotImplemented)
rw.Write([]byte("method {{.MethodName}} not implemented"))
}

{{end}}
22 changes: 22 additions & 0 deletions internal/generator/templates/echo_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package {{ .PkgName }}

import (
"net/http"

echo "{{if notEmptyStr .EngineInfo.PkgName }}{{ .EngineInfo.PkgName }}{{else}}github.com/labstack/echo/v4{{end}}"
)

Expand All @@ -26,3 +28,23 @@ func Register{{.TypeName}}Servant(e *echo.Echo, s {{.TypeName}}) {
// register routes info to router
{{range .Fields}}{{if notHttpAny .HttpMethod }} g.Add("{{.HttpMethod}}", "{{.Path}}", s.{{.MethodName}}){{else}} g.Any("{{.Path}}", s.{{.MethodName}}){{end}}
{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func (*{{$unimplementedServant}}){{.Chain}}() []echo.MiddlewareFunc {
return nil
}

{{end}}
{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(c echo.Context) error {
c.String(http.StatusNotImplemented, "method {{.MethodName}} not implemented")
return nil
}

{{end}}

23 changes: 23 additions & 0 deletions internal/generator/templates/fiber_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
package {{ .PkgName }}

import (
"net/http"
{{if notEmptyStr .Chain }}
"github.com/gofiber/fiber/middleware"
{{end}}
fiber "{{if notEmptyStr .EngineInfo.PkgName }}{{ .EngineInfo.PkgName }}{{else}}github.com/gofiber/fiber{{end}}"
)

Expand All @@ -26,3 +30,22 @@ func Register{{.TypeName}}Servant(app *fiber.App, s {{.TypeName}}) {
// register routes info to router
{{range .Fields}}{{if eq .HttpMethod "GET" }} router.Get("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "POST"}} router.Post("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "PUT"}} router.Put("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "DELETE"}} router.Delete("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "HEAD"}} router.Head("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "OPTIONS"}} router.Options("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "TRACE"}} router.Trace("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "PATCH"}} router.Patch("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "CONNECT"}} router.Connect("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "ANY" }} router.All("{{.Path}}", s.{{.MethodName}}){{end}}
{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func (*{{$unimplementedServant}}){{.Chain}}() []interface{} {
return []interface{}{middleware.Logger()}
}

{{end}}
{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(c *fiber.Ctx) {
c.Status(http.StatusNotImplemented)
c.Write([]byte("method {{.MethodName}} not implemented"))
}

{{end}}
25 changes: 25 additions & 0 deletions internal/generator/templates/fiber_iface_v2.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
package {{ .PkgName }}

import (
"net/http"
{{if notEmptyStr .Chain }}
"github.com/gofiber/fiber/v2/middleware/logger"
{{end}}
fiber "{{if notEmptyStr .EngineInfo.PkgName }}{{ .EngineInfo.PkgName }}{{else}}github.com/gofiber/fiber/v2{{end}}"
)

Expand All @@ -26,3 +30,24 @@ func Register{{.TypeName}}Servant(app *fiber.App, s {{.TypeName}}) {
// register routes info to router
{{range .Fields}}{{if eq .HttpMethod "GET" }} router.Get("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "POST"}} router.Post("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "PUT"}} router.Put("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "DELETE"}} router.Delete("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "HEAD"}} router.Head("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "OPTIONS"}} router.Options("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "TRACE"}} router.Trace("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "PATCH"}} router.Patch("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "CONNECT"}} router.Connect("{{.Path}}", s.{{.MethodName}}){{else if eq .HttpMethod "ANY" }} router.All("{{.Path}}", s.{{.MethodName}}){{end}}
{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func (*{{$unimplementedServant}}){{.Chain}}() []interface{} {
return []interface{}{logger.New()}
}

{{end}}
{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(c *fiber.Ctx) error {
c.Status(http.StatusNotImplemented)
c.Write([]byte("method {{.MethodName}} not implemented"))
return nil
}

{{end}}

20 changes: 20 additions & 0 deletions internal/generator/templates/gin_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package {{ .PkgName }}

import (
"net/http"

gin "{{if notEmptyStr .EngineInfo.PkgName }}{{ .EngineInfo.PkgName }}{{else}}github.com/gin-gonic/gin{{end}}"
)

Expand All @@ -26,3 +28,21 @@ func Register{{.TypeName}}Servant(e *gin.Engine, s {{.TypeName}}) {
// register routes info to router
{{range .Fields}}{{if notHttpAny .HttpMethod }} router.Handle("{{.HttpMethod}}", "{{.Path}}", s.{{.MethodName}}){{else}} router.Any("{{.Path}}", s.{{.MethodName}}){{end}}
{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func (*{{$unimplementedServant}}){{.Chain}}() gin.HandlersChain {
return nil
}

{{end}}
{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(c *gin.Context) {
c.String(http.StatusNotImplemented, "method {{.MethodName}} not implemented")
}

{{end}}
13 changes: 13 additions & 0 deletions internal/generator/templates/httprouter_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ func Register{{.TypeName}}Servant(r *httprouter.Router, s {{.TypeName}}) {
r.Handle(http.MethodConnect, "{{joinPath $.Group .Path}}", s.{{.MethodName}})
r.Handle(http.MethodTrace, "{{joinPath $.Group .Path}}", s.{{.MethodName}}){{end}}
{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(rw http.ResponseWriter, r *http.Request, params httprouter.Params) {
rw.WriteHeader(http.StatusNotImplemented)
rw.Write([]byte("method {{.MethodName}} not implemented"))
}

{{end}}
21 changes: 21 additions & 0 deletions internal/generator/templates/iris_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package {{ .PkgName }}

import (
"net/http"

"{{if notEmptyStr .EngineInfo.PkgName }}{{ .EngineInfo.PkgName }}/context{{else}}github.com/kataras/iris/v12/context{{end}}"

iris "{{if notEmptyStr .EngineInfo.PkgName }}{{ .EngineInfo.PkgName }}{{else}}github.com/kataras/iris/v12{{end}}"
Expand All @@ -28,3 +30,22 @@ func Register{{.TypeName}}Servant(app *iris.Application, s {{.TypeName}}) {
// register routes info to party
{{range .Fields}}{{if notHttpAny .HttpMethod }} p.Handle("{{.HttpMethod}}", "{{.Path}}", s.{{.MethodName}}){{else}} p.Any("{{.Path}}", s.{{.MethodName}}){{end}}
{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func (*{{$unimplementedServant}}){{.Chain}}() context.Handlers {
return nil
}

{{end}}
{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(c context.Context) {
c.StatusCode(http.StatusNotImplemented)
c.WriteString("method {{.MethodName}} not implemented")
}

{{end}}
21 changes: 21 additions & 0 deletions internal/generator/templates/macaron_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package {{ .PkgName }}

import (
"net/http"

macaron "{{if notEmptyStr .EngineInfo.PkgName }}{{ .EngineInfo.PkgName }}{{else}}gopkg.in/macaron.v1{{end}}"
)

Expand Down Expand Up @@ -31,3 +33,22 @@ func Register{{.TypeName}}Servant(m *macaron.Macaron, s {{.TypeName}}) {
}{{end}}
{{range .Fields}}{{if notHttpAny .HttpMethod }} m.Handle("{{.HttpMethod}}", "{{.Path}}", []macaron.Handler{s.{{.MethodName}}}){{else}} m.Any("{{.Path}}", []macaron.Handler{s.{{.MethodName}}}){{end}}
{{end}}{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func (*{{$unimplementedServant}}){{.Chain}}() []macaron.Handler {
return nil
}

{{end}}
{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(c *macaron.Context) {
c.Resp.WriteHeader(http.StatusNotImplemented)
c.Resp.Write([]byte("method {{.MethodName}} not implemented"))
}

{{end}}
19 changes: 19 additions & 0 deletions internal/generator/templates/mux_iface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ func Register{{.TypeName}}Servant(r *mux.Router, s {{.TypeName}}) {
Host("{{.Host}}"){{end}}{{if valideQuery .Queries}}.
Queries({{inflateQuery .Queries}}){{end}}{{end}}
{{end}}}

{{ $unimplementedServant := print "Unimplemented" .TypeName "Servant" }}
// {{$unimplementedServant}} can be embedded to have forward compatible implementations.
type {{$unimplementedServant}} struct {
}

{{if notEmptyStr .Chain }}
func (*{{$unimplementedServant}}){{.Chain}}() []mux.MiddlewareFunc {
return nil
}

{{end}}
{{range .Fields}}
func (*{{$unimplementedServant}}){{.MethodName}}(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusNotImplemented)
rw.Write([]byte("method {{.MethodName}} not implemented"))
}

{{end}}
2 changes: 0 additions & 2 deletions mirc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ check-debug-all:
./mirc new --mir ../../ --style $$target --dst $$target; \
cd $$target; \
make mod-tidy; \
make generate; \
cd -; \
echo ""; \
done
Expand All @@ -70,7 +69,6 @@ check-release-all:
./mirc new --style $$target --dst $$target; \
cd $$target; \
make mod-tidy; \
make generate; \
cd -; \
echo ""; \
done
Expand Down
8 changes: 8 additions & 0 deletions mirc/build/go2tmpl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

GOFILES=$(find . -type f)

for filename in $GOFILES
do
mv ${filename} ${filename}.tmpl
done
33 changes: 14 additions & 19 deletions mirc/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
package cmd

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/alimy/mir/mirc/v2/cmd/templates"
"github.com/spf13/cobra"
)

var (
dstPath string
pkgName string
style string
style []string
mirPkgName string
)

Expand All @@ -30,7 +33,7 @@ func init() {
// parse flags for agentCmd
newCmd.Flags().StringVarP(&dstPath, "dst", "d", ".", "genereted destination target directory")
newCmd.Flags().StringVarP(&pkgName, "pkg", "p", "github.com/alimy/mir-example", "project's package name")
newCmd.Flags().StringVarP(&style, "style", "s", "gin", "generated engine style eg: gin,chi,mux,echo,iris,fiber,fiber-v2,macaron,httprouter")
newCmd.Flags().StringSliceVarP(&style, "style", "s", []string{"gin"}, "generated engine style eg: gin,chi,mux,echo,iris,fiber,fiber-v2,macaron,httprouter")
newCmd.Flags().StringVar(&mirPkgName, "mir", "", "mir replace package name or place")

// register agentCmd as sub-command
Expand All @@ -55,35 +58,29 @@ func newRun(_cmd *cobra.Command, _args []string) {
log.Fatal(err)
}
}

tmpls, exist := tmplFiles[style]
if !exist {
log.Fatal("not exist style engine")
}

ctx := &tmplCtx{
ctx := &templates.TmplCtx{
PkgName: pkgName,
MirPkgName: mirPkgName,
}
if err = genProject(ctx, path, tmpls); err != nil {
if err = genProject(ctx, path, style); err != nil {
log.Fatal(err)
}
}

func genProject(ctx *tmplCtx, dstPath string, tmpls map[string]tmplInfo) error {
func genProject(ctx *templates.TmplCtx, dstPath string, style []string) error {
var (
err error
filePath, dirPath string
file *os.File
)

t, err := newTemplate()
t, err := templates.NewTemplate(style)
if err != nil {
return err
return fmt.Errorf("not exist style for %s: %w", strings.Join(style, ":"), err)
}

for fileName, assetInfo := range tmpls {
filePath = filepath.Join(dstPath, fileName)
for _, tmpl := range t.Templates() {
filePath = filepath.Join(dstPath, tmpl.Name())
dirPath = filepath.Dir(filePath)
if err = os.MkdirAll(dirPath, 0755); err != nil {
break
Expand All @@ -92,12 +89,10 @@ func genProject(ctx *tmplCtx, dstPath string, tmpls map[string]tmplInfo) error {
if err != nil {
break
}
if err = t.ExecuteTemplate(file, assetInfo.name, ctx); err != nil {
break
}
if err = file.Close(); err != nil {
if err = tmpl.Execute(file, ctx); err != nil {
break
}
file.Close()
}
return err
}
7 changes: 2 additions & 5 deletions mirc/cmd/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"strings"

"github.com/alimy/mir/mirc/v2/cmd/templates"
"github.com/spf13/cobra"
)

Expand All @@ -23,9 +24,5 @@ func init() {

// stylesRun run styles command
func stylesRun(_cmd *cobra.Command, _args []string) {
styleNames := make([]string, 0, len(tmplFiles))
for name := range tmplFiles {
styleNames = append(styleNames, name)
}
fmt.Println(strings.Join(styleNames, " "))
fmt.Println(strings.Join(templates.Styles(), " "))
}
Loading

0 comments on commit 57f98c1

Please sign in to comment.