gofasta g method
Adds a method to an existing service — patches both the interface at app/services/interfaces/<snake>_service.go (adding the signature) and the impl at app/services/<snake>.service.go (adding a stub body). Idempotent: re-running with the same method name returns METHOD_ALREADY_EXISTS rather than silently duplicating.
Uses dst for AST patching, so doc comments, blank lines, and import grouping are preserved through the parse → modify → print round-trip.
Usage
gofasta g method <Resource> <Method> [param:type ...] [flags]The resource must already exist (see gofasta g scaffold). Additional positional args are parameters in name:type form — the same shorthand g scaffold uses for fields.
Flags
| Flag | Default | Description |
|---|---|---|
--dry-run | false | Preview the patches without writing. Honors --json for a structured {patch: [...]} plan. |
Examples
Add an Archive method with no parameters:
gofasta g method Order ArchiveResult — app/services/interfaces/order_service.go gains:
type OrderServiceInterface interface {
// ... existing methods preserved ...
Archive(ctx context.Context) error
}And app/services/order.service.go gains:
// Archive is a generated stub. Replace with the real implementation.
func (s *orderService) Archive(ctx context.Context) error {
return fmt.Errorf("OrderServiceInterface.Archive: not implemented")
}Add a method with parameters:
gofasta g method Order Cancel reason:string force:bool
# → Cancel(ctx context.Context, reason string, force bool) errorPreview without writing:
gofasta g method Order Archive --dry-runPlan output (JSON):
gofasta g method Order Archive --dry-run --json{
"actions": [
{"kind": "patch", "path": "app/services/interfaces/order_service.go", "detail": "add Archive to OrderServiceInterface"},
{"kind": "patch", "path": "app/services/order.service.go", "detail": "add Archive impl stub to orderService"}
]
}What it patches
| File | Change |
|---|---|
app/services/interfaces/<snake>_service.go | Appends the method signature to <Resource>ServiceInterface. Adds context import if not present. |
app/services/<snake>.service.go | Appends a stub method to the impl struct. The stub returns a fmt.Errorf("...: not implemented") so the build stays green and existing tests don’t break. |
Naming conventions (overridable via MethodData fields if invoking the API directly):
- Interface name:
<Resource>ServiceInterface(matches scaffold) - Impl struct:
<lowerResource>Service(matches scaffold) - Receiver name:
s
Argument convention
Every generated method takes ctx context.Context as the first parameter and returns error. User-supplied args follow ctx:
gofasta g method Order Cancel reason:string force:bool
# → Cancel(ctx context.Context, reason string, force bool) errorThis matches the gofasta scaffold convention so all methods on a service have the same shape — predictable for callers and consistent with the auth/timeout middleware that hangs off the context.
Error codes
| Code | When |
|---|---|
RESOURCE_NOT_FOUND | The interface or impl file is missing. Run gofasta g scaffold <Resource> first. |
METHOD_ALREADY_EXISTS | The interface already declares a method by that name. Idempotency hit. |
AST_PARSE_FAILED | One of the target files has a syntax error. Fix the file and re-run. |
AST_PATCH_FAILED | dst couldn’t render the modified AST (rare — usually a malformed parameter type that doesn’t parse). |