Repository navigation
chore(lint): enforce Go declaration ordering and mandatory GoDoc comments - #1271
Conversation
There was a problem hiding this comment.
Code Review
This pull request performs a comprehensive refactoring across the codebase to enforce strict Go coding standards, including import grouping, top-level declaration ordering, and GoDoc conventions, alongside enabling corresponding linters in .golangci.yaml. The review feedback highlights three key improvement opportunities: utilizing the returned slice from snappy.Decode in lazyjson_block.go for safety, replacing manual slice-growing logic with slices.Grow in readonly_intern.go for idiomatic Go, and adding an explicit nil check before calling .AsTime() on timestamppb.Timestamp in struct.go to avoid unexpected Unix epoch values.
| if _, err := snappy.Decode(decompressed, compressed); err != nil { | ||
| return nil, fmt.Errorf("failed to decompress snappy block: %w", err) | ||
| } | ||
| return decompressed, nil |
There was a problem hiding this comment.
The snappy.Decode function returns the decoded slice, which should be used instead of the pre-allocated decompressed slice directly. While decompressed is pre-allocated with the correct length, using the returned slice is safer and adheres to the library's API contract.
res, err := snappy.Decode(decompressed, compressed)
if err != nil {
return nil, fmt.Errorf("failed to decompress snappy block: %w", err)
}
return res, nil| newCap := len(slice) * 2 | ||
| if newCap < required { | ||
| newCap = required | ||
| } | ||
| newSlice := make([]T, newCap) | ||
| copy(newSlice, slice) | ||
| return newSlice |
There was a problem hiding this comment.
Instead of manually implementing slice doubling and copying logic, use the standard library's slices.Grow to expand the capacity and then reslice. This is more idiomatic, concise, and avoids manual allocation and copying logic.
| newCap := len(slice) * 2 | |
| if newCap < required { | |
| newCap = required | |
| } | |
| newSlice := make([]T, newCap) | |
| copy(newSlice, slice) | |
| return newSlice | |
| return slices.Grow(slice, required-len(slice))[:required] |
References
- In Go, to grow a slice to a specific length without allocating temporary slices, use the standard library's slices.Grow to expand the capacity and then reslice, rather than manually implementing slice doubling and copying logic.
| case *pb.InternedValue_TimestampValue: | ||
| return structured.NewStandardScalarNode(kind.TimestampValue.AsTime()), nil |
There was a problem hiding this comment.
When converting a protobuf *timestamppb.Timestamp to Go's time.Time using .AsTime(), perform an explicit nil check if Go's zero time.Time{} is expected for nil values, because (*timestamppb.Timestamp)(nil).AsTime() returns the Unix epoch (1970-01-01) instead of the zero time.
case *pb.InternedValue_TimestampValue:
var t time.Time
if kind.TimestampValue != nil {
t = kind.TimestampValue.AsTime()
}
return structured.NewStandardScalarNode(t), nilReferences
- When converting a protobuf *timestamppb.Timestamp to Go's time.Time using .AsTime(), perform an explicit nil check if Go's zero time.Time{} is expected for nil values, because (*timestamppb.Timestamp)(nil).AsTime() returns the Unix epoch (1970-01-01) instead of the zero time.
Summary
Enforces strict Go import/declaration/method ordering and mandatory GoDoc comments across the entire KHI repository, and aligns all existing Go packages with zero remaining violations.
1. Stricter Go Linting & Formatting (
.golangci.yaml,scripts/make/lint.mk, CI)gci+goimports): Enforces three blank-line-separated import groups: standard library -> third-party packages ->github.com/GoogleCloudPlatform/khi/...internal packages.decorder): Enforcesconst -> var -> type -> func(init()first among functions;var _ Interface = (*Impl)(nil)assertions placed invar).funcorder): PlacesNew*constructors before struct methods, and exported struct methods before unexported struct methods.reviveexported,godoclint,godot,staticcheckST1020–ST1022): Requires symbol-named GoDoc comments ending with a period (.) on all exported top-level declarations and methods (excluding package-level comments and*_test.go/ generated files), withmax-issues-per-linter: 0andmax-same-issues: 0.scripts/codequality --lang=go --conventions):decl-visibility-order: Enforces exported-before-unexported ordering within each declaration kind (constwithout reordering insideiotablocks,varignoring blank_checks,type, andfunctiers).exported-member-doc: Enforces GoDoc comments ending with.on all exportedstructfields and exportedinterfacemethods of exported types.make lint-go(withLINT_PKGSpackage filtering support) and.github/workflows/pullrequest.yaml, and documented in.agents/rules/go-coding-rule.md&.agents/rules/comment-guideline.md.scripts/codequality):ExtractFuncTokenHashes) so purely reordered or comment-edited functions are skipped by Stage-2 coverage and mutation runs.2. Repository-Wide Alignment
cmd/,pkg/, andscripts/with zero behavioral changes.Verification
make format-go:0 issuesmake lint-go(golangci-lint+codequality --conventions):0 issuesmake build-go:PASSmake test-go:PASSmake codequality-go CODEQUALITY_MAX=0:PASS (0 violations)(Patch coverage99.4%, Mutation score100.0%:164/164killed on newly added code)make pre-commit:PASS