Skip to content

Commit

Permalink
fixed: [help] HertzOptions change to Validator, exclude side-effect
Browse files Browse the repository at this point in the history
  • Loading branch information
kainonly committed Oct 1, 2023
1 parent 47e708c commit cce2c8a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
15 changes: 2 additions & 13 deletions help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
errorsx "errors"
"github.com/bytedance/gopkg/util/logger"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -49,7 +47,7 @@ func IsEmpty(i any) bool {
return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}

func HertzOptions(input ...config.Option) *config.Options {
func Validator() *go_playground.Validator {
vd := go_playground.NewValidator()
vd.SetValidateTag("vd")
vdx := vd.Engine().(*validator.Validate)
Expand All @@ -67,16 +65,7 @@ func HertzOptions(input ...config.Option) *config.Options {
}
return matched
})
opts := []config.Option{
server.WithCustomValidator(vd),
}
for _, x := range input {
opts = append(opts, x)
}
if os.Getenv("MODE") != "release" {
opts = append(opts, server.WithExitWaitTime(0))
}
return config.NewOptions(opts)
return vd
}

type EMeta struct {
Expand Down
7 changes: 6 additions & 1 deletion rest/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"github.com/bytedance/sonic"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/ut"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route"
Expand Down Expand Up @@ -107,7 +108,11 @@ func TestMain(m *testing.M) {
if err := MockStream(ctx); err != nil {
panic(err)
}
engine = route.NewEngine(help.HertzOptions(server.WithDisablePrintRoute(true)))
engine = route.NewEngine(config.NewOptions([]config.Option{
server.WithExitWaitTime(0),
server.WithDisablePrintRoute(true),
server.WithCustomValidator(help.Validator()),
}))
engine.Use(
requestid.New(),
help.EHandler(),
Expand Down
8 changes: 6 additions & 2 deletions sessions/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"github.com/bytedance/sonic"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/ut"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route"
Expand Down Expand Up @@ -49,8 +50,11 @@ func TestMain(m *testing.M) {
sessions.SetRedis(rdb),
sessions.SetDynamicValues(&v),
)

engine = route.NewEngine(help.HertzOptions(server.WithDisablePrintRoute(true)))
engine = route.NewEngine(config.NewOptions([]config.Option{
server.WithExitWaitTime(0),
server.WithDisablePrintRoute(true),
server.WithCustomValidator(help.Validator()),
}))
engine.Use(
requestid.New(),
help.EHandler(),
Expand Down
7 changes: 6 additions & 1 deletion values/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"github.com/bytedance/sonic"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/ut"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route"
Expand Down Expand Up @@ -56,7 +57,11 @@ func TestMain(m *testing.M) {
values.SetCipher(cipherx),
values.SetType(reflect.TypeOf(values.DynamicValues{})),
)
engine = route.NewEngine(help.HertzOptions(server.WithDisablePrintRoute(true)))
engine = route.NewEngine(config.NewOptions([]config.Option{
server.WithExitWaitTime(0),
server.WithDisablePrintRoute(true),
server.WithCustomValidator(help.Validator()),
}))
engine.Use(
requestid.New(),
help.EHandler(),
Expand Down

0 comments on commit cce2c8a

Please sign in to comment.