From 07e70a80ea13cf9ba07c44c957a72fafb90b4334 Mon Sep 17 00:00:00 2001 From: Rogee Date: Mon, 22 Sep 2025 13:22:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E9=80=BB=E8=BE=91=EF=BC=8C=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=8E=A5=E5=8F=A3=EF=BC=8C=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E5=AD=97=E6=AE=B5=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/ast/route/builder.go | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/pkg/ast/route/builder.go b/pkg/ast/route/builder.go index d65167c..1241ed2 100644 --- a/pkg/ast/route/builder.go +++ b/pkg/ast/route/builder.go @@ -99,17 +99,12 @@ func (b *renderDataBuilder) buildParameters(params []ParamDefinition) []string { if token == "" { return "", false } - if item.Model != "" { - b.needsFieldImport = true - } return token, true }) } func (b *renderDataBuilder) addRequiredImports() { - if b.needsFieldImport { - b.imports = append(b.imports, `field "go.ipao.vip/gen/field"`) - } + // New Model interface doesn't require field import } func (b *renderDataBuilder) dedupeAndSortImports() { @@ -219,13 +214,30 @@ func (b *paramTokenBuilder) buildPathParam() string { } func (b *paramTokenBuilder) buildModelLookupPath() string { - field, fieldType := b.parseModelField() + field, _ := b.parseModelField() - tpl := `func(ctx fiber.Ctx) (*%s, error) { - v := fiber.Params[%s](ctx, "%s") - return %sQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("%s = ?", v)).First() - }` - return fmt.Sprintf(tpl, b.item.Type, fieldType, b.key, b.item.Type, field) + // Use the simplified Model interface without closures + // This provides consistency with other parameter binding methods + if field == "id" && b.key == "id" { + // Use the simplest form for default id 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) {