1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +08:00
This commit is contained in:
2022-04-29 17:18:27 +08:00
parent 830a870158
commit f6ecb9be4b
4 changed files with 62 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package structs
import (
"reflect"
"strings"
"github.com/charlienet/go-mixed/expr"
)
@ -20,10 +21,20 @@ func parseField(fi reflect.StructField, opt option) field {
name: fi.Name,
tagName: expr.If(isValidTag(name), name, expr.If(opt.NameConverter != nil, opt.NameConverter(fi.Name), fi.Name)),
ignoreEmpty: opt.IgnoreEmpty || (opts.Contains("omitempty") && opt.Omitempty),
ignore: name == "-" && opt.Ignore,
ignore: (name == "-" && opt.Ignore) || isSkipField(fi.Name, opt.SkipFields),
}
}
func (f field) shouldIgnore(s reflect.Value) bool {
return f.ignore || (s.IsZero() && f.ignoreEmpty)
}
func isSkipField(name string, skips []string) bool {
for _, v := range skips {
if strings.EqualFold(v, name) {
return true
}
}
return false
}

View File

@ -21,9 +21,21 @@ func TestStructToMap(t *testing.T) {
OmitEmpty: 0,
}
t.Log(structs.ToMap(o, structs.TagName("struct")))
t.Log(structs.ToMap(o, structs.IgnoreEmpty()))
t.Log(structs.ToMap(o, structs.Omitempty()))
t.Log(structs.ToMap(o, structs.Lcfirst()))
t.Log(structs.ToMap(o, structs.Camel2Case()))
t.Log(structs.Struct2Map(o, structs.TagName("struct")))
t.Log(structs.Struct2Map(o, structs.IgnoreEmpty()))
t.Log(structs.Struct2Map(o, structs.Omitempty()))
t.Log(structs.Struct2Map(o, structs.Lcfirst()))
t.Log(structs.Struct2Map(o, structs.Camel2Case()))
}
func TestMapToStruct(t *testing.T) {
}
func TestMap2Map(t *testing.T) {
source := map[string]any{
"Abc": 143,
}
structs.New(source)
}

View File

@ -7,6 +7,7 @@ import (
type optionFunc func(*option)
type option struct {
SkipFields []string
TagName string
DeepCopy bool
Omitempty bool
@ -39,6 +40,18 @@ func DeepCopy() optionFunc {
}
}
func SkipField(field string) optionFunc {
return func(o *option) {
o.SkipFields = append(o.SkipFields, field)
}
}
func SkipFields(fields []string) optionFunc {
return func(o *option) {
o.SkipFields = append(o.SkipFields, fields...)
}
}
func Lcfirst() optionFunc {
return func(o *option) {
o.NameConverter = json.Lcfirst

View File

@ -67,6 +67,12 @@ func (s *Struct) ToMap() map[string]any {
return m
}
/*
struct -> map
struct -> struct
map -> struct
map -> map
*/
func (s *Struct) Copy(dest any) error {
to := indirect(reflect.ValueOf(dest))
@ -102,7 +108,11 @@ func (s *Struct) getByName(name string) (field, bool) {
return field{}, false
}
func ToMap(o any, opts ...optionFunc) map[string]any {
func Map2Struct(o map[string]any, dst any) {
}
func Struct2Map(o any, opts ...optionFunc) map[string]any {
return New(o, opts...).ToMap()
}
@ -112,6 +122,15 @@ func Copy(source, dst any, opts ...optionFunc) {
func parseFields(t reflect.Value, opt option) []field {
typ := indirectType(t.Type())
if typ.Kind() == reflect.Struct {
return parseStructFields(typ, opt)
}
return nil
}
func parseStructFields(typ reflect.Type, opt option) []field {
num := typ.NumField()
fields := make([]field, 0, num)
for i := 0; i < num; i++ {