bytedance / go-tagexpr

An interesting go struct tag expression syntax for field validation, etc.
Apache License 2.0
1.66k stars 142 forks source link

findVdTag() recursion optmized #40

Closed remrain closed 2 years ago

remrain commented 2 years ago

findVdTag() hangs with nested struct

Reproduction:

package main

import (
        "bytes"
        "fmt"
        "net/http"
        "time"

        binding "github.com/bytedance/go-tagexpr/v2/binding"
)

type Node struct {
        N1 *Node
        N2 *Node
        N3 *Node
}

func main() {
        node := &Node{}
        now := time.Now()
        req, _ := http.NewRequest("get", "http://localhost/", bytes.NewReader([]byte{}))

        fmt.Println("started at: ", now)

        binding.Default().BindAndValidate(node, req, &S{})

        fmt.Println("end at: ", time.Now())
        fmt.Println("cost: ", time.Since(now))
}

type S struct{}

func (*S) Get(name string) (string, bool) {
        return "", false
}
$ go run main.go 
started at:  2022-01-26 12:04:02.636644 +0800 CST m=+0.006399274
end at:  2022-01-26 12:08:01.204883 +0800 CST m=+238.584279719
cost:  3m58.577927221s