From bceb45ad8c839892383e77ee53cb55d13c26aca1 Mon Sep 17 00:00:00 2001 From: Nicola Zangrandi Date: Fri, 21 Feb 2025 08:49:22 +0100 Subject: [PATCH] feat(rules): add HTTP and GORM standards to Go rules --- .cursor/rules/go.mdc | 54 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/.cursor/rules/go.mdc b/.cursor/rules/go.mdc index efdbbad..b700fb8 100644 --- a/.cursor/rules/go.mdc +++ b/.cursor/rules/go.mdc @@ -26,6 +26,37 @@ When working with Go: - Document version constraints - Run `go mod verify` before commits +5. HTTP Framework Standards: + - Use Gin for HTTP routing and middleware + - Group related routes under common prefixes + - Use consistent error response format: + ```go + c.JSON(status, gin.H{"error": err.Error()}) + ``` + - Leverage Gin's built-in features (binding, validation, etc.) + - Keep handlers focused and small + +6. GORM Standards: + - Use struct tags for model definitions: + ```go + type Model struct { + ID string `gorm:"primaryKey" json:"id"` + // Add json tags for all fields that need serialization + } + ``` + - Always handle GORM errors explicitly + - Use proper GORM hooks for timestamps (CreatedAt, UpdatedAt) + - Prefer query building over raw SQL + - Use transactions for multi-step operations + - Follow GORM naming conventions: + - Model names: singular, CamelCase + - Table names: plural, snake_case (auto-converted by GORM) + - Use appropriate GORM tags: + - `gorm:"primaryKey"` for primary keys + - `gorm:"not null"` for required fields + - `gorm:"index"` for indexed fields + - `gorm:"type:text"` for specific SQL types + metadata: priority: high version: 1.0 @@ -46,4 +77,25 @@ examples: # Good: Add dependency and tidy go get some/package go mod tidy - go mod verify \ No newline at end of file + go mod verify + + - input: | + # Bad: Flat route structure + r.GET("/notes", handleNotes) + r.GET("/notes/:id", handleNote) + output: | + # Good: Grouped routes + notes := r.Group("/notes") + { + notes.GET("", handleGetNotes) + notes.GET("/:id", handleGetNote) + } + + - input: | + # Bad: Raw SQL and error handling + db.Exec("UPDATE notes SET content = ? WHERE id = ?", content, id) + output: | + # Good: GORM query building and error handling + if err := db.Model(&Note{}).Where("id = ?", id).Update("content", content).Error; err != nil { + return fmt.Errorf("failed to update note: %w", err) + } \ No newline at end of file