Skip to content

Commit

Permalink
🚀 chore: Refactor code and improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Aug 28, 2023
1 parent f696daa commit d3c2085
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 20 deletions.
7 changes: 2 additions & 5 deletions znet/bind_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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 != "" {
Expand Down
1 change: 0 additions & 1 deletion znet/bind_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 0 additions & 10 deletions znet/web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion zreflect/quick.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 7 additions & 1 deletion zreflect/quick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions zvalid/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit d3c2085

Please sign in to comment.