diff --git a/structutil/field.go b/structutil/field.go index 3419172..6be4f70 100644 --- a/structutil/field.go +++ b/structutil/field.go @@ -1,10 +1,12 @@ package structutil import ( - "github.com/duke-git/lancet/v2/pointer" "reflect" + + "github.com/duke-git/lancet/v2/pointer" ) +// Field is abstract struct field for provide several high level functions type Field struct { Struct field reflect.StructField @@ -59,11 +61,13 @@ func (f *Field) Kind() reflect.Kind { return f.rvalue.Kind() } +// IsSlice check if a struct field type is slice or not func (f *Field) IsSlice() bool { k := f.rvalue.Kind() return k == reflect.Slice } +// MapValue conver field value to map. func (f *Field) MapValue(value any) any { val := pointer.ExtractPointer(value) v := reflect.ValueOf(val) diff --git a/structutil/struct.go b/structutil/struct.go index 1b1d28d..914491f 100644 --- a/structutil/struct.go +++ b/structutil/struct.go @@ -57,7 +57,7 @@ func New(value any, tagName ...string) *Struct { // // custom map key // 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) { if !s.IsStruct() { return nil, errInvalidStruct(s) diff --git a/structutil/struct_test.go b/structutil/struct_test.go index f5da905..eb64b61 100644 --- a/structutil/struct_test.go +++ b/structutil/struct_test.go @@ -1,9 +1,10 @@ package structutil import ( - "github.com/duke-git/lancet/v2/internal" "reflect" "testing" + + "github.com/duke-git/lancet/v2/internal" ) func TestStruct_ToMap(t *testing.T) { @@ -113,6 +114,7 @@ func TestStruct_Field(t *testing.T) { assert.Equal("1", a.Value()) assert.Equal("a", a.tag.Name) assert.Equal(false, a.tag.HasOption("omitempty")) + assert.Equal(false, a.tag.IsEmpty()) } func TestStruct_IsStruct(t *testing.T) { diff --git a/structutil/tag.go b/structutil/tag.go index 452147b..cd9f34c 100644 --- a/structutil/tag.go +++ b/structutil/tag.go @@ -1,10 +1,12 @@ package structutil import ( - "github.com/duke-git/lancet/v2/validator" "strings" + + "github.com/duke-git/lancet/v2/validator" ) +// Tag is abstract struct field tag type Tag struct { Name 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 { for _, o := range t.Options { if o == opt { @@ -27,6 +30,7 @@ func (t *Tag) HasOption(opt string) bool { return false } +// IsEmpty check if a struct field has tag setting. func (t *Tag) IsEmpty() bool { return validator.IsEmptyString(t.Name) }