mirror of
https://github.com/gin-gonic/gin.git
synced 2026-09-12 21:05:53 +08:00
1:tagValue only needs to be assigned once, which removes an unnecessary step.
2:This reduces a redundant check. When tagValue is empty, we can first check whether field.Name is empty before assigning it. 3: With this approach, the comment in the second block can also be removed. The second invocation of k, v := head(opt, "=") in the original code appears to be unnecessary and can be removed. Some of these changes may be too minor to provide any meaningful benefit, and the actual optimization is very limited. Please feel free to ignore any suggestions that do not seem worthwhile.
This commit is contained in:
parent
9654077b1b
commit
14f0702ccf
@ -143,23 +143,22 @@ type setOptions struct {
|
||||
}
|
||||
|
||||
func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {
|
||||
var tagValue string
|
||||
var setOpt setOptions
|
||||
|
||||
tagValue, opts := head(field.Tag.Get(tag), ",")
|
||||
|
||||
if tagValue == "" { // default value is FieldName
|
||||
if field.Name == "" {
|
||||
return false, nil
|
||||
}
|
||||
tagValue = field.Name
|
||||
}
|
||||
if tagValue == "" { // when field is "emptyField" variable
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var opt string
|
||||
for len(opts) > 0 {
|
||||
opt, opts = head(opts, ",")
|
||||
|
||||
if k, v := head(opt, "="); k == "default" {
|
||||
k, v := head(opt, "=")
|
||||
if k == "default" {
|
||||
setOpt.isDefaultExists = true
|
||||
setOpt.defaultValue = v
|
||||
|
||||
@ -170,7 +169,7 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter
|
||||
setOpt.defaultValue = strings.ReplaceAll(v, ";", ",")
|
||||
}
|
||||
}
|
||||
} else if k, v = head(opt, "="); k == "parser" {
|
||||
} else if k == "parser" {
|
||||
setOpt.parser = v
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user