Repository navigation
refactor: subject fixes - #2957
Conversation
📝 WalkthroughWalkthroughThis change refactors the subject management subsystem to use value types instead of pointers for Changes
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (1.64.8)Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (4)
⏰ Context from checks skipped due to timeout of 90000ms (11)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
openmeter/subject/adapter/subject.go (1)
62-72: Track the technical debt for nullable field handling.The FIXME comment correctly identifies this as a unique pattern that should be refactored. Consider creating a helper function or adopting a consistent pattern across all adapters.
Would you like me to create an issue to track this technical debt and propose a solution for consistent nullable field handling across all adapters?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (13)
app/common/subject.go(1 hunks)cmd/balance-worker/wire_gen.go(1 hunks)cmd/server/wire_gen.go(1 hunks)openmeter/entitlement/balanceworker/entitlementhandler.go(2 hunks)openmeter/entitlement/balanceworker/recalculate.go(1 hunks)openmeter/entitlement/balanceworker/subject.go(1 hunks)openmeter/server/server_test.go(1 hunks)openmeter/subject/adapter.go(1 hunks)openmeter/subject/adapter/adapter.go(1 hunks)openmeter/subject/adapter/subject.go(2 hunks)openmeter/subject/httphandler/subject.go(4 hunks)openmeter/subject/service.go(1 hunks)openmeter/subject/service/service.go(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (5)
app/common/subject.go (1)
openmeter/subject/adapter.go (1)
Adapter(12-22)
cmd/balance-worker/wire_gen.go (1)
app/common/subject.go (1)
NewSubjectAdapter(54-58)
cmd/server/wire_gen.go (2)
app/common/subject.go (1)
NewSubjectAdapter(54-58)cmd/server/wire.go (1)
Application(41-77)
openmeter/subject/adapter/adapter.go (5)
openmeter/ent/db/client.go (2)
Client(65-153)Driver(258-262)openmeter/subject/adapter.go (1)
Adapter(12-22)pkg/framework/entutils/transaction.go (2)
TxUser(179-195)TxDriver(76-88)openmeter/ent/db/tx.go (1)
Tx(15-108)openmeter/ent/db/expose.go (1)
NewTxClientFromRawConfig(78-176)
openmeter/server/server_test.go (5)
openmeter/subject/service.go (3)
CreateInput(41-47)UpdateInput(68-74)ListParams(33-38)openmeter/ent/db/subject.go (2)
Subject(17-38)Subject(41-56)openmeter/subject/subject.go (1)
Subject(11-18)openmeter/entitlement/balanceworker/worker.go (1)
NamespacedID(38-38)pkg/models/key.go (1)
NamespacedKey(5-8)
⏰ Context from checks skipped due to timeout of 90000ms (11)
- GitHub Check: E2E
- GitHub Check: CI
- GitHub Check: Quickstart
- GitHub Check: Test
- GitHub Check: Migration Checks
- GitHub Check: Lint
- GitHub Check: Developer environment
- GitHub Check: Commit hooks
- GitHub Check: Build
- GitHub Check: Analyze (javascript)
- GitHub Check: Analyze (go)
🔇 Additional comments (25)
openmeter/entitlement/balanceworker/entitlementhandler.go (2)
269-275: LGTM: Consistent refactoring to use centralized subject resolution.The replacement of
GetByKeyWithFallbackwithresolveSubjectIfExistsaligns with the broader refactoring to use stronger typing and centralized subject resolution logic. The error handling is preserved correctly.
341-347: LGTM: Consistent pattern applied for deleted snapshot events.The same refactoring pattern is correctly applied here, maintaining consistency across the codebase. The
resolveSubjectIfExistshelper function centralizes the subject resolution logic effectively.openmeter/entitlement/balanceworker/recalculate.go (1)
328-334: LGTM: Refactoring applied consistently.The replacement of
GetByKeyWithFallbackwithresolveSubjectIfExistsfollows the same pattern used throughout the codebase refactoring. The error handling and return behavior are preserved correctly.app/common/subject.go (1)
56-58: LGTM: Improved error handling for adapter initialization.The function signature change to return
(subject.Adapter, error)enables proper error handling during adapter creation. This aligns with Go best practices for initialization functions and allows upstream callers to handle initialization failures gracefully.cmd/balance-worker/wire_gen.go (1)
238-247: LGTM: Proper error handling and resource cleanup.The error handling for
NewSubjectAdapterfollows the established pattern in this wire-generated file. The cleanup sequence is correctly called in reverse order (cleanup6 through cleanup) to ensure proper resource management when adapter creation fails.cmd/server/wire_gen.go (1)
461-471: LGTM! Proper error handling for subject adapter initialization.The error handling and cleanup sequence is correctly implemented, following the established pattern in this generated file where resources are cleaned up in reverse order of initialization.
openmeter/entitlement/balanceworker/subject.go (1)
11-24: LGTM! Well-implemented subject resolution helper.The function correctly handles the not-found case by returning a minimal subject with only the key populated, which aligns with the previous
GetByKeyWithFallbackbehavior. Error wrapping provides good context for debugging.openmeter/subject/httphandler/subject.go (2)
321-324: LGTM! Improved type safety with strongly typed deletion.The change to use
models.NamespacedIDwith explicit namespace and ID fields improves type safety and makes the API more explicit about what's being deleted.
98-98: LGTM! Consistent use of value types.The changes from pointer to value types for
Subjectentities align with the broader refactoring and reduce the risk of nil pointer dereferences.Also applies to: 188-189, 193-193, 196-196
openmeter/subject/adapter/adapter.go (3)
16-24: LGTM! Good defensive programming with nil check.The constructor now properly validates the required database client parameter and returns a descriptive error, preventing runtime panics from nil pointer dereferences.
26-29: LGTM! Compile-time interface verification.Good practice to verify interface implementations at compile time. This ensures the adapter correctly implements both the subject.Adapter and transaction user interfaces.
35-55: LGTM! Well-implemented transaction support.The transaction methods are correctly implemented:
Txproperly hijacks the transaction and wraps it with error handlingWithTxcreates a new adapter instance bound to the transactionSelfprovides the identity function required by the TxUser interfaceThis enables proper transaction management for subject operations.
openmeter/subject/service.go (1)
12-22: LGTM: Excellent interface improvements for type safety and consistency.The refactored service interface demonstrates strong improvements:
- Value returns eliminate potential nil pointer issues
- Strong typing with
models.NamespacedKeyandmodels.NamespacedIDenhances type safety- Clear method separation (
GetByKeyvsGetById) improves API clarity- Appropriate deprecation guidance for the convenience method
openmeter/server/server_test.go (1)
1514-1540: LGTM: Test service correctly implements the refactored interface.The
NoopSubjectServicehas been properly updated to match the new service interface:
- Return types correctly changed to
subject.Subjectvalues- Method signatures align with the refactored interface including typed parameters
- Maintains appropriate no-op behavior for testing
openmeter/subject/service/service.go (4)
27-35: LGTM: Excellent implementation of input validation and transaction management.The
Createmethod demonstrates best practices:
- Input validation with proper error wrapping using
models.NewGenericValidationError- Transaction management with
transaction.Runaligns with PR objectives- Value return type improves consistency
38-46: LGTM: Consistent implementation pattern.The
Updatemethod follows the same excellent pattern asCreatewith validation and transaction management.
56-75: LGTM: Strong typing with proper validation.The new
GetByIdandGetByKeymethods demonstrate excellent practices:
- Strong typing with
models.NamespacedIDandmodels.NamespacedKey- Input validation for typed parameters
- Consistent transaction wrapping
- Clear method separation improves API usability
85-93: LGTM: Proper transaction handling for delete operations.The
Deletemethod (renamed fromDeleteById) correctly:
- Uses strong typing with
models.NamespacedID- Includes input validation
- Uses
transaction.RunWithNoValueappropriate for operations without return valuesopenmeter/subject/adapter.go (2)
12-22: LGTM: Adapter interface properly aligned with service changes.The adapter interface demonstrates excellent improvements:
- Value returns consistent with service interface
- Strong typing with
models.NamespacedKeyandmodels.NamespacedID- Transaction support through
entutils.TxCreatorembedding- Clear method separation and consistent naming
24-40: LGTM: Well-designed input validation struct.The
GetSubjectAdapterInputstruct with validation logic shows good design:
- Clear validation requirements (namespace required, ID or Key required)
- Proper error messages for validation failures
- Follows consistent validation patterns
openmeter/subject/adapter/subject.go (5)
20-49: Well-implemented transactional Create method with proper error handling.The refactoring to use value types and transaction management improves consistency. The constraint error handling for duplicate keys provides clear feedback.
106-160: Excellent implementation of flexible subject retrieval with deterministic behavior.The function properly handles the ambiguity of ID vs key lookups by preferring ID matches over key matches. This deterministic behavior ensures consistent results.
162-184: Clean implementation with strong typing.Good use of
models.NamespacedKeyfor type safety and validation.
211-263: Comprehensive list implementation with advanced features.The pagination, filtering, searching, and sorting capabilities are well-implemented. The null handling in display name sorting ensures consistent ordering.
289-291: Good defensive programming for metadata initialization.Ensuring metadata is never nil prevents potential nil pointer issues downstream.
Misc refactors to make the subjects codebase more aligned with our transaction handling pracices.
Summary by CodeRabbit
New Features
Refactor
Bug Fixes
Tests