1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

refactor: add function comment for tag.go

This commit is contained in:
dudaodong
2023-03-15 17:43:47 +08:00
parent f79693804b
commit 7261b281ad
4 changed files with 14 additions and 4 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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)
}