Skip to content

Commit

Permalink
Merge branch 'openapi-doc-validate' of github.com:ninjashixuan/gf int…
Browse files Browse the repository at this point in the history
…o openapi-doc-validate
  • Loading branch information
ninjashixuan committed Nov 21, 2024
2 parents 56a67ae + 7481dc9 commit b3cdcd4
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 48 deletions.
24 changes: 15 additions & 9 deletions cmd/gf/internal/cmd/cmd_doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package cmd

import (
"context"
"fmt"
"io"
"net/http"
"os"
Expand All @@ -22,8 +23,12 @@ import (
)

const (
GitName = "gf-site"
BranchName = "gh-pages"

SiteFileName = GitName + "-" + BranchName
// DocURL is the download address of the document
DocURL = "https://github.com/gogf/gf/archive/refs/heads/gh-pages.zip"
DocURL = "https://github.com/gogf/" + GitName + "/archive/refs/heads/" + BranchName + ".zip"
)

var (
Expand Down Expand Up @@ -70,12 +75,13 @@ func (c cDoc) Index(ctx context.Context, in cDocInput) (out *cDocOutput, err err
mlog.Print("Failed to download document:", err)
return
}
s := g.Server()
s.SetServerRoot(docs.DocDir)
s.SetPort(in.Port)
s.SetDumpRouterMap(false)
mlog.Printf("Access address http://127.0.0.1:%d", in.Port)
s.Run()

http.Handle("/", http.FileServer(http.Dir(docs.DocDir)))
mlog.Printf("Access address http://127.0.0.1:%d in %s", in.Port, docs.DocDir)
err = http.ListenAndServe(fmt.Sprintf(":%d", in.Port), nil)
if err != nil {
return nil, err
}
return
}

Expand All @@ -89,7 +95,7 @@ type DocSetting struct {

// NewDocSetting new DocSetting
func NewDocSetting(ctx context.Context, in cDocInput) *DocSetting {
fileName := "gf-doc-md.zip"
fileName := SiteFileName + ".zip"
tempDir := in.Path
if tempDir == "" {
tempDir = gfile.Temp("goframe/docs")
Expand All @@ -99,7 +105,7 @@ func NewDocSetting(ctx context.Context, in cDocInput) *DocSetting {

return &DocSetting{
TempDir: filepath.FromSlash(tempDir),
DocDir: filepath.FromSlash(path.Join(tempDir, "gf-gh-pages")),
DocDir: filepath.FromSlash(path.Join(tempDir, SiteFileName)),
DocURL: in.Proxy + DocURL,
DocZipFile: filepath.FromSlash(path.Join(tempDir, fileName)),
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/gf/internal/cmd/genctrl/genctrl_generate_ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *controllerGenerator) doGenerateCtrlNewByModuleAndVersion(
if err = gfile.PutContents(moduleFilePath, gstr.TrimLeft(content)); err != nil {
return err
}
mlog.Printf(`generated: %s`, moduleFilePath)
mlog.Printf(`generated: %s`, gfile.RealPath(moduleFilePath))
}
if !gfile.Exists(moduleFilePathNew) {
content := gstr.ReplaceByMap(consts.TemplateGenCtrlControllerNewEmpty, g.MapStrStr{
Expand All @@ -99,7 +99,7 @@ func (c *controllerGenerator) doGenerateCtrlNewByModuleAndVersion(
if err = gfile.PutContents(moduleFilePathNew, gstr.TrimLeft(content)); err != nil {
return err
}
mlog.Printf(`generated: %s`, moduleFilePathNew)
mlog.Printf(`generated: %s`, gfile.RealPath(moduleFilePathNew))
}
filePaths, err := gfile.ScanDir(dstModuleFolderPath, "*.go", false)
if err != nil {
Expand Down Expand Up @@ -161,7 +161,7 @@ func (c *controllerGenerator) doGenerateCtrlItem(dstModuleFolderPath string, ite
return err
}
}
mlog.Printf(`generated: %s`, methodFilePath)
mlog.Printf(`generated: %s`, gfile.RealPath(methodFilePath))
return
}

Expand Down Expand Up @@ -221,7 +221,7 @@ func (c *controllerGenerator) doGenerateCtrlMergeItem(dstModuleFolderPath string
if err = gfile.PutContentsAppend(ctrlFilePath, ctrlFileItem.controllers.String()); err != nil {
return err
}
mlog.Printf(`generated: %s`, ctrlFilePath)
mlog.Printf(`generated: %s`, gfile.RealPath(ctrlFilePath))
}
return
}
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/genctrl/genctrl_generate_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (c *apiInterfaceGenerator) doGenerate(apiModuleFolderPath string, module st
"{Interfaces}": gstr.TrimRightStr(interfaceDefinition, "\n", 2),
}))
err = gfile.PutContents(moduleFilePath, interfaceContent)
mlog.Printf(`generated: %s`, moduleFilePath)
mlog.Printf(`generated: %s`, gfile.RealPath(moduleFilePath))
return
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/gf/internal/cmd/genctrl/genctrl_generate_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *apiSdkGenerator) doGenerateSdkPkgFile(sdkFolderPath string) (err error)
"{PkgName}": pkgName,
}))
err = gfile.PutContents(pkgFilePath, fileContent)
mlog.Printf(`generated: %s`, pkgFilePath)
mlog.Printf(`generated: %s`, gfile.RealPath(pkgFilePath))
return
}

