fagongzi / manba

HTTP API Gateway
Apache License 2.0
3.31k stars 759 forks source link

route.go Find函数匹配问题 #245

Open zouyih opened 2 years ago

zouyih commented 2 years ago
func TestFindMatchAmbiguity(t *testing.T) {
    r := NewRoute()
    r.Add(&metapb.API{
        ID:         1,
        URLPattern: "/a/b/c",
        Method:     "*",
    })
    r.Add(&metapb.API{
        ID:         2,
        URLPattern: "/(string):x/b/e",
        Method:     "*",
    })

    params := make(map[string]string, 0)
    paramsFunc := func(name, value []byte) {
        params[string(name)] = string(value)
    }

    id, _ := r.Find([]byte("/a/b/e"), "GET", paramsFunc)
    fmt.Println(id, params)
}

输出: 0 map[]

预期是要匹配到api_id =2, 实际上匹配不了

zouyih commented 2 years ago
func TestFindMatchAmbiguity(t *testing.T) {
    r := NewRoute()

    r.Add(&metapb.API{
        ID:         2,
        URLPattern: "/(string):x/b/e",
        Method:     "*",
    })

    params := make(map[string]string, 0)
    paramsFunc := func(name, value []byte) {
        params[string(name)] = string(value)
    }

    id, _ := r.Find([]byte("/a/b/e"), "GET", paramsFunc)
    fmt.Println(id, params)
}

输出: 2 map[x:a]

如果去掉api_id=1,才能匹配到结果

zouyih commented 2 years ago

route的匹配算法只选了一个路径进行匹配,没有做错误回退处理,用深搜应该可以解决这个问题