@@ -33,53 +33,69 @@ const (
3333 validatorsByGraffitiHex searchTypeKey = "validators_by_graffiti_hex"
3434)
3535
36- // source of truth for all possible search types and their regex
37- var searchTypeMap = map [searchTypeKey ]searchType {
38- validatorByIndex : {
39- regex : types .ReInteger ,
40- responseType : "validator" ,
41- },
42- validatorByPublicKey : {
43- regex : types .ReValidatorPublicKey ,
44- responseType : "validator" ,
45- },
46- validatorList : {
47- regex : types .ReValidatorList ,
48- responseType : string (validatorList ),
49- },
50- validatorsByDepositAddress : {
51- regex : types .ReEthereumAddress ,
52- responseType : string (validatorsByDepositAddress ),
53- },
54- validatorsByDepositEnsName : {
55- regex : types .ReEnsName ,
56- responseType : string (validatorsByDepositAddress ),
57- },
58- validatorsByWithdrawalCredential : {
59- regex : types .ReWithdrawalCredential ,
60- responseType : string (validatorsByWithdrawalCredential ),
61- },
62- validatorsByWithdrawalAddress : {
63- regex : types .ReEthereumAddress ,
64- responseType : string (validatorsByWithdrawalCredential ),
65- },
66- validatorsByWithdrawalEns : {
67- regex : types .ReEnsName ,
68- responseType : string (validatorsByWithdrawalCredential ),
69- },
70- validatorsByGraffiti : {
71- regex : types .ReGraffiti ,
72- responseType : string (validatorsByGraffiti ),
73- },
74- validatorsByGraffitiHex : {
75- regex : types .ReGraffitiHex ,
76- responseType : string (validatorsByGraffiti ),
77- },
78- }
79-
8036type searchType struct {
8137 regex * regexp.Regexp
8238 responseType string
39+ handlerFunc func (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error )
40+ }
41+
42+ var searchTypeMap map [searchTypeKey ]searchType
43+
44+ // using init to avoid initialization cycles, since handler functions may reference the map
45+ func init () {
46+ // source of truth for all possible search types and their regex
47+ searchTypeMap = map [searchTypeKey ]searchType {
48+ validatorByIndex : {
49+ regex : types .ReInteger ,
50+ responseType : "validator" ,
51+ handlerFunc : handleSearchValidatorByIndex ,
52+ },
53+ validatorByPublicKey : {
54+ regex : types .ReValidatorPublicKey ,
55+ responseType : "validator" ,
56+ handlerFunc : handleSearchValidatorByPublicKey ,
57+ },
58+ validatorList : {
59+ regex : types .ReValidatorList ,
60+ responseType : string (validatorList ),
61+ handlerFunc : handleSearchValidatorList ,
62+ },
63+ validatorsByDepositAddress : {
64+ regex : types .ReEthereumAddress ,
65+ responseType : string (validatorsByDepositAddress ),
66+ handlerFunc : handleSearchValidatorsByDepositAddress ,
67+ },
68+ validatorsByDepositEnsName : {
69+ regex : types .ReEnsName ,
70+ responseType : string (validatorsByDepositAddress ),
71+ handlerFunc : handleSearchValidatorsByDepositEnsName ,
72+ },
73+ validatorsByWithdrawalCredential : {
74+ regex : types .ReWithdrawalCredential ,
75+ responseType : string (validatorsByWithdrawalCredential ),
76+ handlerFunc : handleSearchValidatorsByWithdrawalCredential ,
77+ },
78+ validatorsByWithdrawalAddress : {
79+ regex : types .ReEthereumAddress ,
80+ responseType : string (validatorsByWithdrawalCredential ),
81+ handlerFunc : handleSearchValidatorsByWithdrawalAddress ,
82+ },
83+ validatorsByWithdrawalEns : {
84+ regex : types .ReEnsName ,
85+ responseType : string (validatorsByWithdrawalCredential ),
86+ handlerFunc : handleSearchValidatorsByWithdrawalEnsName ,
87+ },
88+ validatorsByGraffiti : {
89+ regex : types .ReGraffiti ,
90+ responseType : string (validatorsByGraffiti ),
91+ handlerFunc : handleSearchValidatorsByGraffiti ,
92+ },
93+ validatorsByGraffitiHex : {
94+ regex : types .ReGraffitiHex ,
95+ responseType : string (validatorsByGraffiti ),
96+ handlerFunc : handleSearchValidatorsByGraffitiHex ,
97+ },
98+ }
8399}
84100
85101// --------------------------------------
@@ -118,7 +134,7 @@ func (h *HandlerService) InternalPostSearch(w http.ResponseWriter, r *http.Reque
118134 chainId := chainId
119135 searchType := searchType
120136 g .Go (func () error {
121- searchResult , err := h . handleSearchType (ctx , req .Input , searchType , chainId )
137+ searchResult , err := searchTypeMap [ searchType ]. handlerFunc (ctx , h , req .Input , chainId )
122138 if err != nil {
123139 if errors .Is (err , dataaccess .ErrNotFound ) {
124140 return nil
@@ -158,33 +174,6 @@ func (h *HandlerService) InternalPostSearch(w http.ResponseWriter, r *http.Reque
158174// --------------------------------------
159175// Search Helper Functions
160176
161- func (h * HandlerService ) handleSearchType (ctx context.Context , input string , searchType searchTypeKey , chainId uint64 ) (* types.SearchResult , error ) {
162- switch searchType {
163- case validatorByIndex :
164- return h .handleSearchValidatorByIndex (ctx , input , chainId )
165- case validatorByPublicKey :
166- return h .handleSearchValidatorByPublicKey (ctx , input , chainId )
167- case validatorList :
168- return h .handleSearchValidatorList (ctx , input , chainId )
169- case validatorsByDepositAddress :
170- return h .handleSearchValidatorsByDepositAddress (ctx , input , chainId )
171- case validatorsByDepositEnsName :
172- return h .handleSearchValidatorsByDepositEnsName (ctx , input , chainId )
173- case validatorsByWithdrawalCredential :
174- return h .handleSearchValidatorsByWithdrawalCredential (ctx , input , chainId )
175- case validatorsByWithdrawalAddress :
176- return h .handleSearchValidatorsByWithdrawalAddress (ctx , input , chainId )
177- case validatorsByWithdrawalEns :
178- return h .handleSearchValidatorsByWithdrawalEnsName (ctx , input , chainId )
179- case validatorsByGraffiti :
180- return h .handleSearchValidatorsByGraffiti (ctx , input , chainId )
181- case validatorsByGraffitiHex :
182- return h .handleSearchValidatorsByGraffitiHex (ctx , input , chainId )
183- default :
184- return nil , errors .New ("invalid search type" )
185- }
186- }
187-
188177func asSearchResult [In any ](searchType searchTypeKey , chainId uint64 , result * In , err error ) (* types.SearchResult , error ) {
189178 if err != nil || result == nil {
190179 return nil , err
@@ -196,7 +185,7 @@ func asSearchResult[In any](searchType searchTypeKey, chainId uint64, result *In
196185 }, nil
197186}
198187
199- func ( h * HandlerService ) handleSearchValidatorByIndex (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
188+ func handleSearchValidatorByIndex (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
200189 index , err := strconv .ParseUint (input , 10 , 64 )
201190 if err != nil {
202191 // input should've been checked by the regex before, this should never happen
@@ -206,7 +195,7 @@ func (h *HandlerService) handleSearchValidatorByIndex(ctx context.Context, input
206195 return asSearchResult (validatorByIndex , chainId , result , err )
207196}
208197
209- func ( h * HandlerService ) handleSearchValidatorByPublicKey (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
198+ func handleSearchValidatorByPublicKey (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
210199 publicKey , err := hex .DecodeString (strings .TrimPrefix (input , "0x" ))
211200 if err != nil {
212201 // input should've been checked by the regex before, this should never happen
@@ -216,7 +205,7 @@ func (h *HandlerService) handleSearchValidatorByPublicKey(ctx context.Context, i
216205 return asSearchResult (validatorByPublicKey , chainId , result , err )
217206}
218207
219- func ( h * HandlerService ) handleSearchValidatorList (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
208+ func handleSearchValidatorList (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
220209 var v validationError
221210 // split the input string into a slice of strings
222211 indices , pubkeys := v .checkValidatorList (input , forbidEmpty )
@@ -235,7 +224,7 @@ func (h *HandlerService) handleSearchValidatorList(ctx context.Context, input st
235224 }, nil
236225}
237226
238- func ( h * HandlerService ) handleSearchValidatorsByDepositAddress (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
227+ func handleSearchValidatorsByDepositAddress (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
239228 address , err := hex .DecodeString (strings .TrimPrefix (input , "0x" ))
240229 if err != nil {
241230 return nil , err
@@ -244,12 +233,12 @@ func (h *HandlerService) handleSearchValidatorsByDepositAddress(ctx context.Cont
244233 return asSearchResult (validatorsByDepositAddress , chainId , result , err )
245234}
246235
247- func ( h * HandlerService ) handleSearchValidatorsByDepositEnsName (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
236+ func handleSearchValidatorsByDepositEnsName (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
248237 result , err := h .daService .GetSearchValidatorsByDepositEnsName (ctx , chainId , input )
249238 return asSearchResult (validatorsByDepositEnsName , chainId , result , err )
250239}
251240
252- func ( h * HandlerService ) handleSearchValidatorsByWithdrawalCredential (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
241+ func handleSearchValidatorsByWithdrawalCredential (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
253242 withdrawalCredential , err := hex .DecodeString (strings .TrimPrefix (input , "0x" ))
254243 if err != nil {
255244 return nil , err
@@ -258,7 +247,7 @@ func (h *HandlerService) handleSearchValidatorsByWithdrawalCredential(ctx contex
258247 return asSearchResult (validatorsByWithdrawalCredential , chainId , result , err )
259248}
260249
261- func ( h * HandlerService ) handleSearchValidatorsByWithdrawalAddress (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
250+ func handleSearchValidatorsByWithdrawalAddress (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
262251 withdrawalString := "010000000000000000000000" + strings .TrimPrefix (input , "0x" )
263252 withdrawalCredential , err := hex .DecodeString (withdrawalString )
264253 if err != nil {
@@ -268,12 +257,12 @@ func (h *HandlerService) handleSearchValidatorsByWithdrawalAddress(ctx context.C
268257 return asSearchResult (validatorsByWithdrawalAddress , chainId , result , err )
269258}
270259
271- func ( h * HandlerService ) handleSearchValidatorsByWithdrawalEnsName (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
260+ func handleSearchValidatorsByWithdrawalEnsName (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
272261 result , err := h .daService .GetSearchValidatorsByWithdrawalEnsName (ctx , chainId , input )
273262 return asSearchResult (validatorsByWithdrawalEns , chainId , result , err )
274263}
275264
276- func ( h * HandlerService ) handleSearchValidatorsByGraffiti (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
265+ func handleSearchValidatorsByGraffiti (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
277266 // regex could only verify max character length, validate max byte length here
278267 if len (input ) > 32 {
279268 return nil , nil // return no error as to not disturb the other search types
@@ -282,7 +271,7 @@ func (h *HandlerService) handleSearchValidatorsByGraffiti(ctx context.Context, i
282271 return asSearchResult (validatorsByGraffiti , chainId , result , err )
283272}
284273
285- func ( h * HandlerService ) handleSearchValidatorsByGraffitiHex (ctx context.Context , input string , chainId uint64 ) (* types.SearchResult , error ) {
274+ func handleSearchValidatorsByGraffitiHex (ctx context.Context , h * HandlerService , input string , chainId uint64 ) (* types.SearchResult , error ) {
286275 graffitiHex , err := hex .DecodeString (strings .TrimPrefix (input , "0x" ))
287276 if err != nil {
288277 return nil , err
0 commit comments