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

[structs] change package structutil to structs (#81)

* refactor package structutil to structs

* add structs package zh-CN docs
This commit is contained in:
zm
2023-03-15 19:14:19 +08:00
committed by GitHub
parent 7261b281ad
commit 5e66bc6227
12 changed files with 594 additions and 26 deletions

112
structs/field.go Normal file
View File

@@ -0,0 +1,112 @@
package structs
import (
"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
tag *Tag
}
func newField(v reflect.Value, f reflect.StructField, tagName string) *Field {
tag := f.Tag.Get(tagName)
field := &Field{
field: f,
tag: newTag(tag),
}
field.rvalue = v
field.rtype = v.Type()
field.TagName = tagName
return field
}
// Tag returns the value that the key in the tag string.
func (f *Field) Tag() *Tag {
return f.tag
}
// Value returns the underlying value of the field.
func (f *Field) Value() any {
return f.rvalue.Interface()
}
// IsEmbedded returns true if the given field is an embedded field.
func (f *Field) IsEmbedded() bool {
return len(f.field.Index) > 1
}
// IsExported returns true if the given field is exported.
func (f *Field) IsExported() bool {
return f.field.IsExported()
}
// IsZero returns true if the given field is zero value.
func (f *Field) IsZero() bool {
z := reflect.Zero(f.rvalue.Type()).Interface()
v := f.Value()
return reflect.DeepEqual(z, v)
}
// Name returns the name of the given field
func (f *Field) Name() string {
return f.field.Name
}
// Kind returns the field's kind
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 covert field value to map
func (f *Field) mapValue(value any) any {
val := pointer.ExtractPointer(value)
v := reflect.ValueOf(val)
var ret any
switch v.Kind() {
case reflect.Struct:
s := New(val)
s.TagName = f.TagName
m, _ := s.ToMap()
ret = m
case reflect.Map:
mapEl := v.Type().Elem()
switch mapEl.Kind() {
case reflect.Ptr, reflect.Array, reflect.Map, reflect.Slice, reflect.Chan:
// iterate the map
m := make(map[string]any, v.Len())
for _, key := range v.MapKeys() {
m[key.String()] = f.mapValue(v.MapIndex(key).Interface())
}
ret = m
default:
ret = v.Interface()
}
case reflect.Slice, reflect.Array:
sEl := v.Type().Elem()
switch sEl.Kind() {
case reflect.Ptr, reflect.Array, reflect.Map, reflect.Slice, reflect.Chan:
slices := make([]any, v.Len())
for i := 0; i < v.Len(); i++ {
slices[i] = f.mapValue(v.Index(i).Interface())
}
ret = slices
default:
ret = v.Interface()
}
default:
ret = v.Interface()
}
return ret
}

272
structs/field_test.go Normal file
View File

@@ -0,0 +1,272 @@
package structs
import (
"github.com/duke-git/lancet/v2/internal"
"reflect"
"testing"
)
func TestField_Tag(t *testing.T) {
assert := internal.NewAssert(t, "TestField_Tag")
type Parent struct {
Name string `json:"name,omitempty"`
}
p1 := &Parent{"111"}
s := New(p1)
n, _ := s.Field("Name")
tag := n.Tag()
assert.Equal("name", tag.Name)
assert.Equal(true, tag.HasOption("omitempty"))
}
func TestField_Value(t *testing.T) {
assert := internal.NewAssert(t, "TestField_Value")
type Parent struct {
Name string `json:"name,omitempty"`
}
p1 := &Parent{"111"}
s := New(p1)
n, _ := s.Field("Name")
assert.Equal("111", n.Value())
}
func TestField_IsEmbedded(t *testing.T) {
assert := internal.NewAssert(t, "TestField_IsEmbedded")
type Parent struct {
Name string
}
type Child struct {
Parent
Age int
}
c1 := &Child{}
c1.Name = "111"
c1.Age = 11
s := New(c1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
assert.Equal(true, n.IsEmbedded())
assert.Equal(false, a.IsEmbedded())
}
func TestField_IsExported(t *testing.T) {
assert := internal.NewAssert(t, "TestField_IsEmbedded")
type Parent struct {
Name string
age int
}
p1 := &Parent{Name: "11", age: 11}
s := New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("age")
assert.Equal(true, n.IsExported())
assert.Equal(false, a.IsExported())
}
func TestField_IsZero(t *testing.T) {
assert := internal.NewAssert(t, "TestField_IsZero")
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
assert.Equal(true, n.IsZero())
assert.Equal(false, a.IsZero())
}
func TestField_Name(t *testing.T) {
assert := internal.NewAssert(t, "TestField_Name")
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
assert.Equal("Name", n.Name())
assert.Equal("Age", a.Name())
}
func TestField_Kind(t *testing.T) {
assert := internal.NewAssert(t, "TestField_Kind")
type Parent struct {
Name string
Age int
}
p1 := &Parent{Age: 11}
s := New(p1)
n, _ := s.Field("Name")
a, _ := s.Field("Age")
assert.Equal(reflect.String, n.Kind())
assert.Equal(reflect.Int, a.Kind())
}
func TestField_IsSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestField_IsSlice")
type Parent struct {
Name string
arr []int
}
p1 := &Parent{arr: []int{1, 2, 3}}
s := New(p1)
a, _ := s.Field("arr")
assert.Equal(true, a.IsSlice())
}
func TestField_MapValue(t *testing.T) {
assert := internal.NewAssert(t, "TestField_MapValue")
t.Run("nested struct", func(t *testing.T) {
type Child struct {
Name string `json:"name"`
}
type Parent struct {
Name string `json:"name"`
Child *Child `json:"child"`
}
c1 := &Child{"11-1"}
p1 := &Parent{
Name: "11",
Child: c1,
}
s := New(p1)
f, ok := s.Field("Child")
val := f.mapValue(f.Value())
assert.Equal(true, ok)
assert.Equal(map[string]any{"name": "11-1"}, val)
})
t.Run("nested ptr struct", func(t *testing.T) {
type Child struct {
Name string `json:"name"`
}
type Parent struct {
Name string `json:"name"`
Child any `json:"child"`
}
c1 := &Child{"11-1"}
c2 := &c1
c3 := &c2
p1 := &Parent{
Name: "11",
Child: c3,
}
s := New(p1)
f, ok := s.Field("Child")
val := f.mapValue(f.Value())
assert.Equal(true, ok)
assert.Equal(map[string]any{"name": "11-1"}, val)
})
t.Run("nested array", func(t *testing.T) {
type Parent struct {
Name string `json:"name"`
Child []int `json:"child"`
}
p1 := &Parent{
Name: "11",
Child: []int{1, 2, 3},
}
s := New(p1)
f, ok := s.Field("Child")
val := f.mapValue(f.Value())
assert.Equal(true, ok)
assert.Equal([]int{1, 2, 3}, val)
})
t.Run("nested array struct", func(t *testing.T) {
type Child struct {
Name string `json:"name"`
}
type Parent struct {
Name string `json:"name"`
Child []*Child `json:"child"`
}
c1 := &Child{"11-1"}
c2 := &Child{"11-2"}
p1 := &Parent{
Name: "11",
Child: []*Child{c1, c2},
}
s := New(p1)
f, ok := s.Field("Child")
val := f.mapValue(f.Value())
assert.Equal(true, ok)
arr := []any{map[string]any{"name": "11-1"}, map[string]any{"name": "11-2"}}
assert.Equal(arr, val)
})
t.Run("nested ptr array struct", func(t *testing.T) {
type Child struct {
Name string `json:"name"`
}
type Parent struct {
Name string `json:"name"`
Child *[]*Child `json:"child"`
}
c1 := &Child{"11-1"}
c2 := &Child{"11-2"}
p1 := &Parent{
Name: "11",
Child: &[]*Child{c1, c2},
}
s := New(p1)
f, ok := s.Field("Child")
val := f.mapValue(f.Value())
assert.Equal(true, ok)
arr := []any{map[string]any{"name": "11-1"}, map[string]any{"name": "11-2"}}
assert.Equal(arr, val)
})
t.Run("nested map in struct", func(t *testing.T) {
type Parent struct {
Name string `json:"name"`
Child map[string]any `json:"child"`
}
p1 := &Parent{
Name: "11",
Child: map[string]any{"a": 1, "b": map[string]any{"name": "11-1"}},
}
s := New(p1)
f, ok := s.Field("Child")
val := f.mapValue(f.Value())
assert.Equal(true, ok)
assert.Equal(map[string]any{"a": 1, "b": map[string]any{"name": "11-1"}}, val)
})
}

116
structs/struct.go Normal file
View File

@@ -0,0 +1,116 @@
package structs
import (
"reflect"
"github.com/duke-git/lancet/v2/pointer"
)
// defaultTagName is the default tag for struct fields to lookup.
var defaultTagName = "json"
// Struct is abstract struct for provide several high level functions
type Struct struct {
raw any
rtype reflect.Type
rvalue reflect.Value
TagName string
}
// New returns a new *Struct
func New(value any, tagName ...string) *Struct {
value = pointer.ExtractPointer(value)
v := reflect.ValueOf(value)
t := reflect.TypeOf(value)
tn := defaultTagName
if len(tagName) > 0 {
tn = tagName[0]
}
// if need: can also set defaultTagName to tn across structs package level
// defaultTagName = tn
return &Struct{
raw: value,
rtype: t,
rvalue: v,
TagName: tn,
}
}
// ToMap converts the given struct to a map[string]any, where the keys
// of the keys are the field names and the values of the map are the values
// of the fields. The default map key is the struct field name, but you can
// change it. The `json` key is the default tag key. Example:
//
// // default
// Name string `json:"name"`
//
// // ignore the field
// Name string // no tag
// Age string `json:"-"` // json ignore tag
// sex string // unexported field
// Goal int `json:"goal,omitempty"` // omitempty if the field is zero value
//
// // custom map key
// Name string `json:"myName"`
//
// ToMap convert the exported fields of a struct to map.
func (s *Struct) ToMap() (map[string]any, error) {
if !s.IsStruct() {
return nil, errInvalidStruct(s)
}
result := make(map[string]any)
fields := s.Fields()
for _, f := range fields {
if !f.IsExported() || f.tag.IsEmpty() || f.tag.Name == "-" {
continue
}
if f.IsZero() && f.tag.HasOption("omitempty") {
continue
}
result[f.tag.Name] = f.mapValue(f.Value())
}
return result, nil
}
// Fields returns all the struct fields within a slice
func (s *Struct) Fields() []*Field {
var fields []*Field
fieldNum := s.rvalue.NumField()
for i := 0; i < fieldNum; i++ {
v := s.rvalue.Field(i)
sf := s.rtype.Field(i)
field := newField(v, sf, s.TagName)
fields = append(fields, field)
}
return fields
}
// Field returns a Field if the given field name was found
func (s *Struct) Field(name string) (*Field, bool) {
f, ok := s.rtype.FieldByName(name)
if !ok {
return nil, false
}
return newField(s.rvalue.FieldByName(name), f, s.TagName), true
}
// IsStruct returns true if the given rvalue is a struct
func (s *Struct) IsStruct() bool {
k := s.rvalue.Kind()
if k == reflect.Invalid {
return false
}
return k == reflect.Struct
}
// ToMap convert struct to map, only convert exported struct field
// map key is specified same as struct field tag `json` value.
func ToMap(v any) (map[string]any, error) {
return New(v).ToMap()
}

View File

@@ -0,0 +1,7 @@
package structs
import "fmt"
func errInvalidStruct(v any) error {
return fmt.Errorf("invalid struct %v", v)
}

132
structs/struct_test.go Normal file
View File

@@ -0,0 +1,132 @@
package structs
import (
"reflect"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestStruct_ToMap(t *testing.T) {
assert := internal.NewAssert(t, "TestStruct_ToMap")
t.Run("invalid struct", func(t *testing.T) {
m, _ := ToMap(1)
var expected map[string]any
assert.Equal(expected, m)
})
t.Run("StructToMap", func(_ *testing.T) {
type People struct {
Name string `json:"name"`
age int
}
p := &People{
"test",
100,
}
pm, _ := ToMap(p)
var expected = map[string]any{"name": "test"}
assert.Equal(expected, pm)
})
t.Run("StructToMapWithJsonAttr", func(_ *testing.T) {
type People struct {
Name string `json:"name,omitempty"` // json tag with attribute
Phone string `json:"phone"` // json tag without attribute
Sex string `json:"-"` // ignore by "-"
Age int // ignore by no tag
email string // ignore by unexported
IsWorking bool `json:"is_working"`
}
p1 := People{
Name: "AAA", // exist
Phone: "1111",
Sex: "male",
Age: 100,
email: "11@gmail.com",
}
p1m, _ := ToMap(p1)
var expect1 = map[string]any{"name": "AAA", "phone": "1111", "is_working": false}
assert.Equal(expect1, p1m)
p2 := People{
Name: "",
Phone: "2222",
Sex: "male",
Age: 0,
email: "22@gmail.com",
IsWorking: true,
}
p2m, _ := ToMap(p2)
var expect2 = map[string]any{"phone": "2222", "is_working": true}
assert.Equal(expect2, p2m)
})
}
func TestStruct_Fields(t *testing.T) {
assert := internal.NewAssert(t, "TestStruct_Fields")
type Parent struct {
A string `json:"a"`
B int `json:"b"`
C []string `json:"c"`
D map[string]any `json:"d"`
}
p1 := &Parent{
A: "1",
B: 11,
C: []string{"11", "22"},
D: map[string]any{"d1": 1, "d2": 2},
}
s := New(p1)
fields := s.Fields()
assert.Equal(4, len(fields))
assert.Equal(reflect.String, fields[0].Kind())
assert.Equal(reflect.Int, fields[1].Kind())
assert.Equal(reflect.Slice, fields[2].Kind())
assert.Equal(reflect.Map, fields[3].Kind())
}
func TestStruct_Field(t *testing.T) {
assert := internal.NewAssert(t, "TestStruct_Field")
type Parent struct {
A string `json:"a"`
B int `json:"b"`
C []string `json:"c"`
D map[string]any `json:"d"`
}
p1 := &Parent{
A: "1",
B: 11,
C: []string{"11", "22"},
D: map[string]any{"d1": 1, "d2": 2},
}
s := New(p1)
a, ok := s.Field("A")
assert.Equal(true, ok)
assert.Equal(reflect.String, a.Kind())
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) {
assert := internal.NewAssert(t, "TestStruct_Field")
type Test1 struct{}
t1 := &Test1{}
t2 := 1
s1 := New(t1)
s2 := New(t2)
assert.Equal(true, s1.IsStruct())
assert.Equal(false, s2.IsStruct())
}

36
structs/tag.go Normal file
View File

@@ -0,0 +1,36 @@
package structs
import (
"strings"
"github.com/duke-git/lancet/v2/validator"
)
// Tag is abstract struct field tag
type Tag struct {
Name string
Options []string
}
func newTag(tag string) *Tag {
res := strings.Split(tag, ",")
return &Tag{
Name: res[0],
Options: res[1:],
}
}
// 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 {
return true
}
}
return false
}
// IsEmpty check if a struct field has tag setting.
func (t *Tag) IsEmpty() bool {
return validator.IsEmptyString(t.Name)
}