mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
add Ptr* functions for package gconv (#2206)
* add Ptr* functions for package gconv * delete binary file * revert changes for file gconv.go Co-authored-by: houseme <housemecn@gmail.com>
This commit is contained in:
@ -79,7 +79,7 @@ func Bytes(any interface{}) []byte {
|
||||
ok = true
|
||||
bytes = make([]byte, originValueAndKind.OriginValue.Len())
|
||||
)
|
||||
for i := range bytes {
|
||||
for i, _ := range bytes {
|
||||
int32Value := Int32(originValueAndKind.OriginValue.Index(i).Interface())
|
||||
if int32Value < 0 || int32Value > math.MaxUint8 {
|
||||
ok = false
|
||||
@ -112,7 +112,7 @@ func Runes(any interface{}) []rune {
|
||||
}
|
||||
|
||||
// String converts `any` to string.
|
||||
// It's most commonly used converting function
|
||||
// It's most commonly used converting function.
|
||||
func String(any interface{}) string {
|
||||
if any == nil {
|
||||
return ""
|
||||
|
||||
96
util/gconv/gconv_ptr.go
Normal file
96
util/gconv/gconv_ptr.go
Normal file
@ -0,0 +1,96 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gconv
|
||||
|
||||
// PtrAny creates and returns an interface{} pointer variable to this value.
|
||||
func PtrAny(any interface{}) *interface{} {
|
||||
return &any
|
||||
}
|
||||
|
||||
// PtrString creates and returns a string pointer variable to this value.
|
||||
func PtrString(any interface{}) *string {
|
||||
v := String(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrBool creates and returns a bool pointer variable to this value.
|
||||
func PtrBool(any interface{}) *bool {
|
||||
v := Bool(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrInt creates and returns an int pointer variable to this value.
|
||||
func PtrInt(any interface{}) *int {
|
||||
v := Int(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrInt8 creates and returns an int8 pointer variable to this value.
|
||||
func PtrInt8(any interface{}) *int8 {
|
||||
v := Int8(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrInt16 creates and returns an int16 pointer variable to this value.
|
||||
func PtrInt16(any interface{}) *int16 {
|
||||
v := Int16(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrInt32 creates and returns an int32 pointer variable to this value.
|
||||
func PtrInt32(any interface{}) *int32 {
|
||||
v := Int32(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrInt64 creates and returns an int64 pointer variable to this value.
|
||||
func PtrInt64(any interface{}) *int64 {
|
||||
v := Int64(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrUint creates and returns an uint pointer variable to this value.
|
||||
func PtrUint(any interface{}) *uint {
|
||||
v := Uint(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrUint8 creates and returns an uint8 pointer variable to this value.
|
||||
func PtrUint8(any interface{}) *uint8 {
|
||||
v := Uint8(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrUint16 creates and returns an uint16 pointer variable to this value.
|
||||
func PtrUint16(any interface{}) *uint16 {
|
||||
v := Uint16(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrUint32 creates and returns an uint32 pointer variable to this value.
|
||||
func PtrUint32(any interface{}) *uint32 {
|
||||
v := Uint32(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrUint64 creates and returns an uint64 pointer variable to this value.
|
||||
func PtrUint64(any interface{}) *uint64 {
|
||||
v := Uint64(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrFloat32 creates and returns a float32 pointer variable to this value.
|
||||
func PtrFloat32(any interface{}) *float32 {
|
||||
v := Float32(any)
|
||||
return &v
|
||||
}
|
||||
|
||||
// PtrFloat64 creates and returns a float64 pointer variable to this value.
|
||||
func PtrFloat64(any interface{}) *float64 {
|
||||
v := Float64(any)
|
||||
return &v
|
||||
}
|
||||
77
util/gconv/gconv_z_unit_ptr_test.go
Normal file
77
util/gconv/gconv_z_unit_ptr_test.go
Normal file
@ -0,0 +1,77 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gconv_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
func Test_Ptr_Functions(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v interface{} = 1
|
||||
t.AssertEQ(gconv.PtrAny(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v string = "1"
|
||||
t.AssertEQ(gconv.PtrString(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v bool = true
|
||||
t.AssertEQ(gconv.PtrBool(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v int = 1
|
||||
t.AssertEQ(gconv.PtrInt(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v int8 = 1
|
||||
t.AssertEQ(gconv.PtrInt8(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v int16 = 1
|
||||
t.AssertEQ(gconv.PtrInt16(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v int32 = 1
|
||||
t.AssertEQ(gconv.PtrInt32(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v int64 = 1
|
||||
t.AssertEQ(gconv.PtrInt64(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v uint = 1
|
||||
t.AssertEQ(gconv.PtrUint(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v uint8 = 1
|
||||
t.AssertEQ(gconv.PtrUint8(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v uint16 = 1
|
||||
t.AssertEQ(gconv.PtrUint16(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v uint32 = 1
|
||||
t.AssertEQ(gconv.PtrUint32(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v uint64 = 1
|
||||
t.AssertEQ(gconv.PtrUint64(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v float32 = 1.01
|
||||
t.AssertEQ(gconv.PtrFloat32(v), &v)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var v float64 = 1.01
|
||||
t.AssertEQ(gconv.PtrFloat64(v), &v)
|
||||
})
|
||||
}
|
||||
@ -14,6 +14,7 @@ import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
func Test_CheckStruct(t *testing.T) {
|
||||
@ -467,3 +468,47 @@ func TestValidator_CheckStructWithData(t *testing.T) {
|
||||
t.AssertNE(g.Validator().Data(data).Assoc(g.Map{"start_time": gtime.Now()}).Run(context.TODO()), nil)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_CheckStruct_PointerAttribute(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Req struct {
|
||||
Name string
|
||||
Age *uint `v:"min:18"`
|
||||
}
|
||||
req := &Req{
|
||||
Name: "john",
|
||||
Age: gconv.PtrUint(0),
|
||||
}
|
||||
err := g.Validator().Data(req).Run(context.TODO())
|
||||
t.Assert(err.String(), "The Age value `0` must be equal or greater than 18")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Req struct {
|
||||
Name string `v:"min-length:3"`
|
||||
Age *uint `v:"min:18"`
|
||||
}
|
||||
req := &Req{
|
||||
Name: "j",
|
||||
Age: gconv.PtrUint(19),
|
||||
}
|
||||
err := g.Validator().Data(req).Run(context.TODO())
|
||||
t.Assert(err.String(), "The Name value `j` length must be equal or greater than 3")
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Params struct {
|
||||
Age *uint `v:"min:18"`
|
||||
}
|
||||
type Req struct {
|
||||
Name string
|
||||
Params *Params
|
||||
}
|
||||
req := &Req{
|
||||
Name: "john",
|
||||
Params: &Params{
|
||||
Age: gconv.PtrUint(0),
|
||||
},
|
||||
}
|
||||
err := g.Validator().Data(req).Run(context.TODO())
|
||||
t.Assert(err.String(), "The Age value `0` must be equal or greater than 18")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user