Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

wln32
Copy link
Member

@wln32 wln32 commented May 13, 2024

如果是严格路由的情况下,缓存带有default,in,valid,这三个tag的字段,避免做不必要的解析

@wln32 wln32 requested review from gqcn and hailaz May 13, 2024 11:13
@gqcn
Copy link
Member

gqcn commented May 21, 2024

@wln32 这个改进思路非常不错!但是有些小瑕疵,我和你一起协作改进一下,需要花一点点时间。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@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.

@gqcn gqcn added the awesome It's awesome! We keep watching. label May 21, 2024
@wln32
Copy link
Member Author

wln32 commented May 22, 2024

@wln32 这个改进思路非常不错!但是有些小瑕疵,我和你一起协作改进一下,需要花一点点时间。

哪些问题呢?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@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?

@gqcn gqcn added the wip label May 30, 2024
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • validatingTagFieldsCount
  • 注释没有描述清楚
  • 注释请放字段后面便于结构体字段整体编码对齐

Copy link
Member Author

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getValidatingTagFieldCount

net/ghttp/ghttp_server_service_handler.go Show resolved Hide resolved

func getValidTagField(field reflect.Type) (count int) {
if field.Kind() == reflect.Ptr {
field = field.Elem()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样获取field的原始类型并不准确,可以给Field结构体增加OriginalType方法,参考现有的OriginalKind实现:

func (f *Field) OriginalKind() reflect.Kind {

if field.Kind() == reflect.Ptr {
field = field.Elem()
}
if field.Kind() == reflect.Struct {
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然要计算标签数量,那么应当计算准确,这里应当使用continue

@gqcn
Copy link
Member

gqcn commented Jun 18, 2024

@wln32 这个改进思路非常不错!但是有些小瑕疵,我和你一起协作改进一下,需要花一点点时间。

哪些问题呢?

我已经在代码中评论你了。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@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?

I've commented you in the code.

@@ -107,6 +107,12 @@ func (r *Request) doParse(pointer interface{}, requestType int) error {
return err
Copy link
Member

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方法,其实可以抽象出来统一处理。

@gqcn
Copy link
Member

gqcn commented Jun 24, 2024

@wln32 Hello handsome, any updates?

@wln32
Copy link
Member Author

wln32 commented Jul 2, 2024

@wln32 Hello handsome, any updates?

应该没了,再更新就是valid那块的缓存起来,gvalid的代码看起来有点麻烦

@gqcn
Copy link
Member

gqcn commented Jul 4, 2024

这个pr很好,后续需要我来看看进一步改进。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


This PR is very good, I need to look at further improvements in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awesome It's awesome! We keep watching. wip
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants