Skip to content

Commit

Permalink
refactor: layout
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed May 10, 2023
1 parent 56f1d42 commit 70dc933
Show file tree
Hide file tree
Showing 21 changed files with 215 additions and 438 deletions.
8 changes: 2 additions & 6 deletions pkg/app/server/binding/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@
package binding

import (
"github.com/cloudwego/hertz/pkg/app/server/binding/path"
"github.com/cloudwego/hertz/pkg/protocol"
)

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

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

var DefaultBinder Binder = &defaultBinder{}
3 changes: 2 additions & 1 deletion pkg/app/server/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ package binding

import (
"fmt"
"github.com/cloudwego/hertz/pkg/app/server/binding/path"
"mime/multipart"
"testing"

Expand Down Expand Up @@ -492,7 +493,7 @@ type CustomizedDecode struct {
A string
}

func (c *CustomizedDecode) CustomizedFieldDecode(req *protocol.Request, params PathParam) error {
func (c *CustomizedDecode) CustomizedFieldDecode(req *protocol.Request, params path.PathParam) error {
q1 := req.URI().QueryArgs().Peek("a")
if len(q1) == 0 {
return fmt.Errorf("can be nil")
Expand Down
15 changes: 15 additions & 0 deletions pkg/app/server/binding/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package binding

import (
standardJson "encoding/json"

hjson "github.com/cloudwego/hertz/pkg/common/json"
)

func ResetJSONUnmarshaler(fn func(data []byte, v interface{}) error) {
hjson.Unmarshal = fn
}

func ResetStdJSONUnmarshaler() {
ResetJSONUnmarshaler(standardJson.Unmarshal)
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
* Modifications are Copyright 2022 CloudWeGo Authors
*/

package binding
package decoder

import (
"fmt"
"reflect"

"github.com/cloudwego/hertz/pkg/app/server/binding/text_decoder"
path1 "github.com/cloudwego/hertz/pkg/app/server/binding/path"
"github.com/cloudwego/hertz/pkg/common/utils"
)

Expand All @@ -58,10 +58,10 @@ type fieldInfo struct {

type baseTypeFieldTextDecoder struct {
fieldInfo
decoder text_decoder.TextDecoder
decoder TextDecoder
}

func (d *baseTypeFieldTextDecoder) Decode(req *bindRequest, params PathParam, reqValue reflect.Value) error {
func (d *baseTypeFieldTextDecoder) Decode(req *bindRequest, params path1.PathParam, reqValue reflect.Value) error {
var err error
var text string
var defaultValue string
Expand Down Expand Up @@ -122,7 +122,7 @@ func (d *baseTypeFieldTextDecoder) Decode(req *bindRequest, params PathParam, re
return nil
}

func getBaseTypeTextDecoder(field reflect.StructField, index int, tagInfos []TagInfo, parentIdx []int) ([]decoder, error) {
func getBaseTypeTextDecoder(field reflect.StructField, index int, tagInfos []TagInfo, parentIdx []int) ([]fieldDecoder, error) {
for idx, tagInfo := range tagInfos {
switch tagInfo.Key {
case pathTag:
Expand Down Expand Up @@ -150,12 +150,12 @@ func getBaseTypeTextDecoder(field reflect.StructField, index int, tagInfos []Tag
fieldType = field.Type.Elem()
}

textDecoder, err := text_decoder.SelectTextDecoder(fieldType)
textDecoder, err := SelectTextDecoder(fieldType)
if err != nil {
return nil, err
}

fieldDecoder := &baseTypeFieldTextDecoder{
return []fieldDecoder{&baseTypeFieldTextDecoder{
fieldInfo: fieldInfo{
index: index,
parentIndex: parentIdx,
Expand All @@ -164,7 +164,5 @@ func getBaseTypeTextDecoder(field reflect.StructField, index int, tagInfos []Tag
fieldType: fieldType,
},
decoder: textDecoder,
}

return []decoder{fieldDecoder}, nil
}}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@
* Modifications are Copyright 2022 CloudWeGo Authors
*/

package binding
package decoder

import (
"reflect"

path1 "github.com/cloudwego/hertz/pkg/app/server/binding/path"
)

type customizedFieldTextDecoder struct {
fieldInfo
}

func (d *customizedFieldTextDecoder) Decode(req *bindRequest, params PathParam, reqValue reflect.Value) error {
func (d *customizedFieldTextDecoder) Decode(req *bindRequest, params path1.PathParam, reqValue reflect.Value) error {
var err error
v := reflect.New(d.fieldType)
decoder := v.Interface().(CustomizedFieldDecoder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Modifications are Copyright 2022 CloudWeGo Authors
*/

package binding
package decoder

import (
"fmt"
Expand All @@ -47,6 +47,7 @@ import (
"net/url"
"reflect"

path1 "github.com/cloudwego/hertz/pkg/app/server/binding/path"
"github.com/cloudwego/hertz/pkg/protocol"
)

Expand All @@ -59,20 +60,20 @@ type bindRequest struct {
Cookie []*http.Cookie
}

type decoder interface {
Decode(req *bindRequest, params PathParam, reqValue reflect.Value) error
type fieldDecoder interface {
Decode(req *bindRequest, params path1.PathParam, reqValue reflect.Value) error
}

type CustomizedFieldDecoder interface {
CustomizedFieldDecode(req *protocol.Request, params PathParam) error
CustomizedFieldDecode(req *protocol.Request, params path1.PathParam) error
}

type Decoder func(req *protocol.Request, params PathParam, rv reflect.Value) error
type Decoder func(req *protocol.Request, params path1.PathParam, rv reflect.Value) error

var customizedFieldDecoderType = reflect.TypeOf((*CustomizedFieldDecoder)(nil)).Elem()

func getReqDecoder(rt reflect.Type) (Decoder, error) {
var decoders []decoder
func GetReqDecoder(rt reflect.Type) (Decoder, error) {
var decoders []fieldDecoder

el := rt.Elem()
if el.Kind() != reflect.Struct {
Expand All @@ -95,7 +96,7 @@ func getReqDecoder(rt reflect.Type) (Decoder, error) {
}
}

return func(req *protocol.Request, params PathParam, rv reflect.Value) error {
return func(req *protocol.Request, params path1.PathParam, rv reflect.Value) error {
bindReq := &bindRequest{
Req: req,
}
Expand All @@ -110,12 +111,12 @@ func getReqDecoder(rt reflect.Type) (Decoder, error) {
}, nil
}

func getFieldDecoder(field reflect.StructField, index int, parentIdx []int) ([]decoder, error) {
func getFieldDecoder(field reflect.StructField, index int, parentIdx []int) ([]fieldDecoder, error) {
for field.Type.Kind() == reflect.Ptr {
field.Type = field.Type.Elem()
}
if reflect.PtrTo(field.Type).Implements(customizedFieldDecoderType) {
return []decoder{&customizedFieldTextDecoder{
return []fieldDecoder{&customizedFieldTextDecoder{
fieldInfo: fieldInfo{
index: index,
parentIndex: parentIdx,
Expand All @@ -139,7 +140,7 @@ func getFieldDecoder(field reflect.StructField, index int, parentIdx []int) ([]d
}

if field.Type.Kind() == reflect.Struct {
var decoders []decoder
var decoders []fieldDecoder
el := field.Type
// todo: built-in bindings for some common structs, code need to be optimized
switch el {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@
* Modifications are Copyright 2022 CloudWeGo Authors
*/

package binding
package decoder

import (
"net/http"
"net/url"

path1 "github.com/cloudwego/hertz/pkg/app/server/binding/path"
)

type getter func(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string)
type getter func(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string)

func path(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string) {
func path(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string) {
var value string
if params != nil {
value, _ = params.Get(key)
Expand All @@ -64,7 +66,7 @@ func path(req *bindRequest, params PathParam, key string, defaultValue ...string
}

// todo: Optimize 'postform' and 'multipart-form'
func form(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string) {
func form(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string) {
if req.Query == nil {
req.Query = make(url.Values)
req.Req.URI().QueryArgs().VisitAll(func(queryKey, value []byte) {
Expand Down Expand Up @@ -116,7 +118,7 @@ func form(req *bindRequest, params PathParam, key string, defaultValue ...string
return
}

func query(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string) {
func query(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string) {
if req.Query == nil {
req.Query = make(url.Values)
req.Req.URI().QueryArgs().VisitAll(func(queryKey, value []byte) {
Expand All @@ -135,7 +137,7 @@ func query(req *bindRequest, params PathParam, key string, defaultValue ...strin
return
}

func cookie(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string) {
func cookie(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string) {
if len(req.Cookie) == 0 {
req.Req.Header.VisitAllCookie(func(cookieKey, value []byte) {
req.Cookie = append(req.Cookie, &http.Cookie{
Expand All @@ -156,7 +158,7 @@ func cookie(req *bindRequest, params PathParam, key string, defaultValue ...stri
return
}

func header(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string) {
func header(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string) {
if req.Header == nil {
req.Header = make(http.Header)
req.Req.Header.VisitAll(func(headerKey, value []byte) {
Expand All @@ -175,19 +177,19 @@ func header(req *bindRequest, params PathParam, key string, defaultValue ...stri
return
}

func json(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string) {
func json(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string) {
// do nothing
return
}

func rawBody(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string) {
func rawBody(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string) {
if req.Req.Header.ContentLength() > 0 {
ret = append(ret, string(req.Req.Body()))
}
return
}

func FileName(req *bindRequest, params PathParam, key string, defaultValue ...string) (ret []string) {
func fileName(req *bindRequest, params path1.PathParam, key string, defaultValue ...string) (ret []string) {
// do nothing
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@
* Modifications are Copyright 2022 CloudWeGo Authors
*/

package binding
package decoder

import (
"fmt"
"reflect"

"github.com/cloudwego/hertz/internal/bytesconv"
path1 "github.com/cloudwego/hertz/pkg/app/server/binding/path"
hjson "github.com/cloudwego/hertz/pkg/common/json"
"github.com/cloudwego/hertz/pkg/common/utils"
)

type mapTypeFieldTextDecoder struct {
fieldInfo
}

func (d *mapTypeFieldTextDecoder) Decode(req *bindRequest, params PathParam, reqValue reflect.Value) error {
func (d *mapTypeFieldTextDecoder) Decode(req *bindRequest, params path1.PathParam, reqValue reflect.Value) error {
var text string
var defaultValue string
for _, tagInfo := range d.tagInfos {
Expand Down Expand Up @@ -94,15 +96,15 @@ func (d *mapTypeFieldTextDecoder) Decode(req *bindRequest, params PathParam, req
return nil
}

err := jsonUnmarshalFunc(bytesconv.S2b(text), field.Addr().Interface())
err := hjson.Unmarshal(bytesconv.S2b(text), field.Addr().Interface())
if err != nil {
return fmt.Errorf("unable to decode '%s' as %s: %w", text, d.fieldType.Name(), err)
}

return nil
}

func getMapTypeTextDecoder(field reflect.StructField, index int, tagInfos []TagInfo, parentIdx []int) ([]decoder, error) {
func getMapTypeTextDecoder(field reflect.StructField, index int, tagInfos []TagInfo, parentIdx []int) ([]fieldDecoder, error) {
for idx, tagInfo := range tagInfos {
switch tagInfo.Key {
case pathTag:
Expand All @@ -129,15 +131,14 @@ func getMapTypeTextDecoder(field reflect.StructField, index int, tagInfos []TagI
for field.Type.Kind() == reflect.Ptr {
fieldType = field.Type.Elem()
}
fieldDecoder := &mapTypeFieldTextDecoder{

return []fieldDecoder{&mapTypeFieldTextDecoder{
fieldInfo: fieldInfo{
index: index,
parentIndex: parentIdx,
fieldName: field.Name,
tagInfos: tagInfos,
fieldType: fieldType,
},
}

return []decoder{fieldDecoder}, nil
}}, nil
}
Loading

0 comments on commit 70dc933

Please sign in to comment.