Skip to content

feat: add ratecards type to add-on pkg - #2616

Merged
chrisgacsal merged 2 commits into
mainfrom
refactor/productcatalog
Apr 7, 2025
Merged

chrisgacsal merged 2 commits into
mainfrom
refactor/productcatalog

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Apr 7, 2025 •

Copy link
Copy Markdown
Collaborator

Overview

Add addon.Ratecards collection type.

Summary by CodeRabbit

  • Refactor

    • Enhanced add-on processing by streamlining how rate cards are converted, compared, and validated for improved consistency.
  • Chores

    • Removed legacy and deprecated rate card logic to simplify and modernize the underlying infrastructure.
  • Tests

    • Updated test cases to align with the new rate card conversion and validation methods, ensuring reliable functionality.

@chrisgacsal chrisgacsal self-assigned this Apr 7, 2025
@chrisgacsal
chrisgacsal requested a review from a team as a code owner April 7, 2025 09:31
@coderabbitai

coderabbitai Bot commented Apr 7, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This pull request updates the handling and conversion of rate cards across several modules. The changes transition from using productcatalog rate card types to addon rate card pointers, introduce a new RateCards type with helper methods, and update validation and equality checks. Legacy code related to FlatFeeRateCard and UsageBasedRateCard is removed. The modifications span mapping, service, assertion, and test layers to ensure consistent conversion using methods like AsProductCatalogRateCards().

Changes

Files Change Summary
openmeter/productcatalog/addon/adapter/mapping.go, openmeter/productcatalog/addon/httpdriver/mapping.go, openmeter/productcatalog/addon/service.go, openmeter/productcatalog/addon/service/addon.go, openmeter/productcatalog/addon/service/service_test.go Updated rate card conversion and assignment by switching from productcatalog types to addon pointer types; introduced calls to AsProductCatalogRateCards() (with lo.ToPtr in tests) to transform and compare rate card slices consistently.
openmeter/productcatalog/addon/addon.go Changed the Addon struct’s RateCards field type from productcatalog.RateCards to a custom RateCards type; revised validation to iterate over individual rate cards and removed the AsProductCatalogAddon method.
openmeter/productcatalog/addon/assert.go Modified assertion calls to transform RateCards via AsProductCatalogRateCards() before comparing them, affecting equality checks in tests.
openmeter/productcatalog/addon/ratecard.go Introduced a new type RateCards (a slice of RateCard) with added methods: At, AsProductCatalogRateCards, IsAligned, Equal, and Validate to enhance rate card operations.
openmeter/productcatalog/plan/ratecard.go Removed legacy code blocks including the NewRateCardFrom function, and the definitions and methods for FlatFeeRateCard and UsageBasedRateCard.

Possibly related PRs

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
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89f824e and f42f67b.

📒 Files selected for processing (9)
  • openmeter/productcatalog/addon/adapter/mapping.go (4 hunks)
  • openmeter/productcatalog/addon/addon.go (2 hunks)
  • openmeter/productcatalog/addon/assert.go (3 hunks)
  • openmeter/productcatalog/addon/httpdriver/mapping.go (1 hunks)
  • openmeter/productcatalog/addon/ratecard.go (2 hunks)
  • openmeter/productcatalog/addon/service.go (1 hunks)
  • openmeter/productcatalog/addon/service/addon.go (1 hunks)
  • openmeter/productcatalog/addon/service/service_test.go (1 hunks)
  • openmeter/productcatalog/plan/ratecard.go (0 hunks)
💤 Files with no reviewable changes (1)
  • openmeter/productcatalog/plan/ratecard.go
🧰 Additional context used
🧬 Code Definitions (3)
openmeter/productcatalog/addon/httpdriver/mapping.go (2)
openmeter/productcatalog/addon/ratecard.go (1)
  • RateCards (153-153)
openmeter/productcatalog/ratecard.go (1)
  • RateCards (350-350)
openmeter/productcatalog/addon/ratecard.go (3)
openmeter/productcatalog/ratecard.go (2)
  • RateCard (28-38)
  • RateCards (350-350)
pkg/isodate/date.go (1)
  • String (16-16)
pkg/models/error.go (1)
  • NewNillableGenericValidationError (129-135)
openmeter/productcatalog/addon/adapter/mapping.go (4)
openmeter/productcatalog/addon/ratecard.go (2)
  • RateCards (153-153)
  • RateCard (61-64)
openmeter/productcatalog/ratecard.go (2)
  • RateCards (350-350)
  • RateCard (28-38)
openmeter/ent/db/addonratecard.go (2)
  • AddonRateCard (21-63)
  • AddonRateCard (99-122)
