Added CODE_CONVENTIONS.md and linters to enforce it, and applied it across the codebase
CODE_CONVENTIONS.md sets out the conventions for Go code in this repository: naming, layout, comments, errors, logging, configuration and tests. .golangci.json enables the linters that check the parts that can be checked (each limited to the rules the conventions use); the rest is applied by hand. Applied across the codebase: - Naming: full-word names in application code, initialisms in one case (BuyerID, PrintJSON, PrintYAML, ocdsGetBuyerID), no names shadowing built-ins (min/max are minGap/maxGap), mustRaw renamed rawJSON as it returns nil rather than panicking, and config.ConfigLevelToLogLevel renamed LevelToLogLevel. - Layout: lines kept to 80 characters where possible, assignments taken out of if and for statements that don't fit, wrapped arguments all on one line or one per line, steps separated by blank lines, no else after a return, function and struct literals wrapped. - Comments: doc comments on every exported name (including each section's Map, Flatten, TypeOf, ValueOf and SetValue), section banners, British spelling. - Errors: a created error starts with its package name, a wrapping error adds only what was being done, sentinels in one commented var block, errors.Is and errors.As in place of == and type assertions, and no err := shadowing of a named result. That fixes the mock server losing its access log's close error. - Logging: the fetcher passes its workers an untagged logger, so findatender's lines are tagged with their own component rather than with fetcher's as well; cmd/mock-fts logs "stopped" with slog. - Configuration: util.WithTimeout (moved from the websocket package) applies a timeout of 0 or less as no limit, used for the websocket write and ping, TCP close and tcpserver shutdown timeouts; every setting documents what 0 or empty means, including the rate limit rule's keys in internal/config/README.md. - Tests: related tests merged into table-driven ones with named cases. Separately from CODE_CONVENTIONS.md, internal/store/README.md gains a soft SQL style convention for the queries in internal/store/queries.go, and the queries are reformatted to follow it.
This commit is contained in:
1 parent
465358609a
commit
67215a6aaa
104 files changed
+3189
-1206
No files matched your search
@@ -30,26 +30,50 @@ type queryCheck struct {
|
||||
// missing here.
|
||||
var checks = map[string]queryCheck{
|
||||
// opportunities
|
||||
"selectOpportunityLatestReleaseDate": {sql: selectOpportunityLatestReleaseDate},
|
||||
"upsertOpportunity": {sql: upsertOpportunity, named: true},
|
||||
"selectOpportunityByUUID": {sql: selectOpportunityByUUID},
|
||||
"selectOpportunityLatestReleaseDate": {
|
||||
sql: selectOpportunityLatestReleaseDate,
|
||||
},
|
||||
"upsertOpportunity": {sql: upsertOpportunity, named: true},
|
||||
"selectOpportunityByUUID": {sql: selectOpportunityByUUID},
|
||||
"listOpportunitiesSelect": {
|
||||
sql: listOpportunitiesSelect + listOpportunitiesOrderAndPage,
|
||||
},
|
||||
"listOpportunitiesOrderAndPage": {
|
||||
sql: listOpportunitiesSelect + listOpportunitiesOrderAndPage,
|
||||
},
|
||||
"filterOpportunityStatus": {sql: withFilter(filterOpportunityStatus)},
|
||||
"filterOpportunityClassificationCode": {sql: withFilter(filterOpportunityClassificationCode)},
|
||||
"filterOpportunityTitleMatch": {sql: withFilter(filterOpportunityTitleMatch)},
|
||||
"filterOpportunityReleasedFrom": {sql: withFilter(filterOpportunityReleasedFrom)},
|
||||
"filterOpportunityReleasedBefore": {sql: withFilter(filterOpportunityReleasedBefore)},
|
||||
"filterOpportunityMarkHidden": {sql: withFilter(filterOpportunityMarkHidden)},
|
||||
"filterOpportunityMarkSkipped": {sql: withFilter(filterOpportunityMarkSkipped)},
|
||||
"filterOpportunityMarkInterested": {sql: withFilter(filterOpportunityMarkInterested)},
|
||||
"filterOpportunityMarkBidSubmitted": {sql: withFilter(filterOpportunityMarkBidSubmitted)},
|
||||
"filterOpportunityMarkWon": {sql: withFilter(filterOpportunityMarkWon)},
|
||||
"filterOpportunityMarkLost": {sql: withFilter(filterOpportunityMarkLost)},
|
||||
"filterOpportunityStatus": {
|
||||
sql: withFilter(filterOpportunityStatus),
|
||||
},
|
||||
"filterOpportunityClassificationCode": {
|
||||
sql: withFilter(filterOpportunityClassificationCode),
|
||||
},
|
||||
"filterOpportunityTitleMatch": {
|
||||
sql: withFilter(filterOpportunityTitleMatch),
|
||||
},
|
||||
"filterOpportunityReleasedFrom": {
|
||||
sql: withFilter(filterOpportunityReleasedFrom),
|
||||
},
|
||||
"filterOpportunityReleasedBefore": {
|
||||
sql: withFilter(filterOpportunityReleasedBefore),
|
||||
},
|
||||
"filterOpportunityMarkHidden": {
|
||||
sql: withFilter(filterOpportunityMarkHidden),
|
||||
},
|
||||
"filterOpportunityMarkSkipped": {
|
||||
sql: withFilter(filterOpportunityMarkSkipped),
|
||||
},
|
||||
"filterOpportunityMarkInterested": {
|
||||
sql: withFilter(filterOpportunityMarkInterested),
|
||||
},
|
||||
"filterOpportunityMarkBidSubmitted": {
|
||||
sql: withFilter(filterOpportunityMarkBidSubmitted),
|
||||
},
|
||||
"filterOpportunityMarkWon": {
|
||||
sql: withFilter(filterOpportunityMarkWon),
|
||||
},
|
||||
"filterOpportunityMarkLost": {
|
||||
sql: withFilter(filterOpportunityMarkLost),
|
||||
},
|
||||
|
||||
// opportunity_ocds_releases
|
||||
"insertRelease": {sql: insertRelease, named: true},
|
||||
@@ -61,12 +85,22 @@ var checks = map[string]queryCheck{
|
||||
"selectReleaseTagsByOpportunity": {sql: selectReleaseTagsByOpportunity},
|
||||
|
||||
// classification_codes
|
||||
"upsertClassificationCodeReturningID": {sql: upsertClassificationCodeReturningID, named: true},
|
||||
"upsertClassificationCodeReturningID": {
|
||||
sql: upsertClassificationCodeReturningID,
|
||||
named: true,
|
||||
},
|
||||
|
||||
// opportunity_classifications
|
||||
"deleteOpportunityClassifications": {sql: deleteOpportunityClassifications},
|
||||
"insertOpportunityClassification": {sql: insertOpportunityClassification},
|
||||
"selectClassificationsForOpportunities": {sql: selectClassificationsForOpportunities, in: true},
|
||||
"deleteOpportunityClassifications": {
|
||||
sql: deleteOpportunityClassifications,
|
||||
},
|
||||
"insertOpportunityClassification": {
|
||||
sql: insertOpportunityClassification,
|
||||
},
|
||||
"selectClassificationsForOpportunities": {
|
||||
sql: selectClassificationsForOpportunities,
|
||||
in: true,
|
||||
},
|
||||
|
||||
// opportunity_marks
|
||||
"insertOpportunityMarks": {sql: insertOpportunityMarks, named: true},
|
||||
@@ -75,12 +109,17 @@ var checks = map[string]queryCheck{
|
||||
}
|
||||
|
||||
func withFilter(fragment string) string {
|
||||
return listOpportunitiesSelect + "\nWHERE " + fragment + listOpportunitiesOrderAndPage
|
||||
return listOpportunitiesSelect + "\nWHERE " + fragment +
|
||||
listOpportunitiesOrderAndPage
|
||||
}
|
||||
|
||||
func openTestDB(t *testing.T) *sqlx.DB {
|
||||
t.Helper()
|
||||
s, err := Open(context.Background(), filepath.Join(t.TempDir(), "queries.db"), WithAutoMigrate())
|
||||
s, err := Open(
|
||||
context.Background(),
|
||||
filepath.Join(t.TempDir(), "queries.db"),
|
||||
WithAutoMigrate(),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("Open: %v", err)
|
||||
}
|
||||
@@ -144,11 +183,13 @@ func TestListOpportunitiesQueryAllFilters(t *testing.T) {
|
||||
Limit: 10,
|
||||
Offset: 5,
|
||||
})
|
||||
|
||||
stmt, err := db.Preparex(query)
|
||||
if err != nil {
|
||||
t.Fatalf("prepare:\n%s\n%v", query, err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
var rows []opportunityRow
|
||||
if err := stmt.Select(&rows, args...); err != nil {
|
||||
t.Errorf("run with %d args:\n%s\n%v", len(args), query, err)
|
||||
@@ -162,6 +203,7 @@ func TestQueriesAllListed(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var declared []string
|
||||
for _, decl := range file.Decls {
|
||||
gen, ok := decl.(*ast.GenDecl)
|
||||
@@ -174,17 +216,21 @@ func TestQueriesAllListed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(declared)
|
||||
if len(declared) == 0 {
|
||||
t.Fatal("found no constants in queries.go")
|
||||
}
|
||||
|
||||
for _, name := range declared {
|
||||
if _, ok := checks[name]; !ok {
|
||||
t.Errorf("queries.go constant %s is not listed in checks", name)
|
||||
}
|
||||
}
|
||||
|
||||
for name := range checks {
|
||||
if i := sort.SearchStrings(declared, name); i == len(declared) || declared[i] != name {
|
||||
if i := sort.SearchStrings(declared, name); i == len(declared) ||
|
||||
declared[i] != name {
|
||||
t.Errorf("checks lists %s, which queries.go does not declare", name)
|
||||
}
|
||||
}
|
||||
@@ -194,20 +240,27 @@ func TestQueriesAllListed(t *testing.T) {
|
||||
// together, for writes that bypass OpportunityMarks.Validate.
|
||||
func TestMarksCheckConstraint(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
if _, err := db.Exec(`INSERT INTO opportunities
|
||||
insert := `INSERT INTO opportunities
|
||||
(uuid, uuid_type, title, status, latest_release_date)
|
||||
VALUES ('ocds-1', 'ocid', 'T', 'active', '2026-01-01T00:00:00Z')`); err != nil {
|
||||
VALUES ('ocds-1', 'ocid', 'T', 'active', '2026-01-01T00:00:00Z')`
|
||||
if _, err := db.Exec(insert); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec(`INSERT INTO opportunity_marks (uuid, won, lost)
|
||||
VALUES ('ocds-1', 1, 1)`); err == nil {
|
||||
|
||||
insertWonAndLost := `INSERT INTO opportunity_marks (uuid, won, lost)
|
||||
VALUES ('ocds-1', 1, 1)`
|
||||
if _, err := db.Exec(insertWonAndLost); err == nil {
|
||||
t.Error("inserting won and lost together succeeded")
|
||||
}
|
||||
if _, err := db.Exec(`INSERT INTO opportunity_marks (uuid, won)
|
||||
VALUES ('ocds-1', 1)`); err != nil {
|
||||
|
||||
insertWon := `INSERT INTO opportunity_marks (uuid, won)
|
||||
VALUES ('ocds-1', 1)`
|
||||
if _, err := db.Exec(insertWon); err != nil {
|
||||
t.Fatalf("inserting won: %v", err)
|
||||
}
|
||||
if _, err := db.Exec(`UPDATE opportunity_marks SET lost = 1 WHERE uuid = 'ocds-1'`); err == nil {
|
||||
|
||||
updateLost := `UPDATE opportunity_marks SET lost = 1 WHERE uuid = 'ocds-1'`
|
||||
if _, err := db.Exec(updateLost); err == nil {
|
||||
t.Error("updating to won and lost together succeeded")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user