Page:
Development
No results
1
Development
Virgil edited this page 2026-02-21 17:14:22 +00:00
Development
Prerequisites
- Go 1.21+ (generics support required)
- Access to a Forgejo instance for integration testing (optional)
Building
go build ./...
Running Tests
# All tests
go test ./...
# Verbose output
go test -v ./...
# Single test
go test -v -run TestClient_Good_Get ./...
# With race detection
go test -race ./...
Test Naming Convention
Tests use a _Good, _Bad, _Ugly suffix pattern:
| Suffix | Purpose |
|---|---|
_Good |
Happy path — expected success |
_Bad |
Expected errors — invalid input, 404 |
_Ugly |
Edge cases — panics, empty data |
Regenerating Types
When the Forgejo Swagger spec is updated:
# Using go generate
go generate ./types/...
# Or directly
go run ./cmd/forgegen/ -spec testdata/swagger.v1.json -out types/
Then verify:
go build ./...
go test ./...
go vet ./...
Adding a New Service
-
Create
servicename.gowith the service struct:- If CRUD applies: embed
Resource[T, C, U]with the appropriate types. - If endpoints are heterogeneous: use a plain
client *Clientfield.
- If CRUD applies: embed
-
Add a constructor:
func newServiceNameService(c *Client) *ServiceNameService -
Add action methods for non-CRUD endpoints.
-
Create
servicename_test.gowith tests following the_Good/_Bad/_Uglyconvention. -
Wire the service in
forge.go:- Add the field to the
Forgestruct. - Add
f.ServiceName = newServiceNameService(c)inNewForge.
- Add the field to the
-
Run all tests:
go test ./...
Adding a New Action Method
Action methods are hand-written methods on service structs for endpoints that don't fit the CRUD pattern.
func (s *ServiceName) ActionName(ctx context.Context, params...) (returnType, error) {
path := fmt.Sprintf("/api/v1/path/%s/%s", param1, param2)
var out types.ReturnType
if err := s.client.Post(ctx, path, body, &out); err != nil {
return nil, err
}
return &out, nil
}
All methods must:
- Accept
context.Contextas the first parameter - Return
erroras the last return value - Use
fmt.Sprintffor path construction with parameters
Project Conventions
- UK English in comments (organisation, colour, licence)
context.Contextas the first parameter on all methods- Errors wrapped as
*APIErrorfor HTTP responses - Generated code has
// Code generatedheaders and must not be edited manually - Commit messages use conventional commits (
feat:,fix:,docs:)
Project Structure
go-forge/
*.go Core library (client, pagination, resource, services)
*_test.go Tests for each module
cmd/forgegen/ Code generator
types/ Generated types (do not edit)
testdata/ Swagger specification
docs/ Documentation
Forge Remote
git remote add forge ssh://git@forge.lthn.ai:2223/core/go-forge.git
git push -u forge main