Skip to content

Commit

Permalink
chore: release v0.8.1 (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF authored Feb 20, 2024
2 parents c2a6e12 + e86cb7f commit 7d8dad0
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35
uses: tj-actions/changed-files@v41

- name: Hz Test
if: contains(steps.changed-files.outputs.all_changed_files, 'cmd/hz')
Expand All @@ -105,7 +105,7 @@ jobs:

- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35
uses: tj-actions/changed-files@v41

- name: Install Protobuf
shell: pwsh
Expand Down
12 changes: 10 additions & 2 deletions cmd/hz/generator/package_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,14 @@ func (r *request) addHeader(header, value string) *request {
return r
}
func (r *request) addHeaders(params map[string]string) *request {
for k, v := range params {
r.addHeader(k, v)
}
return r
}
func (r *request) setQueryParam(param string, value interface{}) *request {
v := reflect.ValueOf(value)
switch v.Kind() {
Expand Down Expand Up @@ -923,7 +931,7 @@ func New{{.ServiceName}}Client(hostUrl string, ops ...Option) (Client, error) {
{{range $_, $MethodInfo := .ClientMethods}}
func (s *{{$.ServiceName}}Client) {{$MethodInfo.Name}}(context context.Context, req *{{$MethodInfo.RequestTypeName}}, reqOpt ...config.RequestOption) (resp *{{$MethodInfo.ReturnTypeName}}, rawResponse *protocol.Response, err error) {
httpResp := &{{$MethodInfo.ReturnTypeName}}{}
httpResp := &{{$MethodInfo.ReturnTypeName}}{}
ret, err := s.client.r().
setContext(context).
setQueryParams(map[string]interface{}{
Expand All @@ -944,7 +952,7 @@ func (s *{{$.ServiceName}}Client) {{$MethodInfo.Name}}(context context.Context,
{{$MethodInfo.BodyParamsCode}}
setRequestOption(reqOpt...).
setResult(httpResp).
execute("{{$MethodInfo.HTTPMethod}}", "{{$MethodInfo.Path}}")
execute("{{if EqualFold $MethodInfo.HTTPMethod "Any"}}POST{{else}}{{ $MethodInfo.HTTPMethod }}{{end}}", "{{$MethodInfo.Path}}")
if err != nil {
return nil, nil, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/hz/generator/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var funcMap = func() template.FuncMap {
"ToSnakeCase": util.ToSnakeCase,
"Split": strings.Split,
"Trim": strings.Trim,
"EqualFold": strings.EqualFold,
}
for key, f := range sprig.TxtFuncMap() {
m[key] = f
Expand Down
2 changes: 1 addition & 1 deletion cmd/hz/meta/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package meta
import "runtime"

// Version hz version
const Version = "v0.8.0"
const Version = "v0.8.1"

const DefaultServiceName = "hertz_service"

Expand Down
3 changes: 2 additions & 1 deletion pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ func (ctx *RequestContext) Copy() *RequestContext {
paramCopy := make([]param.Param, len(cp.Params))
copy(paramCopy, cp.Params)
cp.Params = paramCopy
cp.fullPath = ctx.fullPath
cp.clientIPFunc = ctx.clientIPFunc
cp.formValueFunc = ctx.formValueFunc
cp.binder = ctx.binder
Expand Down Expand Up @@ -1457,7 +1458,7 @@ func (ctx *RequestContext) BindByContentType(obj interface{}) error {
return ctx.BindQuery(obj)
}
ct := utils.FilterContentType(bytesconv.B2s(ctx.Request.Header.ContentType()))
switch ct {
switch strings.ToLower(ct) {
case consts.MIMEApplicationJSON:
return ctx.BindJSON(obj)
case consts.MIMEPROTOBUF:
Expand Down
6 changes: 6 additions & 0 deletions pkg/app/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func TestGet(t *testing.T) {
func TestCopy(t *testing.T) {
t.Parallel()
ctx := NewContext(0)
ctx.fullPath = "full_path"
ctx.Request.Header.Add("header_a", "header_value_a")
ctx.Response.Header.Add("header_b", "header_value_b")
ctx.Params = param.Params{
Expand All @@ -244,6 +245,11 @@ func TestCopy(t *testing.T) {
return
}

if c.fullPath != "full_path" {
t.Errorf("unexpected value: %#v, expected: %#v", c.fullPath, "full_path")
return
}

reqHeaderStr := context.Request.Header.Get("header_a")
if reqHeaderStr != "header_value_a" {
t.Errorf("unexpected value: %#v, expected: %#v", reqHeaderStr, "header_value_a")
Expand Down
42 changes: 42 additions & 0 deletions pkg/app/server/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,48 @@ func TestBind_Issue1015(t *testing.T) {
assert.DeepEqual(t, "asd", result.A)
}

func TestBind_WithoutPreBindForTag(t *testing.T) {
type BaseQuery struct {
Action string `query:"Action" binding:"required"`
Version string `query:"Version" binding:"required"`
}

req := newMockRequest().
SetJSONContentType().
SetRequestURI("http://foobar.com/?Action=action&Version=version").
SetBody([]byte(``))

var result BaseQuery

err := DefaultBinder().BindQuery(req.Req, &result)
if err != nil {
t.Error(err)
}
assert.DeepEqual(t, "action", result.Action)
assert.DeepEqual(t, "version", result.Version)
}

func TestBind_NormalizeContentType(t *testing.T) {
type BaseQuery struct {
Action string `json:"action" binding:"required"`
Version string `json:"version" binding:"required"`
}

req := newMockRequest().
SetHeader("Content-Type", "ApplicAtion/json").
SetRequestURI("http://foobar.com/?Action=action&Version=version").
SetBody([]byte(`{"action":"action", "version":"version"}`))

var result BaseQuery

err := DefaultBinder().BindQuery(req.Req, &result)
if err != nil {
t.Error(err)
}
assert.DeepEqual(t, "action", result.Action)
assert.DeepEqual(t, "version", result.Version)
}

func Benchmark_Binding(b *testing.B) {
type Req struct {
Version string `path:"v"`
Expand Down
13 changes: 8 additions & 5 deletions pkg/app/server/binding/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
"io"
"net/url"
"reflect"
"strings"
"sync"

exprValidator "github.com/bytedance/go-tagexpr/v2/validator"
Expand Down Expand Up @@ -175,9 +176,11 @@ func (b *defaultBinder) bindTag(req *protocol.Request, v interface{}, params par
return b.bindNonStruct(req, v)
}

err := b.preBindBody(req, v)
if err != nil {
return fmt.Errorf("bind body failed, err=%v", err)
if len(tag) == 0 {
err := b.preBindBody(req, v)
if err != nil {
return fmt.Errorf("bind body failed, err=%v", err)
}
}
cache := b.tagCache(tag)
cached, ok := cache.Load(typeID)
Expand Down Expand Up @@ -323,7 +326,7 @@ func (b *defaultBinder) preBindBody(req *protocol.Request, v interface{}) error
return nil
}
ct := bytesconv.B2s(req.Header.ContentType())
switch utils.FilterContentType(ct) {
switch strings.ToLower(utils.FilterContentType(ct)) {
case consts.MIMEApplicationJSON:
return hJson.Unmarshal(req.Body(), v)
case consts.MIMEPROTOBUF:
Expand All @@ -339,7 +342,7 @@ func (b *defaultBinder) preBindBody(req *protocol.Request, v interface{}) error

func (b *defaultBinder) bindNonStruct(req *protocol.Request, v interface{}) (err error) {
ct := bytesconv.B2s(req.Header.ContentType())
switch utils.FilterContentType(ct) {
switch strings.ToLower(utils.FilterContentType(ct)) {
case consts.MIMEApplicationJSON:
err = hJson.Unmarshal(req.Body(), v)
case consts.MIMEPROTOBUF:
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/server/binding/internal/decoder/gjson_required.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func checkRequireJSON(req *protocol.Request, tagInfo TagInfo) bool {
return true
}
ct := bytesconv.B2s(req.Header.ContentType())
if utils.FilterContentType(ct) != consts.MIMEApplicationJSON {
if !strings.EqualFold(utils.FilterContentType(ct), consts.MIMEApplicationJSON) {
return false
}
result := gjson.GetBytes(req.Body(), tagInfo.JSONName)
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/server/binding/internal/decoder/sonic_required.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func checkRequireJSON(req *protocol.Request, tagInfo TagInfo) bool {
return true
}
ct := bytesconv.B2s(req.Header.ContentType())
if utils.FilterContentType(ct) != consts.MIMEApplicationJSON {
if !strings.EqualFold(utils.FilterContentType(ct), consts.MIMEApplicationJSON) {
return false
}
node, _ := sonic.Get(req.Body(), stringSliceForInterface(tagInfo.JSONName)...)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ package hertz
// Name and Version info of this framework, used for statistics and debug
const (
Name = "Hertz"
Version = "v0.8.0"
Version = "v0.8.1"
)

0 comments on commit 7d8dad0

Please sign in to comment.