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:
26
README.md
26
README.md
@@ -1453,29 +1453,31 @@ import "github.com/duke-git/lancet/v2/structs"
|
|||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- **<big>New</big>** : creates a `Struct` instance.
|
- **<big>New</big>** : creates a `Struct` instance.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/struct.md#New)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#New)]
|
||||||
- **<big>ToMap</big>** : converts a valid struct to a map.
|
- **<big>ToMap</big>** : converts a valid struct to a map.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/struct.md#ToMap)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#ToMap)]
|
||||||
- **<big>Fields</big>** : get all fields of a given struct, that the fields are abstract struct field.
|
- **<big>Fields</big>** : get all fields of a given struct, that the fields are abstract struct field.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/struct.md#Fields)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Fields)]
|
||||||
- **<big>IsStruct</big>** : check if the struct is valid.
|
- **<big>IsStruct</big>** : check if the struct is valid.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/struct.md#IsStruct)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsStruct)]
|
||||||
- **<big>Tag</big>** : get a `Tag` of the `Field`, `Tag` is a abstract struct field tag
|
- **<big>Tag</big>** : get a `Tag` of the `Field`, `Tag` is a abstract struct field tag
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/field.md#Tag)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Tag)]
|
||||||
- **<big>Name</big>** : get the field name.
|
- **<big>Name</big>** : get the field name.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/field.md#Name)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Name)]
|
||||||
- **<big>Value</big>** : get the `Field` underlying value.
|
- **<big>Value</big>** : get the `Field` underlying value.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/field.md#Value)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Value)]
|
||||||
- **<big>Kind</big>** : get the field's kind
|
- **<big>Kind</big>** : get the field's kind
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/field.md#Kind)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#Kind)]
|
||||||
- **<big>IsEmbedded</big>** : check if the field is an embedded field.
|
- **<big>IsEmbedded</big>** : check if the field is an embedded field.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/field.md#IsEmbedded)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsEmbedded)]
|
||||||
- **<big>IsExported</big>** : check if the field is exporte
|
- **<big>IsExported</big>** : check if the field is exporte
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/field.md#IsExported)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsExported)]
|
||||||
- **<big>IsZero</big>** : check if the field is zero value
|
- **<big>IsZero</big>** : check if the field is zero value
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/field.md#IsZero)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsZero)]
|
||||||
- **<big>IsSlice</big>** : check if the field is a slice
|
- **<big>IsSlice</big>** : check if the field is a slice
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/structs/field.md#IsSlice)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsSlice)]
|
||||||
|
- **<big>IsTargetType</big>** : check if the field is target type
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#IsTargetType)]
|
||||||
|
|
||||||
<h3 id="strutil"> 21. Strutil package contains some functions to manipulate string. <a href="#index">index</a></h3>
|
<h3 id="strutil"> 21. Strutil package contains some functions to manipulate string. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
|
|||||||
@@ -1452,31 +1452,33 @@ import "github.com/duke-git/lancet/v2/structs"
|
|||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- **<big>New</big>** : `Struct`结构体的构造函数。
|
- **<big>New</big>** : `Struct`结构体的构造函数。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/struct.md#New)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#New)]
|
||||||
- **<big>ToMap</big>** : 将一个合法的 struct 对象转换为 map[string]any。
|
- **<big>ToMap</big>** : 将一个合法的 struct 对象转换为 map[string]any。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/struct.md#ToMap)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#ToMap)]
|
||||||
- **<big>Fields</big>** : 获取一个 struct 对象的属性列表。
|
- **<big>Fields</big>** : 获取一个 struct 对象的属性列表。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/struct.md#Fields)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Fields)]
|
||||||
- **<big>Field</big>** : 根据属性名获取一个 struct 对象的属性。
|
- **<big>Field</big>** : 根据属性名获取一个 struct 对象的属性。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/struct.md#Fields)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Fields)]
|
||||||
- **<big>IsStruct</big>** : 判断是否为一个合法的 struct 对象。
|
- **<big>IsStruct</big>** : 判断是否为一个合法的 struct 对象。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/struct.md#IsStruct)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsStruct)]
|
||||||
- **<big>Tag</big>** : 获取`Field`的`Tag`,默认的 tag key 是 json。
|
- **<big>Tag</big>** : 获取`Field`的`Tag`,默认的 tag key 是 json。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/field.md#Tag)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Tag)]
|
||||||
- **<big>Name</big>** : 获取属性名。
|
- **<big>Name</big>** : 获取属性名。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/field.md#Name)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Name)]
|
||||||
- **<big>Value</big>** : 获取`Field`属性的值。
|
- **<big>Value</big>** : 获取`Field`属性的值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/field.md#Value)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Value)]
|
||||||
- **<big>Kind</big>** : 获取属性 Kind。
|
- **<big>Kind</big>** : 获取属性 Kind。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/field.md#Kind)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#Kind)]
|
||||||
- **<big>IsEmbedded</big>** : 判断属性是否为嵌入。
|
- **<big>IsEmbedded</big>** : 判断属性是否为嵌入。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/field.md#IsEmbedded)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsEmbedded)]
|
||||||
- **<big>IsExported</big>** : 判断属性是否导出。
|
- **<big>IsExported</big>** : 判断属性是否导出。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/field.md#IsExported)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsExported)]
|
||||||
- **<big>IsZero</big>** : 判断属性是否为零值。
|
- **<big>IsZero</big>** : 判断属性是否为零值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/field.md#IsZero)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsZero)]
|
||||||
- **<big>IsSlice</big>** : 判断属性是否是切片。
|
- **<big>IsSlice</big>** : 判断属性是否是切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/structs/field.md#IsSlice)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsSlice)]
|
||||||
|
- **<big>IsTargetType</big>** : 判断属性是否是目标类型。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#IsTargetType)]
|
||||||
|
|
||||||
<h3 id="strutil"> 21. strutil 包含字符串处理的相关函数。 <a href="#index">回到目录</a></h3>
|
<h3 id="strutil"> 21. strutil 包含字符串处理的相关函数。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import (
|
|||||||
- [IsExported](#IsExported)
|
- [IsExported](#IsExported)
|
||||||
- [IsZero](#IsZero)
|
- [IsZero](#IsZero)
|
||||||
- [IsSlice](#IsSlice)
|
- [IsSlice](#IsSlice)
|
||||||
|
- [IsTargetType](#IsTargetType)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -531,4 +532,45 @@ func main() {
|
|||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="IsTargetType">IsTargetType</span>
|
||||||
|
|
||||||
|
<p>判断属性是否是目标类型</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (f *Field) IsTargetType(targetType reflect.Kind) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"github.com/duke-git/lancet/v2/structs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type Parent struct {
|
||||||
|
Name string
|
||||||
|
arr []int
|
||||||
|
}
|
||||||
|
|
||||||
|
p1 := &Parent{arr: []int{1, 2, 3}}
|
||||||
|
s := structs.New(p1)
|
||||||
|
n, _ := s.Field("Name")
|
||||||
|
a, _ := s.Field("arr")
|
||||||
|
|
||||||
|
fmt.Println(n.IsTargetType(reflect.String))
|
||||||
|
fmt.Println(a.IsTargetType(reflect.Slice))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -37,6 +37,7 @@ import (
|
|||||||
- [IsExported](#IsExported)
|
- [IsExported](#IsExported)
|
||||||
- [IsZero](#IsZero)
|
- [IsZero](#IsZero)
|
||||||
- [IsSlice](#IsSlice)
|
- [IsSlice](#IsSlice)
|
||||||
|
- [IsTargetType](#IsTargetType)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -533,3 +534,44 @@ func main() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="IsTargetType">IsTargetType</span>
|
||||||
|
|
||||||
|
<p>check if a struct field type is target type or not</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (f *Field) IsTargetType(targetType reflect.Kind) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"github.com/duke-git/lancet/v2/structs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type Parent struct {
|
||||||
|
Name string
|
||||||
|
arr []int
|
||||||
|
}
|
||||||
|
|
||||||
|
p1 := &Parent{arr: []int{1, 2, 3}}
|
||||||
|
s := structs.New(p1)
|
||||||
|
n, _ := s.Field("Name")
|
||||||
|
a, _ := s.Field("arr")
|
||||||
|
|
||||||
|
fmt.Println(n.IsTargetType(reflect.String))
|
||||||
|
fmt.Println(a.IsTargetType(reflect.Slice))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -68,6 +68,11 @@ func (f *Field) IsSlice() bool {
|
|||||||
return k == reflect.Slice
|
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
|
// mapValue covert field value to map
|
||||||
func (f *Field) mapValue(value any) any {
|
func (f *Field) mapValue(value any) any {
|
||||||
val := pointer.ExtractPointer(value)
|
val := pointer.ExtractPointer(value)
|
||||||
|
|||||||
@@ -154,6 +154,25 @@ func TestField_IsSlice(t *testing.T) {
|
|||||||
assert.Equal(true, a.IsSlice())
|
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) {
|
func TestField_MapValue(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user