Skip to content

fix(go): use options for more helpers #3383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 0 additions & 157 deletions clients/algoliasearch-client-go/algolia/utils/options.go

This file was deleted.

56 changes: 0 additions & 56 deletions clients/algoliasearch-client-go/algolia/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"reflect"
"strings"
"time"

"github.com/algolia/algoliasearch-client-go/v4/algolia/errs"
)

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

type IterableError struct {
Validate func(any, error) bool
Message func(any, error) string
}

func CreateIterable[T any](execute func(*T, error) (*T, error), validate func(*T, error) bool, opts ...IterableOption) (*T, error) {
options := Options{
MaxRetries: 50,
Timeout: func(_ int) time.Duration {
return 1 * time.Second
},
}

for _, opt := range opts {
opt.Apply(&options)
}

var executor func(*T, error) (*T, error)

retryCount := 0

executor = func(previousResponse *T, previousError error) (*T, error) {
response, responseErr := execute(previousResponse, previousError)

retryCount++

if options.Aggregator != nil {
options.Aggregator(response, responseErr)
}

if validate(response, responseErr) {
return response, responseErr
}

if retryCount >= options.MaxRetries {
return nil, errs.NewWaitError(fmt.Sprintf("The maximum number of retries exceeded. (%d/%d)", retryCount, options.MaxRetries))
}

if options.IterableError != nil && options.IterableError.Validate(response, responseErr) {
if options.IterableError.Message != nil {
return nil, errs.NewWaitError(options.IterableError.Message(response, responseErr))
}

return nil, errs.NewWaitError("an error occurred")
}

time.Sleep(options.Timeout(retryCount))

return executor(response, responseErr)
}

return executor(nil, nil)
}

// QueryParameterToString convert any query parameters to string.
func QueryParameterToString(obj any) string {
return strings.ReplaceAll(url.QueryEscape(ParameterToString(obj)), "+", "%20")
Expand Down
Loading
Loading