Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter alias - syncer changes #3082

Merged
merged 19 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions usersync/syncersbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ func BuildSyncers(hostConfig *config.Configuration, bidderInfos config.BidderInf
cfgBySyncerKey := make(map[string][]namedSyncerConfig, len(bidderInfos))
for bidder, bidderInfo := range biddersWithSyncerCfg {
syncerCopy := *bidderInfo.Syncer
if syncerCopy.Key == "" {
syncerCopy.Key = bidder
var err error
syncerCopy.Key, err = getSyncerKey(biddersWithSyncerCfg, bidder, bidderInfo)
if err != nil {
return nil, []error{err}
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved
}
cfgBySyncerKey[syncerCopy.Key] = append(cfgBySyncerKey[syncerCopy.Key], namedSyncerConfig{bidder, syncerCopy})
}
Expand Down Expand Up @@ -123,3 +125,37 @@ func chooseSyncerConfig(biddersSyncerConfig []namedSyncerConfig) (namedSyncerCon

return syncerConfig, nil
}

func getSyncerKey(biddersWithSyncerCfg map[string]config.BidderInfo, bidderName string, bidderInfo config.BidderInfo) (string, error) {
if bidderInfo.Syncer == nil {
return "", fmt.Errorf("found no syncer config for alias bidder %s", bidderName)
}
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved

var (
bidderSyncerCfg = bidderInfo.Syncer
isAlias = len(bidderInfo.AliasOf) > 0
hasInheritedSyncerCfg = false
parentBidderInfo config.BidderInfo
parentBidderName string
)

if isAlias {
parentBidderName = bidderInfo.AliasOf
var parentHasSyncerCfg bool
parentBidderInfo, parentHasSyncerCfg = biddersWithSyncerCfg[parentBidderName]
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved
hasInheritedSyncerCfg = parentHasSyncerCfg && parentBidderInfo.Syncer == bidderSyncerCfg
}

if bidderSyncerCfg.Key == "" {
if hasInheritedSyncerCfg {
return parentBidderName, nil
}
return bidderName, nil
}

if isAlias && !hasInheritedSyncerCfg && bidderSyncerCfg.Key == parentBidderInfo.Syncer.Key {
return "", fmt.Errorf("syncer key of alias bidder %s is same as the syncer key for its parent bidder %s", bidderName, parentBidderName)
}
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved

return bidderSyncerCfg.Key, nil
}
85 changes: 85 additions & 0 deletions usersync/syncersbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,88 @@ func TestChooseSyncerConfig(t *testing.T) {
}
}
}

func TestGetSyncerKey(t *testing.T) {
redirectCfg := config.SyncerEndpoint{URL: "redirect-url", UserMacro: "$UID"}
syncer1 := config.Syncer{Key: "", Redirect: &redirectCfg}
syncer2 := syncer1
syncer3 := config.Syncer{Key: "key-1", Redirect: &redirectCfg}
syncer4 := config.Syncer{Key: "key-1"}

biddersWithSyncerCfg := config.BidderInfos{
"parent-1": config.BidderInfo{Syncer: &syncer1},
"alias-1": config.BidderInfo{AliasOf: "parent-1", Syncer: &syncer1},
"alias-2": config.BidderInfo{AliasOf: "parent-1", Syncer: &syncer2},
"parent-2": config.BidderInfo{Syncer: &syncer3},
"alias-3": config.BidderInfo{AliasOf: "parent-1", Syncer: &syncer3},
"alias-4": config.BidderInfo{AliasOf: "parent-2", Syncer: &syncer4},
}

tests := []struct {
name string
biddersWithSyncerCfg map[string]config.BidderInfo
bidderName string
bidderInfo config.BidderInfo
expectedErr string
expectedSyncerKey string
}{
{
name: "alias_bidder_has_no_syncer_config",
bidderName: "alias-1",
bidderInfo: config.BidderInfo{Syncer: nil},
expectedErr: "found no syncer config for alias bidder alias-1",
},
onkarvhanumante marked this conversation as resolved.
Show resolved Hide resolved
{
name: "use_parent_name_as_syncer_key_when_syncer_key_is_empty_and_alias_inherits_parent_syncer_config",
biddersWithSyncerCfg: biddersWithSyncerCfg,
bidderName: "alias-1",
bidderInfo: biddersWithSyncerCfg["alias-1"],
expectedSyncerKey: "parent-1",
},
{
name: "use_alias_name_as_syncer_key_when_syncer_key_is_empty_and_alias_does_not_inherit_parent_syncer_config",
biddersWithSyncerCfg: biddersWithSyncerCfg,
bidderName: "alias-2",
bidderInfo: biddersWithSyncerCfg["alias-2"],
expectedSyncerKey: "alias-2",
},
{
name: "use_syncer_key_when_parent_and_alias_have_different_syncer_config",
biddersWithSyncerCfg: biddersWithSyncerCfg,
bidderName: "alias-3",
bidderInfo: biddersWithSyncerCfg["alias-3"],
expectedSyncerKey: biddersWithSyncerCfg["alias-3"].Syncer.Key,
},
{
name: "use_syncer_key_when_parent_and_alias_have_different_syncer_config",
biddersWithSyncerCfg: biddersWithSyncerCfg,
bidderName: "alias-4",
bidderInfo: biddersWithSyncerCfg["alias-4"],
expectedErr: "syncer key of alias bidder alias-4 is same as the syncer key for its parent bidder parent-2",
},
{
name: "use_bidder_name_when_non-alias_bidder_has_no_syncer_key",
bidderName: "parent-1",
bidderInfo: biddersWithSyncerCfg["parent-1"],
expectedSyncerKey: "parent-1",
},
{
name: "use_syncer_key_when_non-alias_bidder_has_defined_syncer_key",
bidderName: "parent-2",
bidderInfo: biddersWithSyncerCfg["parent-2"],
expectedSyncerKey: biddersWithSyncerCfg["parent-2"].Syncer.Key,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
syncerKey, err := getSyncerKey(test.biddersWithSyncerCfg, test.bidderName, test.bidderInfo)
if test.expectedErr != "" {
assert.Equal(t, test.expectedErr, err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, test.expectedSyncerKey, syncerKey)
}
})
}
}