forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.go
159 lines (131 loc) · 4.84 KB
/
repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package hooks
import (
"fmt"
"github.com/prebid/prebid-server/hooks/hookstage"
)
// HookRepository is the interface that exposes methods
// that return instance of the certain hook interface.
//
// Each method accepts hook ID and returns hook interface
// registered under this ID and true if hook found
// otherwise nil value returned with the false,
// indicating not found hook for this ID.
type HookRepository interface {
GetEntrypointHook(id string) (hookstage.Entrypoint, bool)
GetRawAuctionHook(id string) (hookstage.RawAuctionRequest, bool)
GetProcessedAuctionHook(id string) (hookstage.ProcessedAuctionRequest, bool)
GetBidderRequestHook(id string) (hookstage.BidderRequest, bool)
GetRawBidderResponseHook(id string) (hookstage.RawBidderResponse, bool)
GetAllProcessedBidResponsesHook(id string) (hookstage.AllProcessedBidResponses, bool)
GetAuctionResponseHook(id string) (hookstage.AuctionResponse, bool)
}
// NewHookRepository returns a new instance of the HookRepository interface.
//
// The hooks argument represents a mapping of hook IDs to types
// implementing at least one of the available hook interfaces, see [hookstage] pkg.
//
// Error returned if provided interface doesn't implement any hook interface
// or hook with same ID already exists.
func NewHookRepository(hooks map[string]interface{}) (HookRepository, error) {
repo := new(hookRepository)
for id, hook := range hooks {
if err := repo.add(id, hook); err != nil {
return nil, err
}
}
return repo, nil
}
type hookRepository struct {
entrypointHooks map[string]hookstage.Entrypoint
rawAuctionHooks map[string]hookstage.RawAuctionRequest
processedAuctionHooks map[string]hookstage.ProcessedAuctionRequest
bidderRequestHooks map[string]hookstage.BidderRequest
rawBidderResponseHooks map[string]hookstage.RawBidderResponse
allProcessedBidResponseHooks map[string]hookstage.AllProcessedBidResponses
auctionResponseHooks map[string]hookstage.AuctionResponse
}
func (r *hookRepository) GetEntrypointHook(id string) (hookstage.Entrypoint, bool) {
return getHook(r.entrypointHooks, id)
}
func (r *hookRepository) GetRawAuctionHook(id string) (hookstage.RawAuctionRequest, bool) {
return getHook(r.rawAuctionHooks, id)
}
func (r *hookRepository) GetProcessedAuctionHook(id string) (hookstage.ProcessedAuctionRequest, bool) {
return getHook(r.processedAuctionHooks, id)
}
func (r *hookRepository) GetBidderRequestHook(id string) (hookstage.BidderRequest, bool) {
return getHook(r.bidderRequestHooks, id)
}
func (r *hookRepository) GetRawBidderResponseHook(id string) (hookstage.RawBidderResponse, bool) {
return getHook(r.rawBidderResponseHooks, id)
}
func (r *hookRepository) GetAllProcessedBidResponsesHook(id string) (hookstage.AllProcessedBidResponses, bool) {
return getHook(r.allProcessedBidResponseHooks, id)
}
func (r *hookRepository) GetAuctionResponseHook(id string) (hookstage.AuctionResponse, bool) {
return getHook(r.auctionResponseHooks, id)
}
func (r *hookRepository) add(id string, hook interface{}) error {
var hasAnyHooks bool
var err error
if h, ok := hook.(hookstage.Entrypoint); ok {
hasAnyHooks = true
if r.entrypointHooks, err = addHook(r.entrypointHooks, h, id); err != nil {
return err
}
}
if h, ok := hook.(hookstage.RawAuctionRequest); ok {
hasAnyHooks = true
if r.rawAuctionHooks, err = addHook(r.rawAuctionHooks, h, id); err != nil {
return err
}
}
if h, ok := hook.(hookstage.ProcessedAuctionRequest); ok {
hasAnyHooks = true
if r.processedAuctionHooks, err = addHook(r.processedAuctionHooks, h, id); err != nil {
return err
}
}
if h, ok := hook.(hookstage.BidderRequest); ok {
hasAnyHooks = true
if r.bidderRequestHooks, err = addHook(r.bidderRequestHooks, h, id); err != nil {
return err
}
}
if h, ok := hook.(hookstage.RawBidderResponse); ok {
hasAnyHooks = true
if r.rawBidderResponseHooks, err = addHook(r.rawBidderResponseHooks, h, id); err != nil {
return err
}
}
if h, ok := hook.(hookstage.AllProcessedBidResponses); ok {
hasAnyHooks = true
if r.allProcessedBidResponseHooks, err = addHook(r.allProcessedBidResponseHooks, h, id); err != nil {
return err
}
}
if h, ok := hook.(hookstage.AuctionResponse); ok {
hasAnyHooks = true
if r.auctionResponseHooks, err = addHook(r.auctionResponseHooks, h, id); err != nil {
return err
}
}
if !hasAnyHooks {
return fmt.Errorf(`hook "%s" does not implement any supported hook interface`, id)
}
return nil
}
func getHook[T any](hooks map[string]T, id string) (T, bool) {
hook, ok := hooks[id]
return hook, ok
}
func addHook[T any](hooks map[string]T, hook T, id string) (map[string]T, error) {
if hooks == nil {
hooks = make(map[string]T)
}
if _, ok := hooks[id]; ok {
return nil, fmt.Errorf(`hook of type "%T" with id "%s" already registered`, new(T), id)
}
hooks[id] = hook
return hooks, nil
}