Skip to content

chore(lint): enforce Go declaration ordering and mandatory GoDoc comments - #1271

Merged
kyasbal merged 1 commit into
mainfrom
push-yvsmznvluwym
Nov 13, 2026
Merged

kyasbal merged 1 commit into
mainfrom
push-yvsmznvluwym

Conversation

@kyasbal

@kyasbal kyasbal commented Nov 13, 2026

Copy link
Copy Markdown
Member

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)

  • Import section ordering (gci + goimports): Enforces three blank-line-separated import groups: standard library -> third-party packages -> github.com/GoogleCloudPlatform/khi/... internal packages.
  • Top-level declaration ordering (decorder): Enforces const -> var -> type -> func (init() first among functions; var _ Interface = (*Impl)(nil) assertions placed in var).
  • Constructor & method ordering (funcorder): Places New* constructors before struct methods, and exported struct methods before unexported struct methods.
  • Mandatory GoDoc & sentence formatting (revive exported, godoclint, godot, staticcheck ST1020–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), with max-issues-per-linter: 0 and max-same-issues: 0.
  • Go AST Convention Checker (scripts/codequality --lang=go --conventions):
    • decl-visibility-order: Enforces exported-before-unexported ordering within each declaration kind (const without reordering inside iota blocks, var ignoring blank _ checks, type, and func tiers).
    • exported-member-doc: Enforces GoDoc comments ending with . on all exported struct fields and exported interface methods of exported types.
    • Wired into make lint-go (with LINT_PKGS package filtering support) and .github/workflows/pullrequest.yaml, and documented in .agents/rules/go-coding-rule.md & .agents/rules/comment-guideline.md.
  • Dynamic Quality Gate Scope Optimization (scripts/codequality):
    • Uses comment-insensitive function token hashing (ExtractFuncTokenHashes) so purely reordered or comment-edited functions are skipped by Stage-2 coverage and mutation runs.

2. Repository-Wide Alignment

  • Reordered declarations/methods/imports and added accurate GoDoc comments across all non-generated Go packages in cmd/, pkg/, and scripts/ with zero behavioral changes.

Verification

  • make format-go: 0 issues
  • make lint-go (golangci-lint + codequality --conventions): 0 issues
  • make build-go: PASS
  • make test-go: PASS
  • make codequality-go CODEQUALITY_MAX=0: PASS (0 violations) (Patch coverage 99.4%, Mutation score 100.0%: 164/164 killed on newly added code)
  • make pre-commit: PASS
@kyasbal
kyasbal merged commit 05004e8 into main Nov 13, 2026
19 checks passed
@kyasbal
kyasbal deleted the push-yvsmznvluwym branch November 13, 2026 08:40

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +143 to +146
if _, err := snappy.Decode(decompressed, compressed); err != nil {
return nil, fmt.Errorf("failed to decompress snappy block: %w", err)
}
return decompressed, nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
Comment on lines +195 to +201
newCap := len(slice) * 2
if newCap < required {
newCap = required
}
newSlice := make([]T, newCap)
copy(newSlice, slice)
return newSlice

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. 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.
Comment on lines +168 to +169
case *pb.InternedValue_TimestampValue:
return structured.NewStandardScalarNode(kind.TimestampValue.AsTime()), nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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), nil
References
  1. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant