Skip to content

Commit

Permalink
feat: enable config to disable
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Sep 21, 2023
1 parent e2ce1ce commit e4084df
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
10 changes: 5 additions & 5 deletions pkg/app/server/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ func TestBind_DefaultTag(t *testing.T) {
assert.DeepEqual(t, "form", result.Form)

bindConfig := &BindConfig{}
bindConfig.EnableDefaultTag = false
bindConfig.DisableDefaultTag = true
binder := NewDefaultBinder(bindConfig)
result2 := Req2{}
err = binder.Bind(req.Req, &result2, params)
Expand Down Expand Up @@ -833,7 +833,7 @@ func TestBind_StructFieldResolve(t *testing.T) {
SetUrlEncodeContentType()
var result Req
bindConfig := &BindConfig{}
bindConfig.EnableStructFieldResolve = true
bindConfig.DisableStructFieldResolve = false
binder := NewDefaultBinder(bindConfig)
err := binder.Bind(req.Req, &result, nil)
if err != nil {
Expand Down Expand Up @@ -1195,7 +1195,7 @@ func TestBind_BindProtobuf(t *testing.T) {

func TestBind_PointerStruct(t *testing.T) {
bindConfig := &BindConfig{}
bindConfig.EnableStructFieldResolve = true
bindConfig.DisableStructFieldResolve = false
binder := NewDefaultBinder(bindConfig)
type Foo struct {
F1 string `query:"F1"`
Expand Down Expand Up @@ -1228,7 +1228,7 @@ func TestBind_PointerStruct(t *testing.T) {

func TestBind_StructRequired(t *testing.T) {
bindConfig := &BindConfig{}
bindConfig.EnableStructFieldResolve = true
bindConfig.DisableStructFieldResolve = false
binder := NewDefaultBinder(bindConfig)
type Foo struct {
F1 string `query:"F1"`
Expand Down Expand Up @@ -1261,7 +1261,7 @@ func TestBind_StructRequired(t *testing.T) {

func TestBind_StructErrorToWarn(t *testing.T) {
bindConfig := &BindConfig{}
bindConfig.EnableStructFieldResolve = true
bindConfig.DisableStructFieldResolve = false
binder := NewDefaultBinder(bindConfig)
type Foo struct {
F1 string `query:"F1"`
Expand Down
20 changes: 10 additions & 10 deletions pkg/app/server/binding/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ type BindConfig struct {
// The default is false.
// Suitable for these parameter types: query/header/cookie/form .
LooseZeroMode bool
// EnableDefaultTag is used to add default tags to a field when it has no tag
// If is true, the field with no tag will be added default tags, for more automated binding. But there may be additional overhead.
// DisableDefaultTag is used to add default tags to a field when it has no tag
// If is false, the field with no tag will be added default tags, for more automated binding. But there may be additional overhead.
// NOTE:
// The default is true.
EnableDefaultTag bool
// EnableStructFieldResolve is used to generate a separate decoder for a struct.
// If is true, the 'struct' field will get a single inDecoder.structTypeFieldTextDecoder, and use json.Unmarshal for decode it.
// The default is false.
DisableDefaultTag bool
// DisableStructFieldResolve is used to generate a separate decoder for a struct.
// If is false, the 'struct' field will get a single inDecoder.structTypeFieldTextDecoder, and use json.Unmarshal for decode it.
// It usually used to add json string to query parameter.
// NOTE:
// The default is true.
EnableStructFieldResolve bool
// The default is false.
DisableStructFieldResolve bool
// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
// interface{} as a Number instead of as a float64.
Expand Down Expand Up @@ -78,8 +78,8 @@ type BindConfig struct {
func NewBindConfig() *BindConfig {
return &BindConfig{
LooseZeroMode: false,
EnableDefaultTag: true,
EnableStructFieldResolve: true,
DisableDefaultTag: false,
DisableStructFieldResolve: false,
EnableDecoderUseNumber: false,
EnableDecoderDisallowUnknownFields: false,
ValidateTag: "vd",
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/server/binding/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func (b *defaultBinder) bindTag(req *protocol.Request, v interface{}, params par

decodeConfig := &inDecoder.DecodeConfig{
LooseZeroMode: b.config.LooseZeroMode,
EnableDefaultTag: b.config.EnableDefaultTag,
EnableStructFieldResolve: b.config.EnableStructFieldResolve,
DisableDefaultTag: b.config.DisableDefaultTag,
DisableStructFieldResolve: b.config.DisableStructFieldResolve,
EnableDecoderUseNumber: b.config.EnableDecoderUseNumber,
EnableDecoderDisallowUnknownFields: b.config.EnableDecoderDisallowUnknownFields,
ValidateTag: b.config.ValidateTag,
Expand Down Expand Up @@ -234,8 +234,8 @@ func (b *defaultBinder) bindTagWithValidate(req *protocol.Request, v interface{}
}
decodeConfig := &inDecoder.DecodeConfig{
LooseZeroMode: b.config.LooseZeroMode,
EnableDefaultTag: b.config.EnableDefaultTag,
EnableStructFieldResolve: b.config.EnableStructFieldResolve,
DisableDefaultTag: b.config.DisableDefaultTag,
DisableStructFieldResolve: b.config.DisableStructFieldResolve,
EnableDecoderUseNumber: b.config.EnableDecoderUseNumber,
EnableDecoderDisallowUnknownFields: b.config.EnableDecoderDisallowUnknownFields,
ValidateTag: b.config.ValidateTag,
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/server/binding/internal/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ type Decoder func(req *protocol.Request, params param.Params, rv reflect.Value)

type DecodeConfig struct {
LooseZeroMode bool
EnableDefaultTag bool
EnableStructFieldResolve bool
DisableDefaultTag bool
DisableStructFieldResolve bool
EnableDecoderUseNumber bool
EnableDecoderDisallowUnknownFields bool
ValidateTag string
Expand Down Expand Up @@ -117,7 +117,7 @@ func getFieldDecoder(field reflect.StructField, index int, parentIdx []int, pare

// JSONName is like 'a.b.c' for 'required validate'
fieldTagInfos, newParentJSONName, needValidate := lookupFieldTags(field, parentJSONName, config)
if len(fieldTagInfos) == 0 && config.EnableDefaultTag {
if len(fieldTagInfos) == 0 && !config.DisableDefaultTag {
fieldTagInfos = getDefaultFieldTags(field)
}
if len(byTag) != 0 {
Expand Down Expand Up @@ -152,7 +152,7 @@ func getFieldDecoder(field reflect.StructField, index int, parentIdx []int, pare
dec, err := getMultipartFileDecoder(field, index, fieldTagInfos, parentIdx, config)
return dec, needValidate, err
}
if config.EnableStructFieldResolve { // decode struct type separately
if !config.DisableStructFieldResolve { // decode struct type separately
structFieldDecoder, err := getStructTypeFieldDecoder(field, index, fieldTagInfos, parentIdx, config)
if err != nil {
return nil, needValidate, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/server/binding/tagexpr_bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ func TestOption(t *testing.T) {
req = newRequest("", header, nil, bodyReader)
recv2 := new(Recv2)
bindConfig := &BindConfig{}
bindConfig.EnableStructFieldResolve = true
bindConfig.DisableStructFieldResolve = false
binder := NewDefaultBinder(bindConfig)
err = binder.Bind(req.Req, recv2, nil)
assert.DeepEqual(t, err.Error(), "'X' field is a 'required' parameter, but the request does not have this parameter")
Expand Down Expand Up @@ -936,7 +936,7 @@ func TestRegTypeUnmarshal(t *testing.T) {
recv := new(T)

bindConfig := &BindConfig{}
bindConfig.EnableStructFieldResolve = true
bindConfig.DisableStructFieldResolve = false
binder := NewDefaultBinder(bindConfig)
err = binder.Bind(req.Req, recv, nil)
if err != nil {
Expand Down

0 comments on commit e4084df

Please sign in to comment.