openmeter/productcatalog/plan/ratecard.go (1)
  • RateCard (64-67)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: Quickstart
  • GitHub Check: E2E
  • GitHub Check: CI
  • GitHub Check: Test
  • GitHub Check: Lint
  • GitHub Check: Developer environment
  • GitHub Check: Commit hooks
  • GitHub Check: Analyze (go)
  • GitHub Check: Build
🔇 Additional comments (17)
openmeter/productcatalog/addon/service/addon.go (1)

590-590: Correctly implementing type conversion for RateCards

The code now uses AsProductCatalogRateCards() method to convert the source addon's RateCards to the appropriate type, ensuring consistent handling of rate cards between addon and productcatalog packages.

openmeter/productcatalog/addon/httpdriver/mapping.go (1)

35-35: Properly converting RateCards type in FromAddon function

The code now correctly calls AsProductCatalogRateCards() to ensure the RateCards are in the right format before converting each rate card using http.FromRateCard(). This change maintains type consistency between the addon and productcatalog packages.

openmeter/productcatalog/addon/service/service_test.go (1)

226-226: Test correctly handles RateCards type conversion

The code now properly converts the published addon's RateCards to productcatalog RateCards format using AsProductCatalogRateCards() and wraps it with lo.ToPtr() to match the expected pointer type. This ensures the test correctly validates the type changes introduced in the PR.

openmeter/productcatalog/addon/service.go (1)

176-176: Equal method properly handles type conversion for comparison

The code now correctly converts p.RateCards to productcatalog format using AsProductCatalogRateCards() before comparing with i.RateCards. This ensures type-consistent equality checks between addon and productcatalog RateCards types.

openmeter/productcatalog/addon/assert.go (3)

24-24: Change in equality validation for rate cards

The modification is updating AssertAddonRateCardsEqual call to convert a.RateCards using the new AsProductCatalogRateCards() method. This aligns with the new RateCards type that's been introduced in the addon package.


49-49: Appropriate conversion of optional rate cards

The update correctly handles the case where i.RateCards is a pointer by dereferencing it before passing to AsProductCatalogRateCards(). This ensures proper conversion when comparing against the addon's rate cards.


63-63: Consistent conversion in bidirectional comparison

The equality comparison now correctly converts both expected and actual RateCards to product catalog rate cards before comparison, ensuring consistency in the comparison logic regardless of source.

openmeter/productcatalog/addon/adapter/mapping.go (3)

46-46: Updated rate cards type initialization

The initialization has been changed to use the new addon.RateCards type instead of productcatalog.RateCards, aligning with the type changes in the Addon struct.


57-57: Changed to append dereferenced rate card pointer

The code now dereferences the pointer returned by fromAddonRateCardRow when appending to the rate cards slice. This change aligns with the new type structure where RateCards is a slice of RateCard (not pointers).


68-119: Return type change for rate card conversion

The function now returns a pointer to addon.RateCard instead of a productcatalog.RateCard. This change is part of the transition to use the addon-specific rate card types throughout the codebase.

The implementation correctly creates and validates the appropriate rate card type based on the input type (flat fee or usage-based).

openmeter/productcatalog/addon/addon.go (2)

19-19: Updated field type to use new RateCards type

The RateCards field type has been changed from productcatalog.RateCards to the new RateCards type defined in the addon package. This change is part of the overall refactoring to use addon-specific rate card types.


37-41: Improved validation for rate cards

The validation logic has been updated to individually validate each rate card in the collection instead of calling a single validation method on the collection. This approach provides more granular error reporting.

openmeter/productcatalog/addon/ratecard.go (5)

153-157: New RateCards type with accessor method

A new RateCards type has been defined as a slice of RateCard with an At method for accessing elements at a specific index. This introduces a more structured approach to managing collections of rate cards.


159-167: Added conversion method to maintain compatibility

The AsProductCatalogRateCards() method provides a clean way to convert the addon-specific rate cards to the product catalog format. This maintains compatibility with existing code that expects the product catalog format.


169-183: Helpful alignment check for billing periods

The IsAligned() method efficiently checks if all rate cards have the same billing period (or no billing period). This is a useful utility for ensuring consistent billing across multiple rate cards.


185-219: Comprehensive equality check implementation

The Equal method provides a thorough comparison between two sets of rate cards. It correctly handles:

  1. Different lengths
  2. Different keys
  3. Detailed equality comparison of each rate card

The implementation uses map-based lookups for efficiency and ensures both collections contain the same rate cards.


221-231: Consistent validation implementation

The Validate() method follows the same pattern as other validation functions in the codebase:

  1. Collects errors from individual validations
  2. Joins them into a single error using errors.Join
  3. Returns a properly formatted validation error or nil

This ensures consistent error handling throughout the codebase.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@chrisgacsal
chrisgacsal force-pushed the refactor/productcatalog branch from 5ed4393 to f42f67b Compare April 7, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants