feat: 添加对 @Bind 注释中 model 字段的支持,优化路径参数绑定逻辑并更新文档

This commit is contained in:
Rogee
2025-09-11 20:10:24 +08:00
parent b93c7726f2
commit 76c7592f26
4 changed files with 178 additions and 50 deletions

View File

@@ -85,31 +85,41 @@ func buildRenderData(opts RenderBuildOpts) (RenderData, error) {
}
func buildParamToken(item ParamDefinition) string {
key := item.Name
if item.Key != "" {
key = item.Key
}
key := item.Name
if item.Key != "" {
key = item.Key
}
switch item.Position {
case PositionQuery:
return fmt.Sprintf(`Query%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key)
case PositionHeader:
return fmt.Sprintf(`Header[%s]("%s")`, item.Type, key)
case PositionFile:
return fmt.Sprintf(`File[multipart.FileHeader]("%s")`, key)
case PositionCookie:
if item.Type == "string" {
return fmt.Sprintf(`CookieParam("%s")`, key)
}
return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key)
case PositionBody:
return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key)
case PositionPath:
return fmt.Sprintf(`Path%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key)
case PositionLocal:
return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key)
}
return ""
switch item.Position {
case PositionQuery:
return fmt.Sprintf(`Query%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key)
case PositionHeader:
return fmt.Sprintf(`Header[%s]("%s")`, item.Type, key)
case PositionFile:
return fmt.Sprintf(`File[multipart.FileHeader]("%s")`, key)
case PositionCookie:
if item.Type == "string" {
return fmt.Sprintf(`CookieParam("%s")`, key)
}
return fmt.Sprintf(`Cookie[%s]("%s")`, item.Type, key)
case PositionBody:
return fmt.Sprintf(`Body[%s]("%s")`, item.Type, key)
case PositionPath:
// If a model field is specified, generate a model-lookup binder from path value.
if item.ModelField != "" || item.Model != "" {
field := item.ModelField
if field == "" {
field = "id"
}
// PathModel is expected to resolve the path param to the specified model by field.
// Example: PathModel[models.User]("id", "user_id")
return fmt.Sprintf(`PathModel[%s]("%s", "%s")`, item.Type, field, key)
}
return fmt.Sprintf(`Path%s[%s]("%s")`, scalarSuffix(item.Type), item.Type, key)
case PositionLocal:
return fmt.Sprintf(`Local[%s]("%s")`, item.Type, key)
}
return ""
}
func scalarSuffix(t string) string {