From d3c208559a722c41bb5eef442c124e4f857b5768 Mon Sep 17 00:00:00 2001 From: seekwe Date: Mon, 28 Aug 2023 20:24:43 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20chore:=20Refactor=20code=20and?= =?UTF-8?q?=20improve=20test=20coverage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- znet/bind_router.go | 7 ++----- znet/bind_router_test.go | 1 - znet/web_test.go | 10 ---------- zreflect/quick.go | 2 +- zreflect/quick_test.go | 8 +++++++- zvalid/value.go | 2 -- 6 files changed, 10 insertions(+), 20 deletions(-) diff --git a/znet/bind_router.go b/znet/bind_router.go index a046421..b542f1c 100644 --- a/znet/bind_router.go +++ b/znet/bind_router.go @@ -38,10 +38,7 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err } } - handleName := "reflect.methodValueCall" - typeOf := zreflect.TypeOf(of) - - // methods := strings.Split("ANY|GET|Post|POST|PUT|DELETE|PATCH|HEAD|OPTIONS", "|") + typeOf := reflect.Indirect(of).Type() return zutil.TryCatch(func() error { return zreflect.ForEachMethod(of, func(i int, m reflect.Method, value reflect.Value) error { if m.Name == "Init" { @@ -71,7 +68,7 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err } fn := value.Interface() - handleName = strings.Join([]string{typeOf.PkgPath(), typeOf.Name(), m.Name}, ".") + handleName := strings.Join([]string{typeOf.PkgPath(), typeOf.Name(), m.Name}, ".") if e.BindStructCase != nil { path = e.BindStructCase(path) } else if e.BindStructDelimiter != "" { diff --git a/znet/bind_router_test.go b/znet/bind_router_test.go index b8c969e..9917944 100644 --- a/znet/bind_router_test.go +++ b/znet/bind_router_test.go @@ -165,5 +165,4 @@ func TestBindStructCase(t *testing.T) { t.Log("Test:", v[0], v[1]) t.Log(w.Code, w.Body.String()) } - r.BindStructCase = nil } diff --git a/znet/web_test.go b/znet/web_test.go index 430e59f..076c65c 100644 --- a/znet/web_test.go +++ b/znet/web_test.go @@ -643,16 +643,6 @@ func TestTemplateNew(t *testing.T) { }) tt.Equal(200, w.Code) tt.EqualExit(`ZlsGo-ok`, w.Body.String()) - return - temple, _ := template.New("tmpTemplate/tpl-html.html").Parse(`{{.title}}`) - r.SetHTMLTemplate(temple) - - w = newRequest(r, "GET", "/Template-define-2", "/Template-define-2", - func(c *Context) { - c.Template(200, "tmpTemplate/tpl-html.html", Data{"title": "ZlsGo"}) - }) - tt.Equal(200, w.Code) - tt.EqualExit(`ZlsGo`, w.Body.String()) } func TestBind(t *testing.T) { diff --git a/zreflect/quick.go b/zreflect/quick.go index c20b534..db0a010 100644 --- a/zreflect/quick.go +++ b/zreflect/quick.go @@ -8,7 +8,7 @@ import ( func ForEachMethod(valof reflect.Value, fn func(index int, method reflect.Method, value reflect.Value) error) error { numMethod := valof.NumMethod() if numMethod == 0 { - return errors.New("method cannot be obtained") + return nil } tp := toRType(NewType(valof)) diff --git a/zreflect/quick_test.go b/zreflect/quick_test.go index 6fbd44d..407ab36 100644 --- a/zreflect/quick_test.go +++ b/zreflect/quick_test.go @@ -10,12 +10,18 @@ import ( func TestForEachMethod(t *testing.T) { tt := zlsgo.NewTest(t) v := ValueOf(Demo) - err := ForEachMethod(v, func(index int, method reflect.Method, value reflect.Value) error { tt.Log(index, method.Name, value.Kind()) return nil }) tt.NoError(err) + + v = ValueOf(&Demo) + err = ForEachMethod(v, func(index int, method reflect.Method, value reflect.Value) error { + tt.Log(index, method.Name, value.Kind()) + return nil + }) + tt.NoError(err) } func TestForEach(t *testing.T) { diff --git a/zvalid/value.go b/zvalid/value.go index 0adc8fd..e123850 100644 --- a/zvalid/value.go +++ b/zvalid/value.go @@ -143,8 +143,6 @@ func (v Engine) VerifiAny(value interface{}, name ...string) Engine { switch vv := value.(type) { case string: s = vv - // case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool: - // s = ztype.ToString(vv) default: s = ztype.ToString(vv) // v.err = setError(&v, "unsupported type")