github.com/prebid/prebid-server/v2@v2.18.0/modules/helpers.go (about)

     1  package modules
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/prebid/prebid-server/v2/hooks"
     8  	"github.com/prebid/prebid-server/v2/hooks/hookstage"
     9  )
    10  
    11  var moduleReplacer = strings.NewReplacer(".", "_", "-", "_")
    12  
    13  func createModuleStageNamesCollection(modules map[string]interface{}) (map[string][]string, error) {
    14  	moduleStageNameCollector := make(map[string][]string)
    15  	var added bool
    16  
    17  	for id, hook := range modules {
    18  		if _, ok := hook.(hookstage.Entrypoint); ok {
    19  			added = true
    20  			stageName := hooks.StageEntrypoint.String()
    21  			moduleStageNameCollector = addModuleStageName(moduleStageNameCollector, id, stageName)
    22  		}
    23  
    24  		if _, ok := hook.(hookstage.RawAuctionRequest); ok {
    25  			added = true
    26  			stageName := hooks.StageRawAuctionRequest.String()
    27  			moduleStageNameCollector = addModuleStageName(moduleStageNameCollector, id, stageName)
    28  		}
    29  
    30  		if _, ok := hook.(hookstage.ProcessedAuctionRequest); ok {
    31  			added = true
    32  			stageName := hooks.StageProcessedAuctionRequest.String()
    33  			moduleStageNameCollector = addModuleStageName(moduleStageNameCollector, id, stageName)
    34  		}
    35  
    36  		if _, ok := hook.(hookstage.BidderRequest); ok {
    37  			added = true
    38  			stageName := hooks.StageBidderRequest.String()
    39  			moduleStageNameCollector = addModuleStageName(moduleStageNameCollector, id, stageName)
    40  		}
    41  
    42  		if _, ok := hook.(hookstage.RawBidderResponse); ok {
    43  			added = true
    44  			stageName := hooks.StageRawBidderResponse.String()
    45  			moduleStageNameCollector = addModuleStageName(moduleStageNameCollector, id, stageName)
    46  		}
    47  
    48  		if _, ok := hook.(hookstage.AllProcessedBidResponses); ok {
    49  			added = true
    50  			stageName := hooks.StageAllProcessedBidResponses.String()
    51  			moduleStageNameCollector = addModuleStageName(moduleStageNameCollector, id, stageName)
    52  		}
    53  
    54  		if _, ok := hook.(hookstage.AuctionResponse); ok {
    55  			added = true
    56  			stageName := hooks.StageAuctionResponse.String()
    57  			moduleStageNameCollector = addModuleStageName(moduleStageNameCollector, id, stageName)
    58  		}
    59  
    60  		if !added {
    61  			return nil, fmt.Errorf(`hook "%s" does not implement any supported hook interface`, id)
    62  		}
    63  	}
    64  
    65  	return moduleStageNameCollector, nil
    66  }
    67  
    68  func addModuleStageName(moduleStageNameCollector map[string][]string, id string, stage string) map[string][]string {
    69  	str := moduleReplacer.Replace(id)
    70  	moduleStageNameCollector[str] = append(moduleStageNameCollector[str], stage)
    71  
    72  	return moduleStageNameCollector
    73  }