feat: 优化参数构建逻辑,简化模型接口,移除不必要的字段导入

This commit is contained in:
Rogee
2025-09-22 13:22:03 +08:00
parent 824861c27c
commit 07e70a80ea

View File

@@ -99,17 +99,12 @@ func (b *renderDataBuilder) buildParameters(params []ParamDefinition) []string {
if token == "" { if token == "" {
return "", false return "", false
} }
if item.Model != "" {
b.needsFieldImport = true
}
return token, true return token, true
}) })
} }
func (b *renderDataBuilder) addRequiredImports() { func (b *renderDataBuilder) addRequiredImports() {
if b.needsFieldImport { // New Model interface doesn't require field import
b.imports = append(b.imports, `field "go.ipao.vip/gen/field"`)
}
} }
func (b *renderDataBuilder) dedupeAndSortImports() { func (b *renderDataBuilder) dedupeAndSortImports() {
@@ -219,13 +214,30 @@ func (b *paramTokenBuilder) buildPathParam() string {
} }
func (b *paramTokenBuilder) buildModelLookupPath() string { func (b *paramTokenBuilder) buildModelLookupPath() string {
field, fieldType := b.parseModelField() field, _ := b.parseModelField()
tpl := `func(ctx fiber.Ctx) (*%s, error) { // Use the simplified Model interface without closures
v := fiber.Params[%s](ctx, "%s") // This provides consistency with other parameter binding methods
return %sQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("%s = ?", v)).First() if field == "id" && b.key == "id" {
}` // Use the simplest form for default id field
return fmt.Sprintf(tpl, b.item.Type, fieldType, b.key, b.item.Type, field) return fmt.Sprintf(`ModelById[%s]("%s")`, b.item.Type, b.key)
} else if field == b.key {
// Field and path key are the same
return fmt.Sprintf(`Model[%s]("%s")`, b.item.Type, field)
} else {
// Different field and path key
return fmt.Sprintf(`Model[%s]("%s", "%s")`, b.item.Type, field, b.key)
}
}
// getModelName extracts the model name from the type, preserving package path
func (b *paramTokenBuilder) getModelName() string {
// Remove the pointer star if present
typeName := strings.TrimPrefix(b.item.Type, "*")
// Keep the full package path for the query object
// e.g., "models.User" becomes "models.UserQuery"
return typeName
} }
func (b *paramTokenBuilder) parseModelField() (string, string) { func (b *paramTokenBuilder) parseModelField() (string, string) {