Skip to content

Commit a8cf033

Browse files
committed
chore: update version
1 parent 78e6ca6 commit a8cf033

File tree

6 files changed

+137
-1056
lines changed

6 files changed

+137
-1056
lines changed

binding/custom_error/main.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,7 @@ func (e *ValidateError) Error() string {
5353
return e.ErrType + ": expr_path=" + e.FailField + ", cause=invalid"
5454
}
5555

56-
func init() {
57-
CustomBindErrFunc := func(failField, msg string) error {
58-
err := BindError{
59-
ErrType: "bindErr",
60-
FailField: "[bindFailField]: " + failField,
61-
Msg: "[bindErrMsg]: " + msg,
62-
}
63-
64-
return &err
65-
}
66-
56+
func main() {
6757
CustomValidateErrFunc := func(failField, msg string) error {
6858
err := ValidateError{
6959
ErrType: "validateErr",
@@ -73,12 +63,10 @@ func init() {
7363

7464
return &err
7565
}
76-
77-
binding.SetErrorFactory(CustomBindErrFunc, CustomValidateErrFunc)
78-
}
79-
80-
func main() {
81-
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
66+
validateConfig := binding.NewValidateConfig()
67+
validateConfig.SetValidatorErrorFactory(CustomValidateErrFunc)
68+
h := server.Default(server.WithHostPorts("127.0.0.1:8080"),
69+
server.WithValidateConfig(validateConfig))
8270

8371
h.GET("bindErr", func(ctx context.Context, c *app.RequestContext) {
8472
type TestBind struct {

binding/custom_type_resolve/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"reflect"
2323
"time"
2424

25+
"github.com/cloudwego/hertz/pkg/route/param"
26+
2527
"github.com/cloudwego/hertz/pkg/app"
2628
"github.com/cloudwego/hertz/pkg/app/client"
2729
"github.com/cloudwego/hertz/pkg/app/server"
@@ -39,20 +41,19 @@ type TestBind struct {
3941
A Nested `query:"a"`
4042
}
4143

42-
func init() {
43-
binding.MustRegTypeUnmarshal(reflect.TypeOf(Nested{}), func(v string, emptyAsZero bool) (reflect.Value, error) {
44-
if v == "" && emptyAsZero {
44+
func main() {
45+
bindConfig := binding.NewBindConfig()
46+
bindConfig.MustRegTypeUnmarshal(reflect.TypeOf(Nested{}), func(req *protocol.Request, params param.Params, text string) (reflect.Value, error) {
47+
if text == "" {
4548
return reflect.ValueOf(Nested{}), nil
4649
}
4750
val := Nested{
48-
B: v[:5],
49-
C: v[5:],
51+
B: text[:5],
52+
C: text[5:],
5053
}
54+
// 此外,也可以利用 req, params 来获取其他参数进行参数绑定
5155
return reflect.ValueOf(val), nil
5256
})
53-
}
54-
55-
func main() {
5657
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
5758

5859
h.GET("customType", func(ctx context.Context, c *app.RequestContext) {

binding/custom_validate_func/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ type ValidateStruct struct {
3333
A string `query:"a" vd:"test($)"`
3434
}
3535

36-
func init() {
37-
binding.MustRegValidateFunc("test", func(args ...interface{}) error {
36+
func main() {
37+
validateConfig := binding.NewValidateConfig()
38+
validateConfig.MustRegValidateFunc("test", func(args ...interface{}) error {
3839
if len(args) != 1 {
3940
return fmt.Errorf("the args must be one")
4041
}
@@ -44,9 +45,6 @@ func init() {
4445
}
4546
return nil
4647
})
47-
}
48-
49-
func main() {
5048
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
5149

5250
h.GET("customValidate", func(ctx context.Context, c *app.RequestContext) {

binding/loose_zero/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ import (
2929
"github.com/cloudwego/hertz/pkg/protocol/consts"
3030
)
3131

32-
func init() {
33-
binding.SetLooseZeroMode(true)
34-
}
35-
3632
func main() {
37-
h := server.Default(server.WithHostPorts("127.0.0.1:8080"))
33+
bindConfig := binding.NewBindConfig()
34+
bindConfig.LooseZeroMode = true
35+
h := server.Default(server.WithHostPorts("127.0.0.1:8080"), server.WithBindConfig(bindConfig))
3836

3937
h.GET("looseZero", func(ctx context.Context, c *app.RequestContext) {
4038
type Loose struct {

go.mod

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module github.com/cloudwego/hertz-examples
22

3-
go 1.16
3+
go 1.21.1
44

55
require (
66
github.com/alibaba/sentinel-golang v1.0.4
77
github.com/apache/thrift v0.16.0
8-
github.com/cloudwego/hertz v0.6.8
9-
github.com/cloudwego/kitex v0.6.1
8+
github.com/cloudwego/hertz v0.7.0
9+
github.com/cloudwego/kitex v0.7.2
1010
github.com/hertz-contrib/cors v0.0.0-20230423034624-2bc83a8400f0
1111
github.com/hertz-contrib/csrf v0.1.1
1212
github.com/hertz-contrib/gzip v0.0.1
@@ -37,6 +37,93 @@ require (
3737
gopkg.in/natefinch/lumberjack.v2 v2.0.0
3838
)
3939

40+
require (
41+
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
42+
github.com/andeya/ameda v1.5.3 // indirect
43+
github.com/andeya/goutil v1.0.1 // indirect
44+
github.com/beorn7/perks v1.0.1 // indirect
45+
github.com/buger/jsonparser v1.1.1 // indirect
46+
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
47+
github.com/bytedance/sonic v1.9.1 // indirect
48+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
49+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
50+
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
51+
github.com/chenzhuoyu/iasm v0.9.0 // indirect
52+
github.com/choleraehyq/pid v0.0.17 // indirect
53+
github.com/cloudwego/configmanager v0.2.0 // indirect
54+
github.com/cloudwego/dynamicgo v0.1.3 // indirect
55+
github.com/cloudwego/fastpb v0.0.4 // indirect
56+
github.com/cloudwego/frugal v0.1.8 // indirect
57+
github.com/cloudwego/localsession v0.0.2 // indirect
58+
github.com/cloudwego/netpoll v0.5.0 // indirect
59+
github.com/cloudwego/thriftgo v0.3.0 // indirect
60+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
61+
github.com/fatih/structtag v1.2.0 // indirect
62+
github.com/fsnotify/fsnotify v1.6.0 // indirect
63+
github.com/go-logr/logr v1.2.4 // indirect
64+
github.com/go-logr/stdr v1.2.2 // indirect
65+
github.com/go-ole/go-ole v1.2.4 // indirect
66+
github.com/golang/protobuf v1.5.3 // indirect
67+
github.com/google/pprof v0.0.0-20230509042627-b1315fad0c5a // indirect
68+
github.com/google/uuid v1.3.0 // indirect
69+
github.com/gorilla/context v1.1.1 // indirect
70+
github.com/gorilla/securecookie v1.1.1 // indirect
71+
github.com/gorilla/sessions v1.2.1 // indirect
72+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
73+
github.com/iancoleman/strcase v0.2.0 // indirect
74+
github.com/jhump/protoreflect v1.8.2 // indirect
75+
github.com/json-iterator/go v1.1.12 // indirect
76+
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
77+
github.com/mattn/go-colorable v0.1.13 // indirect
78+
github.com/mattn/go-isatty v0.0.16 // indirect
79+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
80+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
81+
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 // indirect
82+
github.com/modern-go/reflect2 v1.0.2 // indirect
83+
github.com/nacos-group/nacos-sdk-go v1.1.2 // indirect
84+
github.com/nyaruka/phonenumbers v1.1.6 // indirect
85+
github.com/oleiade/lane v1.0.1 // indirect
86+
github.com/pkg/errors v0.9.1 // indirect
87+
github.com/pmezard/go-difflib v1.0.0 // indirect
88+
github.com/prometheus/client_model v0.3.0 // indirect
89+
github.com/prometheus/common v0.37.0 // indirect
90+
github.com/prometheus/procfs v0.8.0 // indirect
91+
github.com/rs/zerolog v1.28.0 // indirect
92+
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
93+
github.com/shirou/gopsutil/v3 v3.21.6 // indirect
94+
github.com/tidwall/gjson v1.14.4 // indirect
95+
github.com/tidwall/match v1.1.1 // indirect
96+
github.com/tidwall/pretty v1.2.1 // indirect
97+
github.com/tklauser/go-sysconf v0.3.6 // indirect
98+
github.com/tklauser/numcpus v0.2.2 // indirect
99+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
100+
go.opentelemetry.io/contrib/instrumentation/runtime v0.42.0 // indirect
101+
go.opentelemetry.io/contrib/propagators/b3 v1.17.0 // indirect
102+
go.opentelemetry.io/contrib/propagators/ot v1.17.0 // indirect
103+
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
104+
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect
105+
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.39.0 // indirect
106+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
107+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0 // indirect
108+
go.opentelemetry.io/otel/metric v1.16.0 // indirect
109+
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
110+
go.opentelemetry.io/otel/sdk/metric v0.39.0 // indirect
111+
go.opentelemetry.io/otel/trace v1.16.0 // indirect
112+
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
113+
go.uber.org/atomic v1.7.0 // indirect
114+
go.uber.org/multierr v1.11.0 // indirect
115+
go.uber.org/zap v1.23.0 // indirect
116+
golang.org/x/net v0.10.0 // indirect
117+
golang.org/x/sync v0.1.0 // indirect
118+
golang.org/x/sys v0.8.0 // indirect
119+
golang.org/x/text v0.9.0 // indirect
120+
google.golang.org/genproto v0.0.0-20230526203410-71b5a4ffd15e // indirect
121+
google.golang.org/genproto/googleapis/api v0.0.0-20230526203410-71b5a4ffd15e // indirect
122+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e // indirect
123+
google.golang.org/grpc v1.55.0 // indirect
124+
gopkg.in/yaml.v2 v2.4.0 // indirect
125+
)
126+
40127
require (
41128
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
42129
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1764 // indirect

0 commit comments

Comments
 (0)