feat(rules): add HTTP and GORM standards to Go rules

This commit is contained in:
Nicola Zangrandi 2025-02-21 08:49:22 +01:00
parent c030d3e43a
commit bceb45ad8c
Signed by: wasp
GPG key ID: 43C1470D890F23ED

View file

@ -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
@ -47,3 +78,24 @@ examples:
go get some/package
go mod tidy
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)
}