-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net/ghttp:Improve the process of parameter parsing #3578
base: master
Are you sure you want to change the base?
Conversation
@wln32 这个改进思路非常不错!但是有些小瑕疵,我和你一起协作改进一下,需要花一点点时间。 |
@wln32 This improvement idea is very good! But there are some small flaws. I will work with you to improve them, and it will take a little time. |
哪些问题呢? |
What are the problems? |
net/ghttp/ghttp.go
Outdated
inTagFields []gstructs.Field | ||
// Language string `v:"required|eq:go"` | ||
// todo: Subsequently, the fields with valid tags are parsed into maps for direct verification | ||
validTagFields int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validatingTagFieldsCount
- 注释没有描述清楚
- 注释请放字段后面便于结构体字段整体编码对齐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validatingTagFieldsCount
- 注释没有描述清楚
- 注释请放字段后面便于结构体字段整体编码对齐
这里暂时用int类型代替,是用来统计字段有无需要验证的,本来是想把验证的规则用map存起来,然后不需要重复解析
} | ||
} | ||
|
||
func getValidTagField(field reflect.Type) (count int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getValidatingTagFieldCount
|
||
func getValidTagField(field reflect.Type) (count int) { | ||
if field.Kind() == reflect.Ptr { | ||
field = field.Elem() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样获取field
的原始类型并不准确,可以给Field
结构体增加OriginalType
方法,参考现有的OriginalKind
实现:
gf/os/gstructs/gstructs_field.go
Line 86 in 4abb32e
func (f *Field) OriginalKind() reflect.Kind { |
if field.Kind() == reflect.Ptr { | ||
field = field.Elem() | ||
} | ||
if field.Kind() == reflect.Struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
推荐判断逻辑使用尽早退出逻辑,减少代码层级,例如这里可以调整为;
if field.Kind() != reflect.Struct {
return
}
} | ||
if tag != "" { | ||
count++ | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
既然要计算标签数量,那么应当计算准确,这里应当使用continue
我已经在代码中评论你了。 |
I've commented you in the code. |
@@ -107,6 +107,12 @@ func (r *Request) doParse(pointer interface{}, requestType int) error { | |||
return err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这3个case
对应的方法其实存在瑕疵:
mergeInTagStructValue
仅在doGetRequestStruct
中有处理,其他两个case
没有处理。- 这3个方法都重复调用了
mergeDefaultStructValue
方法,其实可以抽象出来统一处理。
@wln32 Hello handsome, any updates? |
应该没了,再更新就是valid那块的缓存起来,gvalid的代码看起来有点麻烦 |
这个pr很好,后续需要我来看看进一步改进。 |
This PR is very good, I need to look at further improvements in the future. |
如果是严格路由的情况下,缓存带有default,in,valid,这三个tag的字段,避免做不必要的解析