Skip to content

Commit

Permalink
feat: add license
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Feb 1, 2023
1 parent 2fa861f commit 2ffa30e
Show file tree
Hide file tree
Showing 40 changed files with 1,305 additions and 1,255 deletions.
11 changes: 5 additions & 6 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ import (
"sync"
"time"

"github.com/cloudwego/hertz/pkg/app/server/binding_v2"

"github.com/cloudwego/hertz/internal/bytesconv"
"github.com/cloudwego/hertz/internal/bytestr"
"github.com/cloudwego/hertz/pkg/app/server/binding"
"github.com/cloudwego/hertz/pkg/app/server/render"
"github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/tracer/traceinfo"
Expand Down Expand Up @@ -1222,22 +1221,22 @@ func bodyAllowedForStatus(status int) bool {
// BindAndValidate binds data from *RequestContext to obj and validates them if needed.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) BindAndValidate(obj interface{}) error {
err := binding_v2.DefaultBinder.Bind(&ctx.Request, ctx.Params, obj)
err := binding.DefaultBinder.Bind(&ctx.Request, ctx.Params, obj)
if err != nil {
return err
}
err = binding_v2.DefaultValidator.ValidateStruct(obj)
err = binding.DefaultValidator.ValidateStruct(obj)
return err
}

// Bind binds data from *RequestContext to obj.
// NOTE: obj should be a pointer.
func (ctx *RequestContext) Bind(obj interface{}) error {
return binding_v2.DefaultBinder.Bind(&ctx.Request, ctx.Params, obj)
return binding.DefaultBinder.Bind(&ctx.Request, ctx.Params, obj)
}

// Validate validates obj with "vd" tag
// NOTE: obj should be a pointer.
func (ctx *RequestContext) Validate(obj interface{}) error {
return binding_v2.DefaultValidator.ValidateStruct(obj)
return binding.DefaultValidator.ValidateStruct(obj)
}
2 changes: 1 addition & 1 deletion pkg/app/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ func TestRequestContext_GetResponse(t *testing.T) {
func TestBindAndValidate(t *testing.T) {
type Test struct {
A string `query:"a"`
B int `query:"b" vd:"$>10"`
B int `query:"b" validate:"gt=10"`
}

c := &RequestContext{}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
package binding_v2
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* MIT License
*
* Copyright (c) 2019-present Fenny and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file may have been modified by CloudWeGo authors. All CloudWeGo
* Modifications are Copyright 2022 CloudWeGo Authors
*/

package binding

import (
"fmt"
"reflect"

"github.com/cloudwego/hertz/pkg/app/server/binding_v2/text_decoder"
"github.com/cloudwego/hertz/pkg/app/server/binding/text_decoder"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol"
)
Expand All @@ -26,7 +66,6 @@ func (d *baseTypeFieldTextDecoder) Decode(req *protocol.Request, params PathPara
var err error
var text string
var defaultValue string
// 最大努力交付,对齐 hertz 现有设计
for _, tagInfo := range d.tagInfos {
if tagInfo.Key == jsonTag {
continue
Expand All @@ -37,7 +76,6 @@ func (d *baseTypeFieldTextDecoder) Decode(req *protocol.Request, params PathPara
ret := tagInfo.Getter(req, params, tagInfo.Value)
defaultValue = tagInfo.Default
if len(ret) != 0 {
// 非数组/切片类型,只取第一个值作为只
text = ret[0]
err = nil
break
Expand All @@ -56,12 +94,10 @@ func (d *baseTypeFieldTextDecoder) Decode(req *protocol.Request, params PathPara
return nil
}

// 得到该field的非nil值
// get the non-nil value for the field
reqValue = GetFieldValue(reqValue, d.parentIndex)
// 根据最终的 Struct,获取对应 field 的 reflect.Value
field := reqValue.Field(d.index)
if field.Kind() == reflect.Ptr {
// 如果是指针则新建一个reflect.Value,然后赋值给指针
t := field.Type()
var ptrDepth int
for t.Kind() == reflect.Ptr {
Expand Down
57 changes: 57 additions & 0 deletions pkg/app/server/binding/binder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* MIT License
*
* Copyright (c) 2019-present Fenny and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file may have been modified by CloudWeGo authors. All CloudWeGo
* Modifications are Copyright 2022 CloudWeGo Authors
*/

package binding

import (
"github.com/cloudwego/hertz/pkg/protocol"
)

// PathParams parameter acquisition interface on the URL path
type PathParams interface {
Get(name string) (string, bool)
}

type Binder interface {
Name() string
Bind(*protocol.Request, PathParams, interface{}) error
}

var DefaultBinder Binder = &Bind{}
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
package binding_v2
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* MIT License
*
* Copyright (c) 2019-present Fenny and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file may have been modified by CloudWeGo authors. All CloudWeGo
* Modifications are Copyright 2022 CloudWeGo Authors
*/

package binding

import (
"fmt"
"testing"

"github.com/cloudwego/hertz/pkg/app/server/binding"
"github.com/cloudwego/hertz/pkg/common/test/assert"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/route/param"
Expand Down Expand Up @@ -412,7 +451,6 @@ func TestBind_TypedefType(t *testing.T) {
assert.DeepEqual(t, "1", s.T1.T1)
}

// 枚举类型BaseType
type EnumType int64

const (
Expand Down Expand Up @@ -484,9 +522,8 @@ func TestBind_CustomizedTypeDecode(t *testing.T) {

func TestBind_JSON(t *testing.T) {
type Req struct {
J1 string `json:"j1"`
J2 int `json:"j2" query:"j2"` // 1. json unmarshal 2. query binding cover
// todo: map
J1 string `json:"j1"`
J2 int `json:"j2" query:"j2"` // 1. json unmarshal 2. query binding cover
J3 []byte `json:"j3"`
J4 [2]string `json:"j4"`
}
Expand Down Expand Up @@ -541,7 +578,7 @@ func TestBind_ResetJSONUnmarshal(t *testing.T) {
}
}

func Benchmark_V2(b *testing.B) {
func Benchmark_Binding(b *testing.B) {
type Req struct {
Version string `path:"v"`
ID int `query:"id"`
Expand Down Expand Up @@ -582,44 +619,3 @@ func Benchmark_V2(b *testing.B) {
}
}
}

func Benchmark_V1(b *testing.B) {
type Req struct {
Version string `path:"v"`
ID int `query:"id"`
Header string `header:"h"`
Form string `form:"f"`
}

req := newMockRequest().
SetRequestURI("http://foobar.com?id=12").
SetHeaders("h", "header").
SetPostArg("f", "form").
SetUrlEncodeContentType()
var params param.Params
params = append(params, param.Param{
Key: "v",
Value: "1",
})

b.ResetTimer()
for i := 0; i < b.N; i++ {
var result Req
err := binding.Bind(req.Req, &result, params)
if err != nil {
b.Error(err)
}
if result.ID != 12 {
b.Error("Id failed")
}
if result.Form != "form" {
b.Error("form failed")
}
if result.Header != "header" {
b.Error("header failed")
}
if result.Version != "1" {
b.Error("path failed")
}
}
}
Loading

0 comments on commit 2ffa30e

Please sign in to comment.