Skip to content

Commit

Permalink
update main codes to v1
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Feb 10, 2024
1 parent bb7c9a6 commit 182c505
Show file tree
Hide file tree
Showing 19 changed files with 2,887 additions and 567 deletions.
27 changes: 14 additions & 13 deletions 00_testcase_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"reflect"
"testing"

"github.com/hedzr/log"

"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/diff"
"github.com/hedzr/evendeep/flags/cms"
"github.com/hedzr/evendeep/internal/tool"
"github.com/hedzr/evendeep/ref"
"github.com/hedzr/evendeep/typ"
logz "github.com/hedzr/logg/slog"

"gopkg.in/hedzr/errors.v3"
)
Expand Down Expand Up @@ -72,17 +71,17 @@ func NewForTest() DeepCopier {
var c1 = newCopier()
WithStrategies(cms.SliceMerge, cms.MapMerge)(c1)
if c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceMerge, cms.MapMerge, cms.OmitIfEmpty, cms.Default) == false {
log.Panicf("except flag set with optional values but not matched, 1")
logz.Panic("except flag set with optional values but not matched, 1")
}
c1 = newDeepCopier()
WithStrategies(cms.SliceCopyAppend, cms.MapCopy)(c1)
if c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceCopyAppend, cms.MapCopy, cms.OmitIfEmpty, cms.Default) == false {
log.Panicf("except flag set with optional values but not matched, 2")
logz.Panic("except flag set with optional values but not matched, 2")
}
c1 = newCloner()
WithStrategies(cms.SliceCopy)(c1)
if c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceCopy, cms.MapCopy, cms.OmitIfEmpty, cms.Default) == false {
log.Panicf("except flag set with optional values but not matched, 3")
logz.Panic("except flag set with optional values but not matched, 3")
}

copier = NewFlatDeepCopier(
Expand Down Expand Up @@ -188,12 +187,12 @@ func DefaultDeepCopyTestRunner(ix int, tc TestCase, opts ...Opt) func(t *testing

// t.Logf("\nexpect: %+v\n got: %+v.", tc.expect, tc.dst)
if err = verifier(tc.Src, tc.Dst, tc.Expect, err); err == nil {
log.Printf("%3d. test passed", ix)
logz.Print("test passed", "pass-index", ix)
return
}

log.Errorf("%3d. Error: %v", ix, err)
t.Fatalf("%3d. %s FAILED, %+v", ix, tc.Description, err)
logz.Error("Error occurs", "index", ix, "error", err)
t.Fatal("FAILED", "index", ix, "desc", tc.Description, "error", err)
}
}

