mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-25 07:05:39 +08:00
Compare commits
10 Commits
1fb2c2754e
...
6bdbc48c94
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bdbc48c94 | ||
|
|
19b877fa50 | ||
|
|
527f128f96 | ||
|
|
b6e5603530 | ||
|
|
822cf17bf9 | ||
|
|
6b6ac46ae3 | ||
|
|
ee1f501d4a | ||
|
|
ef09093976 | ||
|
|
513a86e17f | ||
|
|
b4d09053db |
@ -186,7 +186,10 @@ type BindUnmarshaler interface {
|
||||
func trySetCustom(val string, value reflect.Value) (isSet bool, err error) {
|
||||
switch v := value.Addr().Interface().(type) {
|
||||
case BindUnmarshaler:
|
||||
return true, v.UnmarshalParam(val)
|
||||
if err := v.UnmarshalParam(val); err != nil {
|
||||
return true, fmt.Errorf("invalid value %q: %w", val, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
@ -245,7 +248,10 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
|
||||
if ok, err = trySetCustom(vs[0], value); ok {
|
||||
return ok, err
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("field %q: %w", field.Name, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if vs, err = trySplit(vs, field); err != nil {
|
||||
@ -268,7 +274,10 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
|
||||
if ok, err = trySetCustom(vs[0], value); ok {
|
||||
return ok, err
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("field %q: %w", field.Name, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
if vs, err = trySplit(vs, field); err != nil {
|
||||
@ -293,7 +302,10 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
||||
}
|
||||
}
|
||||
if ok, err := trySetCustom(val, value); ok {
|
||||
return ok, err
|
||||
if err != nil {
|
||||
return true, fmt.Errorf("field %q: %w", field.Name, err)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
return true, setWithProperType(val, value, field)
|
||||
}
|
||||
@ -461,6 +473,12 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
|
||||
|
||||
func setArray(vals []string, value reflect.Value, field reflect.StructField) error {
|
||||
for i, s := range vals {
|
||||
if ok, err := trySetCustom(s, value.Index(i)); ok {
|
||||
if err != nil {
|
||||
return fmt.Errorf("field %q: %w", field.Name, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
err := setWithProperType(s, value.Index(i), field)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@ -754,3 +754,104 @@ func TestMappingEmptyValues(t *testing.T) {
|
||||
assert.Equal(t, []int{1, 2, 3}, s.SliceCsv)
|
||||
})
|
||||
}
|
||||
|
||||
type testCustom struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func (t *testCustom) UnmarshalParam(param string) error {
|
||||
t.Value = "prefix_" + param
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestTrySetCustomIntegration(t *testing.T) {
|
||||
var s struct {
|
||||
F testCustom `form:"f"`
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "prefix_hello", s.F.Value)
|
||||
}
|
||||
|
||||
type badCustom struct{}
|
||||
|
||||
func (b *badCustom) UnmarshalParam(s string) error {
|
||||
return errors.New("boom")
|
||||
}
|
||||
|
||||
func TestTrySetCustom_SingleValues(t *testing.T) {
|
||||
t.Run("Error case", func(t *testing.T) {
|
||||
var s struct {
|
||||
F badCustom `form:"f"`
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid value")
|
||||
})
|
||||
|
||||
t.Run("Integration success", func(t *testing.T) {
|
||||
var s struct {
|
||||
F testCustom `form:"f"`
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"f": {"hello"}}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "prefix_hello", s.F.Value)
|
||||
})
|
||||
|
||||
t.Run("Not applicable type", func(t *testing.T) {
|
||||
var s struct {
|
||||
N int `form:"n"`
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"n": {"42"}}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 42, s.N)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTrySetCustom_Collections(t *testing.T) {
|
||||
t.Run("Slice success", func(t *testing.T) {
|
||||
var s struct {
|
||||
F []testCustom `form:"f"`
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"f": {"one", "two"}}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "prefix_one", s.F[0].Value)
|
||||
assert.Equal(t, "prefix_two", s.F[1].Value)
|
||||
})
|
||||
|
||||
t.Run("Array success", func(t *testing.T) {
|
||||
var s struct {
|
||||
F [2]testCustom `form:"f"`
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"f": {"hello", "world"}}, "form")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "prefix_hello", s.F[0].Value)
|
||||
assert.Equal(t, "prefix_world", s.F[1].Value)
|
||||
})
|
||||
|
||||
t.Run("Slice error", func(t *testing.T) {
|
||||
var s struct {
|
||||
F []badCustom `form:"f"`
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"f": {"oops1", "oops2"}}, "form")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid value")
|
||||
})
|
||||
|
||||
t.Run("Array error", func(t *testing.T) {
|
||||
var s struct {
|
||||
F [2]badCustom `form:"f"`
|
||||
}
|
||||
|
||||
err := mappingByPtr(&s, formSource{"f": {"fail1", "fail2"}}, "form")
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid value")
|
||||
})
|
||||
}
|
||||
|
||||
4
debug.go
4
debug.go
@ -15,6 +15,8 @@ import (
|
||||
|
||||
const ginSupportMinGoVer = 24
|
||||
|
||||
var runtimeVersion = runtime.Version()
|
||||
|
||||
// IsDebugging returns true if the framework is running in debug mode.
|
||||
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
||||
func IsDebugging() bool {
|
||||
@ -77,7 +79,7 @@ func getMinVer(v string) (uint64, error) {
|
||||
}
|
||||
|
||||
func debugPrintWARNINGDefault() {
|
||||
if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {
|
||||
if v, e := getMinVer(runtimeVersion); e == nil && v < ginSupportMinGoVer {
|
||||
debugPrint(`[WARNING] Now Gin requires Go 1.24+.
|
||||
|
||||
`)
|
||||
|
||||
@ -12,7 +12,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@ -21,10 +20,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TODO
|
||||
// func debugRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
||||
// func debugPrint(format string, values ...any) {
|
||||
|
||||
func TestIsDebugging(t *testing.T) {
|
||||
SetMode(DebugMode)
|
||||
assert.True(t, IsDebugging())
|
||||
@ -48,6 +43,18 @@ func TestDebugPrint(t *testing.T) {
|
||||
assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintFunc(t *testing.T) {
|
||||
DebugPrintFunc = func(format string, values ...any) {
|
||||
fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
|
||||
}
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrint("debug print func test: %d", 123)
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Regexp(t, `^\[GIN-debug\] debug print func test: 123`, re)
|
||||
}
|
||||
|
||||
func TestDebugPrintError(t *testing.T) {
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
@ -104,12 +111,17 @@ func TestDebugPrintWARNINGDefault(t *testing.T) {
|
||||
debugPrintWARNINGDefault()
|
||||
SetMode(TestMode)
|
||||
})
|
||||
m, e := getMinVer(runtime.Version())
|
||||
if e == nil && m < ginSupportMinGoVer {
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.24+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
} else {
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
}
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintWARNINGDefaultWithUnsupportedVersion(t *testing.T) {
|
||||
runtimeVersion = "go1.23.12"
|
||||
re := captureOutput(t, func() {
|
||||
SetMode(DebugMode)
|
||||
debugPrintWARNINGDefault()
|
||||
SetMode(TestMode)
|
||||
})
|
||||
assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.24+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||
}
|
||||
|
||||
func TestDebugPrintWARNINGNew(t *testing.T) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user