Skip to content

Commit

Permalink
refactor(cmd/web/restdoc): 有错误信息输出时不再验证数据
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Sep 23, 2023
1 parent 7ac7f05 commit a2f44a4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
13 changes: 10 additions & 3 deletions cmd/web/restdoc/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
)

type Logger struct {
logs logs.Logs
p *message.Printer
count int
logs logs.Logs
p *message.Printer
count int
hasErr bool
}

func New(l logs.Logs, p *message.Printer) *Logger {
Expand All @@ -35,6 +36,8 @@ func (l *Logger) Info(msg any) { l.log(logs.Info, msg, "", 0) }
// Warning 输出警告信息
func (l *Logger) Warning(msg any) { l.log(logs.Warn, msg, "", 0) }

func (l *Logger) HasError() bool { return l.hasErr }

// Error 输出错误信息
//
// 如果 msg 包含了定位信息,则 filename 和 line 将被忽略
Expand Down Expand Up @@ -75,6 +78,10 @@ func (l *Logger) log(lv logs.Level, msg any, filename string, line int) {

l.count++ // 只有真正输出时,才需要+1。

if !l.hasErr && (lv == logs.Error || lv == logs.Fatal) {
l.hasErr = true
}

mm := m.LocaleString(l.p)
if filename != "" {
mm = web.Phrase("%s at %s:%d", mm, filename, line).LocaleString(l.p)
Expand Down
2 changes: 1 addition & 1 deletion cmd/web/restdoc/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestLogger(t *testing.T) {
e1 := &scanner.Error{Pos: token.Position{Filename: "f1.go"}, Msg: "e1"}
e2 := &scanner.Error{Pos: token.Position{Filename: "f1.go"}, Msg: "e2"}
l.Error(e1, "f1.go", 0)
a.Equal(1, l.Count()).True(buf.Len() > 0)
a.Equal(1, l.Count()).True(buf.Len() > 0).True(l.HasError())

list := scanner.ErrorList{e1, e2}
l.Error(list, "f1.go", 0)
Expand Down
4 changes: 4 additions & 0 deletions cmd/web/restdoc/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (p *Parser) Parse(ctx context.Context) *openapi.OpenAPI {
}
wg.Wait()

if p.l.HasError() {
return nil
}

if err := t.Doc().Validate(ctx); err != nil {
p.l.Error(err, "", 0)
return nil
Expand Down
11 changes: 7 additions & 4 deletions cmd/web/restdoc/restdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
}

l := logger.New(ls, p)
doc := parser.New(l, *urlPrefix, tags)
dp := parser.New(l, *urlPrefix, tags)
for _, dir := range fs.Args() {
doc.AddDir(ctx, dir, *r)
dp.AddDir(ctx, dir, *r)

if *d {
modCache := filepath.Join(build.Default.GOPATH, "pkg", "mod")
Expand All @@ -75,12 +75,15 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
continue
}
modDir := filepath.Join(modCache, p.Mod.Path+"@"+p.Mod.Version)
doc.AddDir(ctx, modDir, *r)
dp.AddDir(ctx, modDir, *r)
}
}
}

return doc.Parse(ctx).SaveAs(*o)
if doc := dp.Parse(ctx); doc != nil {
return doc.SaveAs(*o)
}
return nil
}
})
}
2 changes: 1 addition & 1 deletion cmd/web/restdoc/schema/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (f SearchFunc) fromTypeSpec(t *openapi.OpenAPI, currPath, tag string, file
case *ast.IndexListExpr: // type x = G[int, float]
return f.fromIndexListExprType(t, file, currPath, tag, ts)
default:
msg := web.Phrase("%s can not convert to ast.StructType", s.Type)
msg := web.Phrase("%s can not convert to ast.StructType", s.Name.Name)
return nil, newError(s.Pos(), msg)
}
}
Expand Down
14 changes: 9 additions & 5 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"testing"
"time"

"github.com/issue9/assert/v3"
"github.com/issue9/mux/v7"
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestServer_Routers(t *testing.T) {
srv := newTestServer(a, nil)

defer servertest.Run(a, srv)()
defer srv.Close(0)
defer srv.Close(500 * time.Millisecond)

ver := group.NewHeaderVersion("ver", "v", log.Default(), "2")
a.NotNil(ver)
Expand All @@ -46,8 +47,11 @@ func TestServer_Routers(t *testing.T) {
uu, err := r1.URL(false, "/posts/1", nil)
a.NotError(err).Equal("https://example.com/posts/1", uu)

r1.Prefix("/p1").Delete("/path", buildHandler(http.StatusCreated))
servertest.Delete(a, "http://localhost:8080/p1/path").Header("Accept", "application/json;v=2").Do(nil).Status(http.StatusCreated)
r1.Prefix("/p1").Delete("/path", buildHandler(http.StatusNoContent))
servertest.Delete(a, "http://localhost:8080/p1/path").
Header("Accept", "application/json;v=2").
Do(nil).
Status(http.StatusNoContent)
servertest.NewRequest(a, http.MethodOptions, "http://localhost:8080/p1/path").
Header("Accept", "application/json;v=2").
Do(nil).Status(http.StatusOK)
Expand All @@ -72,7 +76,7 @@ func TestServer_FileServer(t *testing.T) {
s.CatalogBuilder().SetString(language.MustParse("zh-CN"), "problem.404", "NOT FOUND")
r := s.NewRouter("def", nil)
defer servertest.Run(a, s)()
defer s.Close(0)
defer s.Close(500 * time.Millisecond)

t.Run("problems", func(t *testing.T) {
r.Get("/v1/{path}", s.FileServer(os.DirFS("./testdata"), "path", "index.html"))
Expand Down Expand Up @@ -144,7 +148,7 @@ func TestMiddleware(t *testing.T) {
prefix.Get("/path", buildHandler(201))

defer servertest.Run(a, srv)()
defer srv.Close(0)
defer srv.Close(500 * time.Millisecond)

servertest.Get(a, "http://localhost:8080/p1/path").
Header("accept", "application/json").
Expand Down
2 changes: 1 addition & 1 deletion web.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

// Version 当前框架的版本
const Version = "0.84.0"
const Version = "0.85.0"

type (
Logger = logs.Logger
Expand Down

0 comments on commit a2f44a4

Please sign in to comment.