Expand All @@ -219,11 +218,13 @@ func runTestCasesWithOpts(t *testing.T, cases []TestCase, opts ...Opt) {
func DoTestCasesVerifier(t *testing.T) Verifier {
return func(src, dst, expect typ.Any, e error) (err error) {
a, b := reflect.ValueOf(dst), reflect.ValueOf(expect)
aa, _ := tool.Rdecode(a)
bb, _ := tool.Rdecode(b)
aa, _ := ref.Rdecode(a)
bb, _ := ref.Rdecode(b)
av, bv := aa.Interface(), bb.Interface()
log.Printf("\nexpect: %+v (%v | %v)\n got: %+v (%v | %v)\n err: %v",
bv, tool.Typfmtv(&bb), aa.Type(), av, tool.Typfmtv(&aa), bb.Type(), e)
logz.Print("mismatched",
logz.Group("expect", "bv", bv, "typ", ref.Typfmtv(&bb), "t", aa.Type()),
logz.Group("got", "av", av, "typ", ref.Typfmtv(&aa), "t", bb.Type()),
"error", e)

dif, equal := diff.New(expect, dst,
diff.WithSliceOrderedComparison(false),
Expand Down
38 changes: 19 additions & 19 deletions 01_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (
"time"
"unsafe"

"github.com/hedzr/log"
"gopkg.in/hedzr/errors.v3"

"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/internal/cl"
"github.com/hedzr/evendeep/internal/tool"
"github.com/hedzr/evendeep/ref"
logz "github.com/hedzr/logg/slog"

"gopkg.in/hedzr/errors.v3"
)

// TestLogNormal _
func TestLogNormal(t *testing.T) {
// config := log.NewLoggerConfigWith(true, "logrus", "trace")
// config := logz.NewLoggerConfigWith(true, "logrus", "trace")
// logger := logrus.NewWithConfig(config)
log.Printf("hello")
log.Infof("hello info")
log.Warnf("hello warn")
log.Errorf("hello error")
log.Debugf("hello debug")
log.Tracef("hello trace")
logz.Print("hello")
logz.Info("hello info")
logz.Warn("hello warn")
logz.Error("hello error")
logz.Debug("hello debug")
logz.Trace("hello trace")

dbglog.Log("but again")
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestSliceLen(t *testing.T) {
v = reflect.Append(v, reflect.ValueOf("ghi"), reflect.ValueOf("jkl"))

fmt.Println("Our value is a type of :", v.Kind())
fmt.Printf("len : %v, %v\n", v.Len(), tool.Typfmtv(&v))
fmt.Printf("len : %v, %v\n", v.Len(), ref.Typfmtv(&v))

vSlice := v.Slice(0, v.Len())
vSliceElems := vSlice.Interface()
Expand Down Expand Up @@ -140,25 +140,25 @@ func TestValueValid(t *testing.T) {

var v reflect.Value

t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", ref.Valfmt(&v), ref.Typfmtv(&v), v.IsValid(), ref.IsNil(v), ref.IsZero(v))

v = reflect.ValueOf(ival)
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", ref.Valfmt(&v), ref.Typfmtv(&v), v.IsValid(), ref.IsNil(v), ref.IsZero(v))

v = reflect.ValueOf(pival)
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", ref.Valfmt(&v), ref.Typfmtv(&v), v.IsValid(), ref.IsNil(v), ref.IsZero(v))

v = reflect.ValueOf(aval)
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", ref.Valfmt(&v), ref.Typfmtv(&v), v.IsValid(), ref.IsNil(v), ref.IsZero(v))

v = reflect.ValueOf(paval)
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", ref.Valfmt(&v), ref.Typfmtv(&v), v.IsValid(), ref.IsNil(v), ref.IsZero(v))

var b bool
v = reflect.ValueOf(b)
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", ref.Valfmt(&v), ref.Typfmtv(&v), v.IsValid(), ref.IsNil(v), ref.IsZero(v))

b = true
v = reflect.ValueOf(b)
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", tool.Valfmt(&v), tool.Typfmtv(&v), v.IsValid(), tool.IsNil(v), tool.IsZero(v))
t.Logf("ival: %v (%v), isvalid/isnil/iszero: %v/%v/%v", ref.Valfmt(&v), ref.Typfmtv(&v), v.IsValid(), ref.IsNil(v), ref.IsZero(v))
}
18 changes: 10 additions & 8 deletions 03_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"reflect"
"testing"

"gopkg.in/hedzr/errors.v3"
"github.com/hedzr/evendeep/ref"

"github.com/hedzr/evendeep/flags"
"github.com/hedzr/evendeep/flags/cms"
"github.com/hedzr/evendeep/internal/tool"

"gopkg.in/hedzr/errors.v3"
)

func TestRegisterInitRoutines(t *testing.T) {
Expand Down Expand Up @@ -72,7 +74,7 @@ func TestParamsBasics(t *testing.T) {
a, expects := prepareAFT()

v := reflect.ValueOf(&a)
v = tool.Rindirect(v)
v = ref.Rindirect(v)

for i := 0; i < v.NumField(); i++ {
fld := v.Type().Field(i)
Expand Down Expand Up @@ -104,7 +106,7 @@ func TestParamsBasics3(t *testing.T) {
}
var a AFS1
v := reflect.ValueOf(&a)
v = tool.Rindirect(v)
v = ref.Rindirect(v)
sf, _ := v.Type().FieldByName("wouldbe")
// sf0, _ := v.Type().FieldByName("flags")
// sf1, _ := v.Type().FieldByName("converter")
Expand Down Expand Up @@ -210,7 +212,7 @@ func TestDeferCatchers(t *testing.T) {
tgt1 := &BBB{X1: "no", X2: "longer", Y: "-1"}

src, dst := reflect.ValueOf(&src1), reflect.ValueOf(&tgt1)
svv, dvv := tool.Rdecodesimple(src), tool.Rdecodesimple(dst)
svv, dvv := ref.Rdecodesimple(src), ref.Rdecodesimple(dst)
sf1, df1 := svv.Field(1), dvv.Field(1)

c := newCopier()
Expand Down Expand Up @@ -241,7 +243,7 @@ func TestDeferCatchers(t *testing.T) {
tgt1 := &BBB{X1: "no", X2: "longer", Y: "-1"}

src, dst := reflect.ValueOf(&src1), reflect.ValueOf(&tgt1)
svv, dvv := tool.Rdecodesimple(src), tool.Rdecodesimple(dst)
svv, dvv := ref.Rdecodesimple(src), ref.Rdecodesimple(dst)
// sf1, df1 := svv.Field(1), dvv.Field(1)

c := newCopier()
Expand Down Expand Up @@ -276,7 +278,7 @@ func TestDeferCatchers(t *testing.T) {
tgt1 := &BBB{X1: "no", X2: "longer", Y: "-1"}

src, dst := reflect.ValueOf(&src1), reflect.ValueOf(&tgt1)
svv, dvv := tool.Rdecodesimple(src), tool.Rdecodesimple(dst)
svv, dvv := ref.Rdecodesimple(src), ref.Rdecodesimple(dst)
// sf1, df1 := svv.Field(1), dvv.Field(1)
postCatcher(func() {
_ = c.copyToInternal(nil, svv, dvv, func(c *cpController, params *Params, from, to reflect.Value) (err error) {
Expand All @@ -295,7 +297,7 @@ func TestDeferCatchers(t *testing.T) {
tgt1 := &BBB{X1: "no", X2: "longer", Y: "-1"}

src, dst := reflect.ValueOf(&src1), reflect.ValueOf(&tgt1)
svv, dvv := tool.Rdecodesimple(src), tool.Rdecodesimple(dst)
svv, dvv := ref.Rdecodesimple(src), ref.Rdecodesimple(dst)
// sf1, df1 := svv.Field(1), dvv.Field(1)

t.Logf("src: %v, %v", src.IsValid(), svv.IsValid())
Expand Down Expand Up @@ -340,7 +342,7 @@ func TestDeferCatchers(t *testing.T) {
tgt1 := &BBB{X1: "no", X2: "longer", Y: "-1"}

src, dst := reflect.ValueOf(&src1), reflect.ValueOf(&tgt1)
svv, dvv := tool.Rindirect(src), tool.Rindirect(dst)
svv, dvv := ref.Rindirect(src), ref.Rindirect(dst)
// sf1, df1 := svv.Field(1), dvv.Field(1)

root := newParams(withOwners(c, nil, &svv, &dvv, &src, &dst))
Expand Down
39 changes: 22 additions & 17 deletions ctrl.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package evendeep

import (
"github.com/hedzr/log"
"reflect"
"unsafe"

"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/flags"
"github.com/hedzr/evendeep/flags/cms"
"github.com/hedzr/evendeep/internal/tool"
"github.com/hedzr/evendeep/ref"
"github.com/hedzr/evendeep/typ"
logz "github.com/hedzr/logg/slog"

"gopkg.in/hedzr/errors.v3"

"reflect"
"unsafe"
)

type cpController struct {
Expand Down Expand Up @@ -84,15 +83,15 @@ func (c *cpController) CopyTo(fromObjOrPtr, toObjPtr interface{}, opts ...Opt) (
var (
from0 = reflect.ValueOf(fromObjOrPtr)
to0 = reflect.ValueOf(toObjPtr)
from = tool.Rindirect(from0)
to = tool.Rindirect(to0)
from = ref.Rindirect(from0)
to = ref.Rindirect(to0)
root = newParams(withOwners(c, nil, &from0, &to0, &from, &to))
)

dbglog.Log(" flags: %v", c.flags)
dbglog.Log("flags (verbose): %+v", c.flags)
dbglog.Log(" from.type: %v | input: %v", tool.Typfmtv(&from), tool.Typfmtv(&from0))
dbglog.Log(" to.type: %v | input: %v", tool.Typfmtv(&to), tool.Typfmtv(&to0))
dbglog.Log(" from.type: %v | input: %v", ref.Typfmtv(&from), ref.Typfmtv(&from0))
dbglog.Log(" to.type: %v | input: %v", ref.Typfmtv(&to), ref.Typfmtv(&to0))

err = c.copyTo(root, from, to)
return
Expand All @@ -102,7 +101,7 @@ func (c *cpController) copyTo(params *Params, from, to reflect.Value) (err error
err = c.copyToInternal(params, from, to,
func(c *cpController, params *Params, from, to reflect.Value) (err error) {
kind, pkgPath := from.Kind(), from.Type().PkgPath()
if c.sourceExtractor != nil && to.IsValid() && !tool.IsNil(to) {
if c.sourceExtractor != nil && to.IsValid() && !ref.IsNil(to) {
// use tool.IsNil because we are checking for:
// 1. to,IsNil() if 'to' is an addressable value (such as slice, map, or ptr)
// 2. false if 'to' is not an addressable value (such as struct, int, ...)
Expand All @@ -118,7 +117,7 @@ func (c *cpController) copyTo(params *Params, from, to reflect.Value) (err error
}

// source is primitive type, or in a reserved package such as time, os, ...
dbglog.Log(" - from.type: %v - fallback to copyDefaultHandler | to.type: %v", kind, tool.Typfmtv(&to))
dbglog.Log(" - from.type: %v - fallback to copyDefaultHandler | to.type: %v", kind, ref.Typfmtv(&to))
err = copyDefaultHandler(c, params, from, to)
return
})
Expand Down Expand Up @@ -148,7 +147,7 @@ func (c *cpController) copyToInternal( //nolint:gocognit //yes, it is an integra
}

//nolint:lll,nestif //keep it
if from.CanAddr() && to.CanAddr() && tool.KindIs(from.Kind(), reflect.Array, reflect.Map, reflect.Slice, reflect.Struct) {
if from.CanAddr() && to.CanAddr() && ref.KindIs(from.Kind(), reflect.Array, reflect.Map, reflect.Slice, reflect.Struct) {
addr1 := unsafe.Pointer(from.UnsafeAddr())
addr2 := unsafe.Pointer(to.UnsafeAddr())
if uintptr(addr1) > uintptr(addr2) {
Expand Down Expand Up @@ -177,20 +176,26 @@ func (c *cpController) copyToInternal( //nolint:gocognit //yes, it is an integra
defer func() {
if e := recover(); e != nil {
err = errors.New("[recovered] copyTo unsatisfied ([%v] -> [%v])",
tool.RindirectType(from.Type()), tool.RindirectType(to.Type())).
ref.RindirectType(from.Type()), ref.RindirectType(to.Type())).
WithMaxObjectStringLength(maxObjectStringLen).
WithData(e).
WithTaggedData(errors.TaggedData{
"source": from,
"target": to,
})

// skip go-lib frames and defer-recover frame, back to the point throwing panic
n := log.CalcStackFrames(1) // skip defer-recover frame at first
// // skip go-lib frames and defer-recover frame, back to the point throwing panic
// n := logz.CalcStackFrames(1) // skip defer-recover frame at first
// if c.rethrow {
// logz.Skip(n).Panicf("%+v", err)
// } else {
// logz.Skip(n).Errorf("%+v", err)
// }

if c.rethrow {
log.Skip(n).Panicf("%+v", err)
logz.Panic("[recovered] copyTo unsatisfied", "error", err)
} else {
log.Skip(n).Errorf("%+v", err)
logz.Error("[recovered] copyTo unsatisfied", "error", err)
}
}
}()
Expand Down
11 changes: 6 additions & 5 deletions ctrl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
"time"
"unsafe"

"gopkg.in/hedzr/errors.v3"
"github.com/hedzr/evendeep/ref"

"github.com/hedzr/evendeep"
"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/diff"
"github.com/hedzr/evendeep/flags/cms"
"github.com/hedzr/evendeep/internal/tool"
"github.com/hedzr/evendeep/typ"

"gopkg.in/hedzr/errors.v3"
)

const (
Expand Down Expand Up @@ -981,10 +982,10 @@ func TestStructWithTargetSetter_map2struct(t *testing.T) {
var f = s.FieldByName(fldName)
if f.IsValid() {
if value.Type().ConvertibleTo(f.Type()) {
dbglog.Log("struct.%q <- %v", fldName, tool.Valfmt(&value))
dbglog.Log("struct.%q <- %v", fldName, ref.Valfmt(&value))
f.Set(value.Convert(f.Type()))
} else {
dbglog.Log("struct.%q <- %v", fldName, tool.Valfmt(&value))
dbglog.Log("struct.%q <- %v", fldName, ref.Valfmt(&value))
f.Set(value)
}
}
Expand All @@ -994,7 +995,7 @@ func TestStructWithTargetSetter_map2struct(t *testing.T) {
if value != nil {
name := "Mo" + strings.Join(sourceNames, ".")
setStructByName(reflect.ValueOf(tgt).Elem(), name, *value)
dbglog.Log("struct.%q <- %v", name, tool.Valfmt(value))
dbglog.Log("struct.%q <- %v", name, ref.Valfmt(value))
}
return // ErrShouldFallback to call the evendeep standard processing
}),
Expand Down
Loading

0 comments on commit 182c505

Please sign in to comment.