mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 02:02:27 +08:00
refactor: add function comment for tag.go
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
package structutil
|
package structutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/pointer"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/pointer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Field is abstract struct field for provide several high level functions
|
||||||
type Field struct {
|
type Field struct {
|
||||||
Struct
|
Struct
|
||||||
field reflect.StructField
|
field reflect.StructField
|
||||||
@@ -59,11 +61,13 @@ func (f *Field) Kind() reflect.Kind {
|
|||||||
return f.rvalue.Kind()
|
return f.rvalue.Kind()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsSlice check if a struct field type is slice or not
|
||||||
func (f *Field) IsSlice() bool {
|
func (f *Field) IsSlice() bool {
|
||||||
k := f.rvalue.Kind()
|
k := f.rvalue.Kind()
|
||||||
return k == reflect.Slice
|
return k == reflect.Slice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MapValue conver 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)
|
||||||
v := reflect.ValueOf(val)
|
v := reflect.ValueOf(val)
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ func New(value any, tagName ...string) *Struct {
|
|||||||
// // custom map key
|
// // custom map key
|
||||||
// Name string `json:"myName"`
|
// Name string `json:"myName"`
|
||||||
//
|
//
|
||||||
// Only the exported fields of a struct can be converted.
|
// ToMap conver the exported fields of a struct to map.
|
||||||
func (s *Struct) ToMap() (map[string]any, error) {
|
func (s *Struct) ToMap() (map[string]any, error) {
|
||||||
if !s.IsStruct() {
|
if !s.IsStruct() {
|
||||||
return nil, errInvalidStruct(s)
|
return nil, errInvalidStruct(s)
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package structutil
|
package structutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStruct_ToMap(t *testing.T) {
|
func TestStruct_ToMap(t *testing.T) {
|
||||||
@@ -113,6 +114,7 @@ func TestStruct_Field(t *testing.T) {
|
|||||||
assert.Equal("1", a.Value())
|
assert.Equal("1", a.Value())
|
||||||
assert.Equal("a", a.tag.Name)
|
assert.Equal("a", a.tag.Name)
|
||||||
assert.Equal(false, a.tag.HasOption("omitempty"))
|
assert.Equal(false, a.tag.HasOption("omitempty"))
|
||||||
|
assert.Equal(false, a.tag.IsEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStruct_IsStruct(t *testing.T) {
|
func TestStruct_IsStruct(t *testing.T) {
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package structutil
|
package structutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/validator"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/validator"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Tag is abstract struct field tag
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
Name string
|
Name string
|
||||||
Options []string
|
Options []string
|
||||||
@@ -18,6 +20,7 @@ func newTag(tag string) *Tag {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HasOption check if a struct field tag has option setting.
|
||||||
func (t *Tag) HasOption(opt string) bool {
|
func (t *Tag) HasOption(opt string) bool {
|
||||||
for _, o := range t.Options {
|
for _, o := range t.Options {
|
||||||
if o == opt {
|
if o == opt {
|
||||||
@@ -27,6 +30,7 @@ func (t *Tag) HasOption(opt string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsEmpty check if a struct field has tag setting.
|
||||||
func (t *Tag) IsEmpty() bool {
|
func (t *Tag) IsEmpty() bool {
|
||||||
return validator.IsEmptyString(t.Name)
|
return validator.IsEmptyString(t.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user