Skip to content

Commit

Permalink
fix go again
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Jul 17, 2024
1 parent 6291847 commit 3b34349
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 311 deletions.
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, nil
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ private Map<String, Object> traverseParams(
boolean isCodegenModel = spec instanceof CodegenModel;

Map<String, Object> testOutput = createDefaultOutput();
boolean isRequired = false;

if (spec instanceof CodegenParameter parameter) {
testOutput.put("required", parameter.required);
isRequired = parameter.required;
} else if (spec instanceof CodegenProperty property) {
testOutput.put("required", property.required);
isRequired = property.required;
}

if (!isCodegenModel) {
Expand All @@ -130,6 +131,8 @@ private Map<String, Object> traverseParams(
testOutput.put("isKeyAllUpperCase", StringUtils.isAllUpperCase(finalParamName));
testOutput.put("parentSuffix", suffix - 1);
testOutput.put("useAnonymousKey", !finalParamName.matches("(.*)_[0-9]$") && suffix != 0);
testOutput.put("required", isRequired);
testOutput.put("goFunctionalParam", !isRequired && suffix == 0);
testOutput.put("suffix", suffix);
testOutput.put("parent", parent);
testOutput.put("isRoot", "".equals(parent));
Expand Down
3 changes: 1 addition & 2 deletions playground/go/personalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

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

func testPersonalization(appID, apiKey string) int {
Expand All @@ -20,7 +19,7 @@ func testPersonalization(appID, apiKey string) int {
// it will fail expectedly because of the very short timeout to showcase the context usage.
deleteUserProfileResponse, err := personalizationClient.DeleteUserProfile(
personalizationClient.NewApiDeleteUserProfileRequest("userToken"),
utils.WithContext(ctx),
personalization.WithContext(ctx),
)
if err != nil {
fmt.Printf("request error with DeleteUserProfile: %v\n", err)
Expand Down
3 changes: 1 addition & 2 deletions playground/go/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

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

func testSearch(appID, apiKey string) int {
Expand All @@ -14,7 +13,7 @@ func testSearch(appID, apiKey string) int {
panic(err)
}

res, err := searchClient.WaitForApiKey(search.API_KEY_OPERATION_ADD, "test", &search.ApiKey{}, utils.WithContext(context.Background()), utils.WithMaxRetries(4))
res, err := searchClient.WaitForApiKey("test", search.API_KEY_OPERATION_ADD, &search.ApiKey{}, search.WithContext(context.Background()), search.WithMaxRetries(4))
print(res)
print(err.Error())

Expand Down
Loading

0 comments on commit 3b34349

Please sign in to comment.