From 692df4395802d14031c0dea90d12ffc48f11174f Mon Sep 17 00:00:00 2001 From: seekwe Date: Mon, 2 Dec 2024 19:27:45 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20refactor:=20Change=20the?= =?UTF-8?q?=20writing=20style?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zreflect/quick.go | 13 +++++-------- ztype/conv.go | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/zreflect/quick.go b/zreflect/quick.go index 1cf2a17..7255efd 100644 --- a/zreflect/quick.go +++ b/zreflect/quick.go @@ -23,15 +23,13 @@ func ForEachMethod(valof reflect.Value, fn func(index int, method reflect.Method return nil } -var ( - // SkipChild Field is returned when a struct field is skipped. - SkipChild = errors.New("skip struct") -) +// SkipChild Field is returned when a struct field is skipped. +var SkipChild = errors.New("skip struct") // ForEach For Each Struct field func ForEach(typ reflect.Type, fn func(parent []string, index int, tag string, field reflect.StructField) error) (err error) { var forField func(typ reflect.Type, parent []string) error - if typ.Kind() == reflect.Pointer { + if typ.Kind() == reflect.Ptr { typ = typ.Elem() } forField = func(typ reflect.Type, parent []string) error { @@ -63,9 +61,8 @@ func ForEachValue(val reflect.Value, fn func(parent []string, index int, tag str if !Nonzero(val) { return errors.New("reflect.Value is zero") } - if val.Kind() == reflect.Pointer { - val = val.Elem() - } + + val = reflect.Indirect(val) typ := toRType(NewType(val)) var forField func(val reflect.Value, typ reflect.Type, parent []string) error forField = func(val reflect.Value, typ reflect.Type, parent []string) error { diff --git a/ztype/conv.go b/ztype/conv.go index 7b202c6..30159f3 100644 --- a/ztype/conv.go +++ b/ztype/conv.go @@ -415,7 +415,7 @@ func (d *Conver) toMapFromStruct(name string, dataVal reflect.Value, val reflect return fmt.Errorf("cannot assign type '%s' to map value field of type '%s'", vTyp, valMap.Type().Elem()) } - if vTyp.Kind() == reflect.Pointer && !v.IsZero() { + if vTyp.Kind() == reflect.Ptr && !v.IsZero() { vTyp = v.Elem().Type() }