mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
add new func & docs error repair (#153)
* feat: add method IsTargetType() * docs:document error repair
This commit is contained in:
@@ -68,6 +68,11 @@ func (f *Field) IsSlice() bool {
|
||||
return k == reflect.Slice
|
||||
}
|
||||
|
||||
// IsTargetType check if a struct field type is target type or not
|
||||
func (f *Field) IsTargetType(targetType reflect.Kind) bool {
|
||||
return f.rvalue.Kind() == targetType
|
||||
}
|
||||
|
||||
// mapValue covert field value to map
|
||||
func (f *Field) mapValue(value any) any {
|
||||
val := pointer.ExtractPointer(value)
|
||||
|
||||
@@ -154,6 +154,25 @@ func TestField_IsSlice(t *testing.T) {
|
||||
assert.Equal(true, a.IsSlice())
|
||||
}
|
||||
|
||||
func TestField_IsTargetType(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestField_IsTargetType")
|
||||
|
||||
type Parent struct {
|
||||
Name string
|
||||
arr []int
|
||||
}
|
||||
|
||||
p1 := &Parent{Name: "test", arr: []int{1, 2, 3}}
|
||||
s := New(p1)
|
||||
n, _ := s.Field("Name")
|
||||
a, _ := s.Field("arr")
|
||||
|
||||
assert.Equal(true, n.IsTargetType(reflect.String))
|
||||
assert.Equal(true, a.IsTargetType(reflect.Slice))
|
||||
}
|
||||
|
||||
func TestField_MapValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user