perf(binding): avoid unnecessary strconv.Parse calls for empty form values

This commit is contained in:
1911860538 2025-12-29 20:16:11 +08:00
parent 915e4c90d2
commit 41c1ee6015

View File

@ -361,7 +361,8 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
func setIntField(val string, bitSize int, field reflect.Value) error { func setIntField(val string, bitSize int, field reflect.Value) error {
if val == "" { if val == "" {
val = "0" field.SetInt(0)
return nil
} }
intVal, err := strconv.ParseInt(val, 10, bitSize) intVal, err := strconv.ParseInt(val, 10, bitSize)
if err == nil { if err == nil {
@ -372,7 +373,8 @@ func setIntField(val string, bitSize int, field reflect.Value) error {
func setUintField(val string, bitSize int, field reflect.Value) error { func setUintField(val string, bitSize int, field reflect.Value) error {
if val == "" { if val == "" {
val = "0" field.SetUint(0)
return nil
} }
uintVal, err := strconv.ParseUint(val, 10, bitSize) uintVal, err := strconv.ParseUint(val, 10, bitSize)
if err == nil { if err == nil {
@ -383,7 +385,8 @@ func setUintField(val string, bitSize int, field reflect.Value) error {
func setBoolField(val string, field reflect.Value) error { func setBoolField(val string, field reflect.Value) error {
if val == "" { if val == "" {
val = "false" field.SetBool(false)
return nil
} }
boolVal, err := strconv.ParseBool(val) boolVal, err := strconv.ParseBool(val)
if err == nil { if err == nil {
@ -394,7 +397,8 @@ func setBoolField(val string, field reflect.Value) error {
func setFloatField(val string, bitSize int, field reflect.Value) error { func setFloatField(val string, bitSize int, field reflect.Value) error {
if val == "" { if val == "" {
val = "0.0" field.SetFloat(0)
return nil
} }
floatVal, err := strconv.ParseFloat(val, bitSize) floatVal, err := strconv.ParseFloat(val, bitSize)
if err == nil { if err == nil {
@ -481,7 +485,8 @@ func setSlice(vals []string, value reflect.Value, field reflect.StructField) err
func setTimeDuration(val string, value reflect.Value) error { func setTimeDuration(val string, value reflect.Value) error {
if val == "" { if val == "" {
val = "0" value.Set(reflect.ValueOf(time.Duration(0)))
return nil
} }
d, err := time.ParseDuration(val) d, err := time.ParseDuration(val)