Automatically generate database CRUD operations, HTTP clients, and net/rpc wrappers from Go interface definitions. Eliminates boilerplate code in Go projects.
A Go code generation tool that automatically generates CRUD operations for databases, HTTP client code, and net/rpc client/server wrappers based on predefined schemas. Eliminates repetitive boilerplate code by defining interfaces and automatically generating implementation code.
This skill helps you work with the `defc` code generation tool in Go projects. It provides guidance on:
When working with defc in a Go project, follow these steps:
First, examine the repository structure:
For generating code:
**Automatic mode detection:**
```bash
defc generate schema.go
```
**Explicit mode selection:**
```bash
defc --mode=sqlx --output=query.go schema.go
defc --mode=api --output=service.go schema.go
defc --mode=rpc --output=client.go schema.go
```
**Understanding mode detection:**
Run tests in this order:
```bash
./test.sh
go test -cover ./gen
go test -cover ./runtime
go test -cover ./sqlx
go test -tags=test ./gen/integration
go test -run TestRunCommand ./gen
go test -run TestRpc -tags=test ./gen/integration
```
**Code validation:**
```bash
go vet ./...
go vet ./gen
```
```bash
go build
go install
go run -mod=mod github.com/x5iu/defc@latest --help
```
When creating or modifying interface schemas:
**For sqlx mode (database operations):**
**For api mode (HTTP clients):**
**For rpc mode (net/rpc wrappers):**
The generation flow:
1. Parse Go interface definitions using `go/ast` and `go/parser`
2. Extract method signatures and comments with SQL/HTTP specifications
3. Auto-detect generation mode via `DetectTargetDecl` (gen/tools.go:360-407)
4. Build context with `CliBuilder` pattern (imports, features, functions, templates)
5. Apply Go templates (`sqlx.tmpl`, `api.tmpl`, `rpc.tmpl`) to generate code
6. Format with `go/format` and manage imports with `goimports`
7. Write to output file
When modifying generation templates in `gen/template/`:
Generated code follows naming conventions:
The tool supports modular features:
Enable features based on project requirements using the builder pattern.
**When adding new generation modes:**
1. Create a template in `gen/template/`
2. Add detection logic in `DetectTargetDecl` (gen/tools.go:360-407)
3. Implement builder methods in `gen/builder.go`
4. Add integration tests in `gen/integration/`
**When debugging generation:**
1. Check AST parsing in relevant gen/ files
2. Verify template syntax in `gen/template/*.tmpl`
3. Run with `--disable-auto-import` to isolate import issues
4. Use `go vet` for validation instead of `go build`
**When writing tests:**
**Generate database CRUD from interface:**
```bash
defc --mode=sqlx --output=db_generated.go user_schema.go
```
**Generate HTTP client with auto-detection:**
```bash
defc generate api_schema.go
```
**Generate RPC wrappers without runtime:**
```bash
defc --mode=rpc --features=rpc/nort --output=rpc_client.go service.go
```
**Run full test suite:**
```bash
./test.sh
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/defc-go-code-generator/raw