Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fumeboy/nni
通过声明嵌套结构体的方式生成URL
https://github.com/fumeboy/nni
Last synced: 2 months ago
JSON representation
通过声明嵌套结构体的方式生成URL
- Host: GitHub
- URL: https://github.com/fumeboy/nni
- Owner: fumeboy
- Created: 2021-03-27T15:10:00.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-28T03:06:57.000Z (almost 4 years ago)
- Last Synced: 2024-08-03T09:07:06.890Z (6 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
- awesome-github-star - nni
README
# 通过嵌套结构体的方式生成URL
如下方式定义结构体
```go
/*
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:
```txt
{/apple/:kind/get/:number/ get}
```## 使用场景?
可以是这样
```go
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 上
```go
// @router /***/**/handle1 [POST]
func Handle1(c *gin.Context) {
```而且这样从下向上的书写 URL,也很容易写错 parent path `/***/**/`