Skip to content
/ nni Public

通过声明嵌套结构体的方式生成URL

Notifications You must be signed in to change notification settings

fumeboy/nni

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

通过嵌套结构体的方式生成URL

如下方式定义结构体

/*
type Route struct { // the path is "/"
	// 空结构体
}
*/

type RouteApple struct { // the path is "/apple/:kind"
	nni.Route `url:"apple"`
	kind string // 这里的 kind 关联 path param,可以用依赖注入的方式设置值
}

type RouteAppleGet struct { // the path is "/apple/:kind/get/:number"
	RouteApple `url:"get"`
	number string
	nni.GET // 这个 GET 指 http request method。是空结构体
}

func TestExample1(t *testing.T){
	fmt.Println(nni.Parse(&RouteAppleGet{}))
}

nni.Parse() output:

{/apple/:kind/get/:number/ get}

使用场景?

可以是这样

type RouteAppleGet struct { // the path is "/apple/:kind/get/:number"
	RouteApple `url:"get"`
	number string
	nni.GET
}

func (handler *RouteAppleGet) handle(c *gin.Context){

}

router.Registe(&RouteAppleGet{})

目的

你不用像下面这样通过注释的方式弱捆绑 URL 到 handler 上

// @router /***/**/handle1 [POST]
func Handle1(c *gin.Context) {

而且这样从下向上的书写 URL,也很容易写错 parent path /***/**/

About

通过声明嵌套结构体的方式生成URL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages