Skip to content

feat: auto-generate ServiceResourcePath constants (#75)#83

Merged
yimsk merged 6 commits intodevelopfrom
feat/issue-75
Jan 3, 2026
Merged

feat: auto-generate ServiceResourcePath constants (#75)#83
yimsk merged 6 commits intodevelopfrom
feat/issue-75

Conversation

@yimsk
Copy link
Copy Markdown
Contributor

@yimsk yimsk commented Jan 3, 2026

Summary

  • Auto-generate ServiceResourcePath constants in each resource package for consistent error messages
  • Rename custom/cfn to custom/cloudformation to match AWS CLI naming conventions
  • Fix transit-gateways BaseDAO service name (ec2vpc)

Changes

  • 163 constants.go files generated - each resource now has ServiceResourcePath = "service/resource"
  • genimports enhanced - generates both imports and constants files with build verification
  • Test coverage added - 397 lines of new tests for genimports package

Closes #75

yimsk added 5 commits January 3, 2026 02:14
…r strings

Fixes #75 - extends gen-imports to generate constants.go in each resource
package with ServiceResourcePath constant, updates all 163 DAO files to use
it instead of hardcoded strings.
- Add unit tests for GenerateConstantsFile, GetServiceDisplayName,
  GroupByService, GetPackageInfo functions
- Add build verification step to gen-imports script to catch
  compilation errors early
- Rename custom/cfn/ to custom/cloudformation/ (AWS CLI convention)
- Add registry.DefaultDisplayNames() as single source of truth
- Remove duplicate ServiceDisplayNames map from genimports (-70 lines)
- genimports now imports registry for display name lookup
… coverage

- Fix vpc/transit-gateways BaseDAO: ec2 -> vpc (match ServiceResourcePath)
- Apply strings.CutPrefix modernization in readPackageName
- Add comprehensive tests for FindRegisterPackages, GetProjectRoot, readPackageName
- Improve genimports test coverage: 48.3% -> 94.8%
@claude

This comment was marked as resolved.

@claude
Copy link
Copy Markdown

claude Bot commented Jan 3, 2026

PR Review: Auto-generate ServiceResourcePath constants (#75)

Summary

This PR implements automatic generation of ServiceResourcePath constants across all 163 resource packages to ensure consistent error messages. It also includes the custom/cfn to custom/cloudformation rename, fixes a bug in transit-gateways, and adds comprehensive test coverage for the genimports package.

✅ Strengths

1. Excellent Code Quality

  • Clean implementation: The genimports package is well-structured with clear separation of concerns
  • Modern Go idioms: Uses strings.CutPrefix (line 128 in genimports.go) instead of older strings.TrimPrefix + manual checking
  • Proper error handling: Consistent use of error wrapping with context

2. Outstanding Test Coverage

  • Comprehensive test suite: 397 lines of new tests covering all major functions
  • Test coverage improvement: 48.3% → 94.8% for genimports package (mentioned in commit message)
  • Edge case testing: Tests handle hyphenated directories, missing files, empty files, and other edge cases
  • Good test organization: Clear test names and well-structured table-driven tests

3. Build Verification

  • CI verification step ensures generated files stay in sync (ci.yml:25-28)
  • Generator includes build step to catch compilation errors early
  • Clear error messages guide developers to run task gen-imports

4. Documentation

  • Updated adding-resources.md with "Generated Files" section (lines 496-538)
  • Clear comments in generated files indicating they're auto-generated
  • Regeneration instructions included in every generated file

5. Consistency Improvements

  • DRY principle: Eliminates 163 hardcoded "service/resource" strings
  • Single source of truth: Uses registry.DefaultDisplayNames() instead of duplicating the map in genimports
  • Better naming: custom/cloudformation aligns with AWS CLI conventions

🔍 Code Review Details

genimports.go

Good practices:

  • Clean separation: FindRegisterPackages, GroupByService, GetPackageInfo each have single responsibilities
  • Proper sorting for deterministic output (line 41)
  • Fallback package name logic handles edge cases (lines 103-107)

Minor observation:

  • Line 357-359 in genimports_test.go: The test allows "main // comment" as package name, but this might not be the desired behavior. Consider stripping comments after package name extraction.

transit-gateways fix

Critical bug fix:

  • Line 29 in dao.go: Correctly uses dao.NewBaseDAO("vpc", "transit-gateways")
  • This matches the ServiceResourcePath = "vpc/transit-gateways" constant
  • Previously was incorrectly using "ec2" as the service name

🎯 Potential Issues & Suggestions

1. Test Case: Package Name with Comments (Minor)

Location: genimports_test.go:347-360

The test "handles package with trailing content" expects readPackageName to return "main // comment" for input package main // comment. However, this includes the comment in the package name, which seems incorrect.

Suggestion:

func readPackageName(filePath string) string {
    // ... existing code ...
    for scanner.Scan() {
        line := strings.TrimSpace(scanner.Text())
        if pkg, ok := strings.CutPrefix(line, "package "); ok {
            // Strip comments
            if idx := strings.Index(pkg, "//"); idx >= 0 {
                pkg = strings.TrimSpace(pkg[:idx])
            }
            return pkg
        }
    }
    return ""
}

Impact: Low - Real register.go files are unlikely to have inline comments after package declarations

2. Error Handling in FindRegisterPackages (Low)

Location: genimports.go:23

The function returns early on any error during filepath.Walk, which could fail silently if a single file has permission issues.

Suggestion: Consider logging warnings for individual file errors while continuing to walk the directory tree, only returning error for critical failures.

Impact: Low - Unlikely in normal development environments

3. Missing Test: Integration Test (Enhancement)

The test suite has excellent unit test coverage but lacks an integration test that:

  1. Runs the actual generator on a small test project
  2. Verifies generated constants.go files compile
  3. Verifies generated imports_custom.go compiles

Suggestion: Add an integration test in cmd/claws/generate_test.go or similar.

Impact: Low - Current CI verification provides similar protection

4. Generated File Headers (Minor - Enhancement)

Location: genimports.go:137-144

The generated constants.go files include "To regenerate: task gen-imports" but the actual command is go generate ./... (as shown in CI).

Suggestion: Update comment to:

// To regenerate: task gen-imports (or: go generate ./...)

Impact: Very Low - Documentation clarity only

🔒 Security Considerations

No security concerns identified

  • No user input is processed without validation
  • File operations are properly scoped to project directory
  • No SQL injection, XSS, or other OWASP risks
  • Generated code doesn't introduce security vulnerabilities

⚡ Performance Considerations

Good performance characteristics

  • Generator runs during development, not runtime
  • No performance impact on production code
  • File walking is efficient with early returns
  • Caching in Registry (lines 59-62) is well-implemented

📊 Test Coverage

Excellent coverage:

  • ✅ GenerateConstantsFile: Multiple scenarios tested
  • ✅ GetServiceDisplayName: Edge cases covered
  • ✅ GroupByService: Grouping logic verified
  • ✅ GetPackageInfo: Path parsing tested
  • ✅ FindRegisterPackages: File discovery tested with temp directories
  • ✅ GetProjectRoot: Working directory handling tested
  • ✅ readPackageName: File parsing tested

Minor gap:

  • Integration testing (mentioned above)

🎨 Best Practices Adherence

Follows Go best practices:

  • Idiomatic Go code throughout
  • Proper use of defer for resource cleanup
  • Table-driven tests
  • Clear function names and comments
  • No unnecessary abstractions

Follows project conventions:

  • Matches existing codebase style
  • Integrates with existing task runner
  • Updates documentation alongside code
  • CI verification ensures consistency

📝 Documentation Quality

Excellent documentation:

  • Updated adding-resources.md with comprehensive "Generated Files" section
  • Clear usage examples in documentation
  • Generated files include regeneration instructions
  • Commit messages are descriptive

🔄 Refactoring Quality

The cfn → cloudformation rename is well-executed:

  • ✅ Package directory renamed
  • ✅ Import paths updated in imports_custom.go
  • ✅ Registry aliases maintained for backward compatibility (lines 80-81)
  • ✅ Display names unified in registry.DefaultDisplayNames()
  • ✅ Removed duplicate ServiceDisplayNames map (-70 lines)

Final Verdict

APPROVE with minor suggestions

This is a high-quality PR that:

  • ✅ Solves the stated problem (consistent error messages via ServiceResourcePath)
  • ✅ Improves code maintainability (eliminates 163 hardcoded strings)
  • ✅ Includes excellent test coverage (94.8%)
  • ✅ Fixes a real bug (transit-gateways service name)
  • ✅ Improves naming consistency (cfn → cloudformation)
  • ✅ Has proper CI verification
  • ✅ Updates documentation

The minor suggestions are optional enhancements that don't block merging:

  1. Strip comments from package names in readPackageName
  2. Consider integration testing
  3. Clarify regeneration command in generated file headers

No blocking issues identified. The code is production-ready.

Metrics

  • Files changed: 326+
  • Lines added: 1,878
  • Lines deleted: 250
  • New constants.go files: 163
  • Test coverage: 94.8% (genimports)
  • Bug fixes: 1 (transit-gateways)

Great work! 🎉

@yimsk yimsk merged commit 098826a into develop Jan 3, 2026
5 checks passed
@yimsk yimsk deleted the feat/issue-75 branch January 3, 2026 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant