Skip to content

chore: remove redundant structure for account with definition - #5872

Merged
ItsSudip merged 11 commits into
masterfrom
chore/refactorAccountType
May 22, 2025
Merged

ItsSudip merged 11 commits into
masterfrom
chore/refactorAccountType

Conversation

@ItsSudip

@ItsSudip ItsSudip commented May 19, 2025 •

Copy link
Copy Markdown
Member

Description

We are removing redundant structure accountWithDefinition. This name is misleading. It is possible AccountDefinition can be undefined in account. So made it optional in account.

Copilot Generated

This pull request refactors the account association logic in the backend configuration to improve code reuse and maintainability. It introduces a new helper function, populateAccountToDestination, and removes the redundant AccountWithDefinition struct. Additionally, it updates the test suite to align with these changes and includes a new test for JSON serialization of destination accounts.

Refactoring and Code Reuse:

  • Added the populateAccountToDestination helper function to centralize the logic for populating account details into destinations, improving code reuse. (backend-config/account_association.go, backend-config/account_association.goR24-R58)
  • Refactored setDestinationAccounts to use the new helper function, simplifying the logic for setting both delivery and delete accounts. (backend-config/account_association.go, backend-config/account_association.goL40-R80)

Structural Changes:

  • Replaced the AccountWithDefinition struct with the existing Account struct, adding a pointer to AccountDefinition for consistency and simplification. (backend-config/types.go, [1] [2] [3]

Test Updates:

  • Updated tests in account_association_test.go to reflect the changes in struct field names (e.g., Id to ID) and the removal of AccountWithDefinition. (backend-config/account_association_test.go, [1] [2] [3] [4]
  • Added a new test case to validate JSON serialization and deserialization of destination accounts, ensuring compatibility with the updated Account struct. (backend-config/account_association_test.go, backend-config/account_association_test.goL241-R295)

Dependency Addition:

Linear Ticket

https://linear.app/rudderstack/issue/INT-3685/refactor-logic-for-merging-account-information-with-destination

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

@koladilip
koladilip self-requested a review May 19, 2025 11:24
@ItsSudip ItsSudip self-assigned this May 19, 2025
@koladilip

koladilip commented May 19, 2025 •

Copy link
Copy Markdown
Contributor

I've made the following changes to the PR:

  1. Refactored the setDestinationAccounts method in account_association.go to use a new utility function populateAccountToDestination that handles populating an account to a destination based on the account ID

  2. Added a test for JSON serialization/deserialization of the DestinationT struct with accounts to ensure that the JSON field names (deliveryAccount and deleteAccount) are correctly handled

These changes improve code maintainability by reducing duplication and adding test coverage for the JSON serialization aspects of the refactored code.

@koladilip
koladilip force-pushed the chore/refactorAccountType branch from 29eb565 to 4c85ac4 Compare May 19, 2025 11:48
@codecov

codecov Bot commented May 19, 2025 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.97%. Comparing base (9d8b573) to head (c88272c).
Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5872      +/-   ##
==========================================
+ Coverage   76.91%   76.97%   +0.05%     
==========================================
  Files         492      492              
  Lines       67808    67806       -2     
==========================================
+ Hits        52157    52195      +38     
+ Misses      12812    12780      -32     
+ Partials     2839     2831       -8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ItsSudip ItsSudip changed the title chore: remove redundant structur for account with definition chore: remove redundant structure for account with definition May 19, 2025
@koladilip
koladilip force-pushed the chore/refactorAccountType branch from 4c85ac4 to 5a455be Compare May 19, 2025 13:04
Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Resolved
@ItsSudip
ItsSudip requested review from atzoum and utsabc May 20, 2025 06:11
@koladilip

Copy link
Copy Markdown
Contributor

I've addressed the issue raised by @utsabc regarding the handling of non-existent account definitions. The changes include:

  1. Fixed the issue with &accountDefinition: When AccountDefinitionName is blank or doesn't exist in the map, we now set AccountDefinition to nil instead of a pointer to an empty struct.

  2. Added condition for warning logs: Only log warnings when AccountDefinitionName is not empty but doesn't exist in the map. This avoids unnecessary warnings when AccountDefinitionName is intentionally blank.

  3. Added a specific test case: Created a test for blank account definition names that verifies the AccountDefinition field is properly set to nil.

This ensures that when an account has a blank account definition name, the code doesn't create a misleading pointer to an empty account definition struct, which could cause issues elsewhere in the codebase.

@koladilip

Copy link
Copy Markdown
Contributor

I've further refactored the code to make it more elegant by extracting a dedicated helper function getAccountDefinition that handles the account definition lookup logic:

func (c *ConfigT) getAccountDefinition(accountDefinitionName, accountID, accountField, destinationID, flowType string) *AccountDefinition {
    if accountDefinitionName == "" {
        return nil
    }

    if accountDefinition, exists := c.AccountDefinitions[accountDefinitionName]; exists {
        return &accountDefinition
    }

    // Log warning if account definition not found
    pkgLogger.Warnn("Account definition not found in configured accountDefinitions for "+flowType+" flow",
        logger.NewStringField(accountField, accountID),
        logger.NewStringField("accountDefinitionName", accountDefinitionName),
        logger.NewStringField("destinationId", destinationID))
    
    return nil
}

This change:

  1. Improves readability: The function name clearly describes its purpose
  2. Simplifies the main function: The populateAccountToDestination function is now more focused on its core responsibility
  3. Promotes reusability: The account definition lookup logic can be reused elsewhere if needed
  4. Maintains the same behavior: All tests continue to pass

The code now handles the case of blank AccountDefinitionName more explicitly and elegantly.

@koladilip

Copy link
Copy Markdown
Contributor

I've changed the log level from warning (Warnn) to error (Errorn) for missing account definitions and accounts. This is more appropriate since these are configuration errors that should be addressed, not just warnings that can be ignored.

@atzoum atzoum 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.

Please consider simplifying to the following two methods

// setDestinationAccounts assigns accounts to a destination based on its configuration.
// It handles two types of account associations:
//
//  1. Regular account [rudderAccountId] - Used for normal event delivery flow
//  2. Delete account [rudderDeleteAccountId] - Used for data deletion/regulation flow
//
// For each account type, it:
//
//  1. Checks if the corresponding account ID exists in the destination config
//  2. Verifies the account exists in the accounts map
//  3. Creates a copy of the account which contains its definition as well
//  4. Assigns it to the appropriate field in the destination
//
// Parameters:
//   - dest: Pointer to the destination being configured
func (c *ConfigT) setDestinationAccounts(dest *DestinationT) {
	// Check and set the regular account if specified in the destination config
	if accountID, ok := dest.Config["rudderAccountId"].(string); ok && accountID != "" {
		dest.DeliveryAccount = c.populateAccountToDestination(accountID, pkgLogger.Withn(
			logger.NewStringField("accountId", accountID),
			logger.NewStringField("destinationId", dest.ID),
			logger.NewStringField("flowType", "delivery"),
		))
	}

	// Check and set the delete account if specified in the destination config
	if deleteAccountID, ok := dest.Config["rudderDeleteAccountId"].(string); ok && deleteAccountID != "" {
		dest.DeleteAccount = c.populateAccountToDestination(deleteAccountID, pkgLogger.Withn(
			logger.NewStringField("accountId", deleteAccountID),
			logger.NewStringField("destinationId", dest.ID),
			logger.NewStringField("flowType", "regulation"),
		))
	}
}

// populateAccountToDestination populates an account to a destination field based on the account ID.
// It creates an copy of the Account object by combining account details with its definition.
//
// Parameters:
//   - accountID: The ID of the account to populate
//   - log: The logger to use for logging errors
//
// Returns:
//   - *Account: The populated account object or nil if the account doesn't exist
func (c *ConfigT) populateAccountToDestination(accountID string, log logger.Logger) *Account {
	if account, ok := c.Accounts[accountID]; ok {
		if accountDefinition, ok := c.AccountDefinitions[account.AccountDefinitionName]; ok {
			account.AccountDefinition = &accountDefinition
		} else {
			log.Errorn("Account definition not found in configured definitions",
				logger.NewStringField("definition", account.AccountDefinitionName),
			)
		}
		return &account
	}
	pkgLogger.Errorn("Account not found in configured accounts")
	return nil
}

Comment thread backend-config/account_association.go Outdated
Comment on lines 37 to 40

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.

I don't understand why we need this, what it protects us from

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We are already using accounts for oauth destinations. So for those existing accounts we are not going to get any accountDefinitionName. Returning nil will help us segregating account with accountDefinition and account without accountDefinition.

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.

I have comment elsewhere as well, maybe we shouldn't be calling the method if account definition is empty

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We have removed the logic of checking empty string of accountDefinitionName to the caller.

Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Resolved
@ItsSudip
ItsSudip requested review from atzoum and koladilip May 21, 2025 03:47
Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Resolved
@ItsSudip
ItsSudip force-pushed the chore/refactorAccountType branch from f20aeb6 to 89c39da Compare May 21, 2025 06:48
@ItsSudip
ItsSudip force-pushed the chore/refactorAccountType branch from 89c39da to 417e8e6 Compare May 21, 2025 06:49
Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Resolved
Comment thread backend-config/account_association.go Outdated
Comment on lines +37 to +39
if accountDefinitionName == "" {
return 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.

It would be preferrable to avoid calling this method if accountDefinitionName is empty, since the caller has a better context with respect what an empty accountDefinitionName means. The logic behind the empty check here is not that straightforward

Suggested change
if accountDefinitionName == "" {
return nil
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved this logic to the caller.

@ItsSudip
ItsSudip merged commit 89bc62b into master May 22, 2025
@ItsSudip
ItsSudip deleted the chore/refactorAccountType branch May 22, 2025 12:43
mihir20 pushed a commit that referenced this pull request May 26, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.50.0-rc.1](v1.49.0...v1.50.0-rc.1)
(2025-05-26)


### Features

* implement aws v2 implementation
([#5852](#5852))
([18fd4fb](18fd4fb))


### Bug Fixes

* do not panic for invalid router payload
([#5880](#5880))
([f4a0e7c](f4a0e7c))
* server panicking with invalid byte sequence for encoding utf8 0x00
([#5882](#5882))
([4c1a0de](4c1a0de))
* skip source_id when fetching warehouse schema from database
([#5886](#5886))
([092ee2a](092ee2a))
* warehouse pending tables to skip
([#5867](#5867))
([864fd49](864fd49))
* warehouse transformations
([#5875](#5875))
([715ccfa](715ccfa))


### Miscellaneous

* **deps:** bump the go-deps group across 1 directory with 25 updates
([#5890](#5890))
([3c4ddd8](3c4ddd8))
* **jobsdb:** migrate incomplete datasets in pairs excluding completed
ones ([#5876](#5876))
([9d8b573](9d8b573))
* **jobsdb:** remove support for jobDoneMigrateThres
([#5873](#5873))
([4745d2b](4745d2b))
* make reporting client params configurable
([#5891](#5891))
([39f7e75](39f7e75))
* move house keeping action to commit sha
([#5885](#5885))
([d379e82](d379e82))
* refactor config change dtype for config to json.RawMessage
([#5874](#5874))
([940adb0](940adb0))
* refactor webhook pkg to make it reusable in other repos
([#5594](#5594))
([1ee57bc](1ee57bc))
* remove redundant structure for account with definition
([#5872](#5872))
([89bc62b](89bc62b))
* remove support for transformer-based kafka batching
([#5870](#5870))
([60a571c](60a571c))
* revert support new consent resolution strategy values
([#5855](#5855))
([c7e3b37](c7e3b37))
* revert use account to decide oauth type of a destination
([#5861](#5861))
([6232755](6232755))
* **router:** additional observability metrics
([#5887](#5887))
([da3ba3c](da3ba3c))
* sync release v1.49.0 to main branch
([#5851](#5851))
([78cc5a9](78cc5a9))
* use jsonrs from rudder-go-kit
([#5869](#5869))
([39064af](39064af))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
atzoum pushed a commit that referenced this pull request May 26, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.50.0-rc.2](v1.49.0...v1.50.0-rc.2)
(2025-05-26)


### Features

* implement aws v2 implementation
([#5852](#5852))
([18fd4fb](18fd4fb))


### Bug Fixes

* do not panic for invalid router payload
([#5880](#5880))
([f4a0e7c](f4a0e7c))
* error gathering metrics, metric was collected before with the same
name and label values
([#5900](#5900))
([422f45b](422f45b))
* handle missing eventUpload config gracefully
([#5898](#5898))
([05a926d](05a926d))
* server panicking with invalid byte sequence for encoding utf8 0x00
([#5882](#5882))
([4c1a0de](4c1a0de))
* skip source_id when fetching warehouse schema from database
([#5886](#5886))
([092ee2a](092ee2a))
* warehouse pending tables to skip
([#5867](#5867))
([864fd49](864fd49))
* warehouse transformations
([#5875](#5875))
([715ccfa](715ccfa))


### Miscellaneous

* **deps:** bump the go-deps group across 1 directory with 25 updates
([#5890](#5890))
([3c4ddd8](3c4ddd8))
* **jobsdb:** migrate incomplete datasets in pairs excluding completed
ones ([#5876](#5876))
([9d8b573](9d8b573))
* **jobsdb:** remove support for jobDoneMigrateThres
([#5873](#5873))
([4745d2b](4745d2b))
* make reporting client params configurable
([#5891](#5891))
([39f7e75](39f7e75))
* move house keeping action to commit sha
([#5885](#5885))
([d379e82](d379e82))
* refactor config change dtype for config to json.RawMessage
([#5874](#5874))
([940adb0](940adb0))
* refactor webhook pkg to make it reusable in other repos
([#5594](#5594))
([1ee57bc](1ee57bc))
* remove redundant structure for account with definition
([#5872](#5872))
([89bc62b](89bc62b))
* remove support for transformer-based kafka batching
([#5870](#5870))
([60a571c](60a571c))
* revert support new consent resolution strategy values
([#5855](#5855))
([c7e3b37](c7e3b37))
* revert use account to decide oauth type of a destination
([#5861](#5861))
([6232755](6232755))
* **router:** additional observability metrics
([#5887](#5887))
([da3ba3c](da3ba3c))
* sync release v1.49.0 to main branch
([#5851](#5851))
([78cc5a9](78cc5a9))
* use jsonrs from rudder-go-kit
([#5869](#5869))
([39064af](39064af))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
mihir20 pushed a commit that referenced this pull request May 27, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.50.0](v1.49.0...v1.50.0)
(2025-05-26)


### Features

* implement aws v2 implementation
([#5852](#5852))
([18fd4fb](18fd4fb))


### Bug Fixes

* do not panic for invalid router payload
([#5880](#5880))
([f4a0e7c](f4a0e7c))
* error gathering metrics, metric was collected before with the same
name and label values
([#5900](#5900))
([422f45b](422f45b))
* handle missing eventUpload config gracefully
([#5898](#5898))
([05a926d](05a926d))
* server panicking with invalid byte sequence for encoding utf8 0x00
([#5882](#5882))
([4c1a0de](4c1a0de))
* skip source_id when fetching warehouse schema from database
([#5886](#5886))
([092ee2a](092ee2a))
* warehouse pending tables to skip
([#5867](#5867))
([864fd49](864fd49))
* warehouse transformations
([#5875](#5875))
([715ccfa](715ccfa))


### Miscellaneous

* **deps:** bump the go-deps group across 1 directory with 25 updates
([#5890](#5890))
([3c4ddd8](3c4ddd8))
* **jobsdb:** migrate incomplete datasets in pairs excluding completed
ones ([#5876](#5876))
([9d8b573](9d8b573))
* **jobsdb:** remove support for jobDoneMigrateThres
([#5873](#5873))
([4745d2b](4745d2b))
* make reporting client params configurable
([#5891](#5891))
([39f7e75](39f7e75))
* move house keeping action to commit sha
([#5885](#5885))
([d379e82](d379e82))
* refactor config change dtype for config to json.RawMessage
([#5874](#5874))
([940adb0](940adb0))
* refactor webhook pkg to make it reusable in other repos
([#5594](#5594))
([1ee57bc](1ee57bc))
* remove redundant structure for account with definition
([#5872](#5872))
([89bc62b](89bc62b))
* remove support for transformer-based kafka batching
([#5870](#5870))
([60a571c](60a571c))
* revert support new consent resolution strategy values
([#5855](#5855))
([c7e3b37](c7e3b37))
* revert use account to decide oauth type of a destination
([#5861](#5861))
([6232755](6232755))
* **router:** additional observability metrics
([#5887](#5887))
([da3ba3c](da3ba3c))
* sync release v1.49.0 to main branch
([#5851](#5851))
([78cc5a9](78cc5a9))
* use jsonrs from rudder-go-kit
([#5869](#5869))
([39064af](39064af))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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.

4 participants