fix issue 5732 (#5735)

This commit is contained in:
Ming Deng 2025-01-01 10:25:23 +08:00 committed by GitHub
parent e7fa4835f7
commit a21efb5613
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 9 deletions

View File

@ -17,10 +17,11 @@ package cache
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/stretchr/testify/assert"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestFileCacheStartAndGC(t *testing.T) { func TestFileCacheStartAndGC(t *testing.T) {

View File

@ -50,8 +50,9 @@ func parseFormToStruct(form url.Values, objT reflect.Type, objV reflect.Value) e
continue continue
} }
value, ok := formValue(tag, form, fieldT) value, ok := formFirstValue(tag, form, fieldT)
if !ok {
if !ok && fieldT.Type.Kind() != reflect.Slice {
continue continue
} }
@ -168,11 +169,12 @@ func formTagName(fieldT reflect.StructField) (string, bool) {
return tag, true return tag, true
} }
func formValue(tag string, form url.Values, fieldT reflect.StructField) (string, bool) { func formFirstValue(tag string, form url.Values, fieldT reflect.StructField) (string, bool) {
formValues := form[tag] formValues := form[tag]
if len(formValues) == 0 { if len(formValues) == 0 {
defaultValue := fieldT.Tag.Get("default") defaultValue := fieldT.Tag.Get("default")
return defaultValue, defaultValue != "" return defaultValue, defaultValue != ""
} }
return formValues[0], true val := formValues[0]
return val, val != ""
} }

View File

@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestFormValue(t *testing.T) { func TestFormFirstValue(t *testing.T) {
typ := reflect.TypeOf(TestStruct{}) typ := reflect.TypeOf(TestStruct{})
defField, _ := typ.FieldByName("DefaultField") defField, _ := typ.FieldByName("DefaultField")
noDefField, _ := typ.FieldByName("NoDefaultField") noDefField, _ := typ.FieldByName("NoDefaultField")
@ -54,7 +54,17 @@ func TestFormValue(t *testing.T) {
"defaultField": {"", "bcd"}, "defaultField": {"", "bcd"},
}, },
wantRes: "", wantRes: "",
wantOk: true, wantOk: false,
},
{
name: "empty default value",
tag: "defaultField",
field: defField,
form: map[string][]string{
"defaultField": {"", "bcd"},
},
wantRes: "",
wantOk: false,
}, },
{ {
name: "use default value", name: "use default value",
@ -75,7 +85,7 @@ func TestFormValue(t *testing.T) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
val, ok := formValue(tc.tag, tc.form, tc.field) val, ok := formFirstValue(tc.tag, tc.form, tc.field)
assert.Equal(t, tc.wantRes, val) assert.Equal(t, tc.wantRes, val)
assert.Equal(t, tc.wantOk, ok) assert.Equal(t, tc.wantOk, ok)
}) })

View File

@ -15,8 +15,8 @@
package web package web
import ( import (
"context"
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"