Skip to content

Commit 7d2b420

Browse files
authored
fix(go): use options for more helpers (#3383)
1 parent 63a513e commit 7d2b420

File tree

12 files changed

+484
-383
lines changed

12 files changed

+484
-383
lines changed

clients/algoliasearch-client-go/algolia/utils/options.go

Lines changed: 0 additions & 157 deletions
This file was deleted.

clients/algoliasearch-client-go/algolia/utils/utils.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"reflect"
99
"strings"
1010
"time"
11-
12-
"github.com/algolia/algoliasearch-client-go/v4/algolia/errs"
1311
)
1412

1513
// ToPtr is a helper routine that returns a pointer to the given value.
@@ -68,60 +66,6 @@ func IsNilOrEmpty(i any) bool {
6866
}
6967
}
7068

71-
type IterableError struct {
72-
Validate func(any, error) bool
73-
Message func(any, error) string
74-
}
75-
76-
func CreateIterable[T any](execute func(*T, error) (*T, error), validate func(*T, error) bool, opts ...IterableOption) (*T, error) {
77-
options := Options{
78-
MaxRetries: 50,
79-
Timeout: func(_ int) time.Duration {
80-
return 1 * time.Second
81-
},
82-
}
83-
84-
for _, opt := range opts {
85-
opt.Apply(&options)
86-
}
87-
88-
var executor func(*T, error) (*T, error)
89-
90-
retryCount := 0
91-
92-
executor = func(previousResponse *T, previousError error) (*T, error) {
93-
response, responseErr := execute(previousResponse, previousError)
94-
95-
retryCount++
96-
97-
if options.Aggregator != nil {
98-
options.Aggregator(response, responseErr)
99-
}
100-
101-
if validate(response, responseErr) {
102-
return response, responseErr
103-
}
104-
105-
if retryCount >= options.MaxRetries {
106-
return nil, errs.NewWaitError(fmt.Sprintf("The maximum number of retries exceeded. (%d/%d)", retryCount, options.MaxRetries))
107-
}
108-
109-
if options.IterableError != nil && options.IterableError.Validate(response, responseErr) {
110-
if options.IterableError.Message != nil {
111-
return nil, errs.NewWaitError(options.IterableError.Message(response, responseErr))
112-
}
113-
114-
return nil, errs.NewWaitError("an error occurred")
115-
}
116-
117-
time.Sleep(options.Timeout(retryCount))
118-
119-
return executor(response, responseErr)
120-
}
121-
122-
return executor(nil, nil)
123-
}
124-
12569
// QueryParameterToString convert any query parameters to string.
12670
func QueryParameterToString(obj any) string {
12771
return strings.ReplaceAll(url.QueryEscape(ParameterToString(obj)), "+", "%20")

0 commit comments

Comments
 (0)