Expand Down Expand Up @@ -130,9 +130,9 @@ func (c *apiSdkGenerator) doGenerateSdkIClient(
if isDirty {
err = gfile.PutContents(iClientFilePath, fileContent)
if isExist {
mlog.Printf(`updated: %s`, iClientFilePath)
mlog.Printf(`updated: %s`, gfile.RealPath(iClientFilePath))
} else {
mlog.Printf(`generated: %s`, iClientFilePath)
mlog.Printf(`generated: %s`, gfile.RealPath(iClientFilePath))
}
}
return
Expand Down Expand Up @@ -183,7 +183,7 @@ func (c *apiSdkGenerator) doGenerateSdkImplementer(
implementerFileContent += "\n"
}
err = gfile.PutContents(implementerFilePath, implementerFileContent)
mlog.Printf(`generated: %s`, implementerFilePath)
mlog.Printf(`generated: %s`, gfile.RealPath(implementerFilePath))
return
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/gf/internal/cmd/gendao/gendao_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func generateDaoIndex(in generateDaoIndexInput) {
mlog.Fatalf("writing content to '%s' failed: %v", path, err)
} else {
utils.GoFmt(path)
mlog.Print("generated:", path)
mlog.Print("generated:", gfile.RealPath(path))
}
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func generateDaoInternal(in generateDaoInternalInput) {
mlog.Fatalf("writing content to '%s' failed: %v", path, err)
} else {
utils.GoFmt(path)
mlog.Print("generated:", path)
mlog.Print("generated:", gfile.RealPath(path))
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/gendao/gendao_do.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func generateDo(ctx context.Context, in CGenDaoInternalInput) {
mlog.Fatalf(`writing content to "%s" failed: %v`, doFilePath, err)
} else {
utils.GoFmt(doFilePath)
mlog.Print("generated:", doFilePath)
mlog.Print("generated:", gfile.RealPath(doFilePath))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/gendao/gendao_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
mlog.Fatalf("writing content to '%s' failed: %v", entityFilePath, err)
} else {
utils.GoFmt(entityFilePath)
mlog.Print("generated:", entityFilePath)
mlog.Print("generated:", gfile.RealPath(entityFilePath))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/cmd/genpbentity/genpbentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func generatePbEntityContentFile(ctx context.Context, in CGenPbEntityInternalInp
if err := gfile.PutContents(path, strings.TrimSpace(entityContent)); err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", path, err)
} else {
mlog.Print("generated:", path)
mlog.Print("generated:", gfile.RealPath(path))
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/gf/internal/packed/template-mono-app.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/gf/internal/packed/template-mono.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/gf/internal/packed/template-single.go

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions errors/gerror/gerror_z_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,25 @@ func ExampleIs() {
// true
// false
}

func ExampleCode() {
err1 := gerror.NewCode(gcode.CodeInternalError, "permission denied")
err2 := gerror.Wrap(err1, "operation failed")
fmt.Println(gerror.Code(err1))
fmt.Println(gerror.Code(err2))

// Output:
// 50:Internal Error
// 50:Internal Error
}

func ExampleHasCode() {
err1 := gerror.NewCode(gcode.CodeInternalError, "permission denied")
err2 := gerror.Wrap(err1, "operation failed")
fmt.Println(gerror.HasCode(err1, gcode.CodeOK))
fmt.Println(gerror.HasCode(err2, gcode.CodeInternalError))

// Output:
// false
// true
}
22 changes: 0 additions & 22 deletions net/ghttp/ghttp_server_service_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,28 +221,6 @@ func (s *Server) checkAndCreateFuncInfo(
}
*/

// The request struct should be named as `xxxReq`.
reqStructName := trimGeneric(reflectType.In(1).String())
if !gstr.HasSuffix(reqStructName, `Req`) {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid struct naming for request: defined as "%s", but it should be named with "Req" suffix like "XxxReq"`,
reqStructName,
)
return
}

// The response struct should be named as `xxxRes`.
resStructName := trimGeneric(reflectType.Out(0).String())
if !gstr.HasSuffix(resStructName, `Res`) {
err = gerror.NewCodef(
gcode.CodeInvalidParameter,
`invalid struct naming for response: defined as "%s", but it should be named with "Res" suffix like "XxxRes"`,
resStructName,
)
return
}

funcInfo.IsStrictRoute = true

inputObject = reflect.New(funcInfo.Type.In(1).Elem())
Expand Down

0 comments on commit b3cdcd4

Please sign in to comment.