Skip to content

Commit

Permalink
Update chooseSyncerConfig
Browse files Browse the repository at this point in the history
Update chooseSyncerConfig to implement following adapter alias rules:
- alias and parent bidder can use same syncer key
- non-alias bidder cannot have same syncer key
- aliases of different parents cannot have same syncer key
- aliases of same parent and non-alias bidder have same syncer key
  • Loading branch information
onkarvhanumante committed Sep 6, 2023
1 parent aa619e5 commit eb836cd
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
49 changes: 38 additions & 11 deletions usersync/syncersbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
)

type namedSyncerConfig struct {
name string
cfg config.Syncer
name string
cfg config.Syncer
bidderInfo config.BidderInfo
}

// SyncerBuildError represents an error with building a syncer.
Expand Down Expand Up @@ -42,7 +43,7 @@ func BuildSyncers(hostConfig *config.Configuration, bidderInfos config.BidderInf
if err != nil {
return nil, []error{err}
}
cfgBySyncerKey[syncerCopy.Key] = append(cfgBySyncerKey[syncerCopy.Key], namedSyncerConfig{bidder, syncerCopy})
cfgBySyncerKey[syncerCopy.Key] = append(cfgBySyncerKey[syncerCopy.Key], namedSyncerConfig{bidder, syncerCopy, bidderInfo})
}

// resolve host endpoint
Expand Down Expand Up @@ -100,29 +101,55 @@ func chooseSyncerConfig(biddersSyncerConfig []namedSyncerConfig) (namedSyncerCon
return biddersSyncerConfig[0], nil
}

var bidderNames []string
var bidderNamesWithEndpoints []string
var syncerConfig namedSyncerConfig
var (
bidderNames []string
nonAliasBidderNames []string
aliasBidderNames []string
syncerConfig namedSyncerConfig
parentBidderNameMap = make(map[string]struct{})
)

for _, bidder := range biddersSyncerConfig {
bidderNames = append(bidderNames, bidder.name)
if bidder.cfg.IFrame != nil || bidder.cfg.Redirect != nil {
bidderNamesWithEndpoints = append(bidderNamesWithEndpoints, bidder.name)
if len(bidder.bidderInfo.AliasOf) > 0 {
aliasBidderNames = append(aliasBidderNames, bidder.name)
parentBidderNameMap[bidder.bidderInfo.AliasOf] = struct{}{}
} else {
nonAliasBidderNames = append(nonAliasBidderNames, bidder.name)
}
syncerConfig = bidder
}
}

if len(bidderNamesWithEndpoints) == 0 {
// check if bidders have same syncer key but no endpoints
if len(nonAliasBidderNames)+len(aliasBidderNames) == 0 {
sort.Strings(bidderNames)
bidders := strings.Join(bidderNames, ", ")
return namedSyncerConfig{}, fmt.Errorf("bidders %s share the same syncer key, but none define endpoints (iframe and/or redirect)", bidders)
}

if len(bidderNamesWithEndpoints) > 1 {
sort.Strings(bidderNamesWithEndpoints)
bidders := strings.Join(bidderNamesWithEndpoints, ", ")
// check if non-alias bidders have same syncer key
if len(nonAliasBidderNames) > 1 {
sort.Strings(nonAliasBidderNames)
bidders := strings.Join(nonAliasBidderNames, ", ")
return namedSyncerConfig{}, fmt.Errorf("bidders %s define endpoints (iframe and/or redirect) for the same syncer key, but only one bidder is permitted to define endpoints", bidders)
}

// check if alias bidders of different parent have same syncer key
if len(parentBidderNameMap) > 1 {
sort.Strings(aliasBidderNames)
return namedSyncerConfig{}, fmt.Errorf("alias bidders %s of different parents defines endpoints (iframe and/or redirect) for the same syncer key, but only one bidder is permitted to define endpoints", strings.Join(aliasBidderNames, ", "))
}

// check if aliases of same parent and non-alias bidder have same syncer key
if len(parentBidderNameMap) != 0 {
if _, ok := parentBidderNameMap[nonAliasBidderNames[0]]; !ok {
sort.Strings(aliasBidderNames)
return namedSyncerConfig{}, fmt.Errorf("alias bidders %s and non-alias bidder %s defines endpoints (iframe and/or redirect) for the same syncer key, but only one bidder is permitted to define endpoints", strings.Join(aliasBidderNames, ", "), nonAliasBidderNames[0])
}
}

return syncerConfig, nil
}

Expand Down
27 changes: 26 additions & 1 deletion usersync/syncersbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ func TestChooseSyncerConfig(t *testing.T) {
bidderAEmpty = namedSyncerConfig{name: "bidderA", cfg: config.Syncer{}}
bidderBPopulated = namedSyncerConfig{name: "bidderB", cfg: config.Syncer{Key: "a", IFrame: &config.SyncerEndpoint{URL: "anyURL"}}}
bidderBEmpty = namedSyncerConfig{name: "bidderB", cfg: config.Syncer{}}
syncerCfg = config.Syncer{Key: "key", Redirect: &config.SyncerEndpoint{RedirectURL: "redirect-url"}}
parent1 = namedSyncerConfig{name: "parent-1", cfg: syncerCfg, bidderInfo: config.BidderInfo{AliasOf: ""}}
parent2 = namedSyncerConfig{name: "parent-2", cfg: syncerCfg, bidderInfo: config.BidderInfo{AliasOf: ""}}
alias1 = namedSyncerConfig{name: "alias-1", cfg: syncerCfg, bidderInfo: config.BidderInfo{AliasOf: "parent-1"}}
alias2 = namedSyncerConfig{name: "alias-2", cfg: syncerCfg, bidderInfo: config.BidderInfo{AliasOf: "parent-2"}}
alias3 = namedSyncerConfig{name: "alias-3", cfg: syncerCfg, bidderInfo: config.BidderInfo{AliasOf: "parent-2"}}
)

testCases := []struct {
Expand Down Expand Up @@ -316,11 +322,30 @@ func TestChooseSyncerConfig(t *testing.T) {
given: []namedSyncerConfig{bidderAEmpty, bidderBPopulated},
expectedConfig: bidderBPopulated,
},
{
description: "alias-can-have-same-key-as-parent",
given: []namedSyncerConfig{parent1, alias1},
expectedConfig: alias1,
},
{
description: "alias-of-differnt-parent-cannot-have-same-key",
given: []namedSyncerConfig{alias1, alias2},
expectedError: "alias bidders alias-1, alias-2 of different parents defines endpoints (iframe and/or redirect) for the same syncer key, but only one bidder is permitted to define endpoints",
},
{
description: "non-alias-bidders-cannot-have-same-key",
given: []namedSyncerConfig{parent1, parent2},
expectedError: "bidders parent-1, parent-2 define endpoints (iframe and/or redirect) for the same syncer key, but only one bidder is permitted to define endpoints",
},
{
description: "non-alias-and-aliases-of-same-parent-cannot-have-same-key",
given: []namedSyncerConfig{parent1, alias2, alias3},
expectedError: "alias bidders alias-2, alias-3 and non-alias bidder parent-1 defines endpoints (iframe and/or redirect) for the same syncer key, but only one bidder is permitted to define endpoints",
},
}

for _, test := range testCases {
result, err := chooseSyncerConfig(test.given)

if test.expectedError == "" {
assert.NoError(t, err, test.description+":err")
assert.Equal(t, test.expectedConfig, result, test.description+":result")
Expand Down

0 comments on commit eb836cd

Please sign in to comment.