1:tagValue only needs to be assigned once, which removes an unnecessary step.

Removed a potentially unnecessary temporary variable.
(I'm not sure if it's a personal preference, because this issue is completely negligible.)

2:This reduces a redundant check. When tagValue is empty, we can first check whether field.Name is empty before assigning it.With this approach, the comment in the second block can also be removed.

3:The second invocation of k, v := head(opt, "=") in the original code appears to be unnecessary and can be removed.
This commit is contained in:
cloud 2026-06-03 19:43:07 +08:00
parent d75fcd4c9a
commit 78d2282ccd

View File

@ -143,24 +143,23 @@ type setOptions struct {
}
func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) {
var tagValue string
var setOpt setOptions
tagValue = field.Tag.Get(tag)
tagValue, opts := head(tagValue, ",")
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
@ -171,7 +170,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
}
}