From ec582c4d1221e9139d0f2da85761a6b11de9c541 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Mon, 6 Jan 2025 16:28:16 +0100 Subject: [PATCH] go is baaaaaad --- clients/algoliasearch-client-go/.golangci.yml | 1 + templates/go/api.mustache | 66 ++++++++++++++++++- templates/go/search_helpers.mustache | 32 +++++---- 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/clients/algoliasearch-client-go/.golangci.yml b/clients/algoliasearch-client-go/.golangci.yml index 26b09fa902..6a638b3644 100644 --- a/clients/algoliasearch-client-go/.golangci.yml +++ b/clients/algoliasearch-client-go/.golangci.yml @@ -61,6 +61,7 @@ linters: # Deprecated - execinquery + - exportloopref issues: exclude-generated: disable diff --git a/templates/go/api.mustache b/templates/go/api.mustache index 0804ec8718..cabfc6bbf2 100644 --- a/templates/go/api.mustache +++ b/templates/go/api.mustache @@ -39,6 +39,9 @@ type config struct { // -- Partial update options createIfNotExists bool + // -- ReplaceAllObjects options + scopes []ScopeType + // -- Iterable options maxRetries int timeout func(int) time.Duration @@ -113,7 +116,7 @@ func WithBatchSize(batchSize int) chunkedBatchOption { }) } -// --------- ChunkedBatch options --------- +// --------- PartialUpdateObjects options --------- type PartialUpdateObjectsOption interface { ChunkedBatchOption @@ -146,6 +149,39 @@ func WithCreateIfNotExists(createIfNotExists bool) partialUpdateObjectsOption { }) } +// --------- ReplaceAllObjects options --------- + +type ReplaceAllObjectsOption interface { + ChunkedBatchOption + replaceAllObjects() +} + +type replaceAllObjectsOption func(*config) + +var ( + _ ReplaceAllObjectsOption = (*replaceAllObjectsOption)(nil) + _ ReplaceAllObjectsOption = (*chunkedBatchOption)(nil) + _ ReplaceAllObjectsOption = (*requestOption)(nil) +) + +func (p replaceAllObjectsOption) apply(c *config) { + p(c) +} + +func (p replaceAllObjectsOption) replaceAllObjects() {} + +func (p replaceAllObjectsOption) chunkedBatch() {} + +func (c chunkedBatchOption) replaceAllObjects() {} + +func (r requestOption) replaceAllObjects() {} + +func WithScopes(scopes []ScopeType) replaceAllObjectsOption { + return replaceAllObjectsOption(func(c *config) { + c.scopes = scopes + }) +} + // --------- Iterable options ---------. type IterableOption interface { @@ -243,7 +279,7 @@ func toIterableOptions(opts []ChunkedBatchOption) []IterableOption { return iterableOpts } -func toIterableOptionsWaitFor(opts []WaitForApiKeyOption) []IterableOption { +func waitForApiKeyToIterableOptions(opts []WaitForApiKeyOption) []IterableOption { iterableOpts := make([]IterableOption, 0, len(opts)) for _, opt := range opts { @@ -255,7 +291,19 @@ func toIterableOptionsWaitFor(opts []WaitForApiKeyOption) []IterableOption { return iterableOpts } -func toChunkedBatchOptions(opts []PartialUpdateObjectsOption) []ChunkedBatchOption { +func replaceAllObjectsToIterableOptions(opts []ReplaceAllObjectsOption) []IterableOption { + iterableOpts := make([]IterableOption, 0, len(opts)) + + for _, opt := range opts { + if opt, ok := opt.(IterableOption); ok { + iterableOpts = append(iterableOpts, opt) + } + } + + return iterableOpts +} + +func partialUpdateObjectsToChunkedBatchOptions(opts []PartialUpdateObjectsOption) []ChunkedBatchOption { chunkedBatchOpts := make([]ChunkedBatchOption, 0, len(opts)) for _, opt := range opts { @@ -266,6 +314,18 @@ func toChunkedBatchOptions(opts []PartialUpdateObjectsOption) []ChunkedBatchOpti return chunkedBatchOpts } + +func replaceAllObjectsToChunkBactchOptions(opts []ReplaceAllObjectsOption) []ChunkedBatchOption { + chunkedBatchOpts := make([]ChunkedBatchOption, 0, len(opts)) + + for _, opt := range opts { + if opt, ok := opt.(ChunkedBatchOption); ok { + chunkedBatchOpts = append(chunkedBatchOpts, opt) + } + } + + return chunkedBatchOpts +} {{/isSearchClient}} {{#operation}} diff --git a/templates/go/search_helpers.mustache b/templates/go/search_helpers.mustache index 1bf131b234..9c529f14e9 100644 --- a/templates/go/search_helpers.mustache +++ b/templates/go/search_helpers.mustache @@ -286,7 +286,7 @@ func (c *APIClient) WaitForApiKey( return c.GetApiKey(c.NewApiGetApiKeyRequest(key), toRequestOptions(opts)...) }, validateFunc, - toIterableOptionsWaitFor(opts)..., + waitForApiKeyToIterableOptions(opts)..., ) } @@ -587,7 +587,7 @@ func (c *APIClient) PartialUpdateObjects(indexName string, objects []map[string] action = ACTION_PARTIAL_UPDATE_OBJECT_NO_CREATE } - return c.ChunkedBatch(indexName, objects, action, toChunkedBatchOptions(opts)...) + return c.ChunkedBatch(indexName, objects, action, partialUpdateObjectsToChunkedBatchOptions(opts)...) } /* @@ -645,42 +645,50 @@ See https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers @param indexName string - the index name to replace objects into. @param objects []map[string]any - List of objects to replace. - @param opts ...ChunkedBatchOption - Optional parameters for the request. + @param opts ...ReplaceAllObjectsOption - Optional parameters for the request. @return *ReplaceAllObjectsResponse - The response of the replace all objects operation. @return error - Error if any. */ -func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any, opts ...ChunkedBatchOption) (*ReplaceAllObjectsResponse, error) { +func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any, opts ...ReplaceAllObjectsOption) (*ReplaceAllObjectsResponse, error) { tmpIndexName := fmt.Sprintf("%s_tmp_%d", indexName, time.Now().UnixNano()) - copyResp, err := c.OperationIndex(c.NewApiOperationIndexRequest(indexName, NewOperationIndexParams(OPERATION_TYPE_COPY, tmpIndexName, WithOperationIndexParamsScope([]ScopeType{SCOPE_TYPE_SETTINGS, SCOPE_TYPE_RULES, SCOPE_TYPE_SYNONYMS}))), toRequestOptions(opts)...) + conf := config{ + scopes: []ScopeType{SCOPE_TYPE_SETTINGS, SCOPE_TYPE_RULES, SCOPE_TYPE_SYNONYMS}, + } + + for _, opt := range opts { + opt.apply(&conf) + } + + opts = append(opts, WithWaitForTasks(true)) + + copyResp, err := c.OperationIndex(c.NewApiOperationIndexRequest(indexName, NewOperationIndexParams(OPERATION_TYPE_COPY, tmpIndexName, WithOperationIndexParamsScope(conf.scopes))), toRequestOptions(opts)...) if err != nil { return nil, err } - opts = append(opts, WithWaitForTasks(true)) - - batchResp, err := c.ChunkedBatch(tmpIndexName, objects, ACTION_ADD_OBJECT, opts...) + batchResp, err := c.ChunkedBatch(tmpIndexName, objects, ACTION_ADD_OBJECT, replaceAllObjectsToChunkBactchOptions(opts)...) if err != nil { _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName)) return nil, err } - _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, toIterableOptions(opts)...) + _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...) if err != nil { _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName)) return nil, err } - copyResp, err = c.OperationIndex(c.NewApiOperationIndexRequest(indexName, NewOperationIndexParams(OPERATION_TYPE_COPY, tmpIndexName, WithOperationIndexParamsScope([]ScopeType{SCOPE_TYPE_SETTINGS, SCOPE_TYPE_RULES, SCOPE_TYPE_SYNONYMS}))), toRequestOptions(opts)...) + copyResp, err = c.OperationIndex(c.NewApiOperationIndexRequest(indexName, NewOperationIndexParams(OPERATION_TYPE_COPY, tmpIndexName, WithOperationIndexParamsScope(conf.scopes))), toRequestOptions(opts)...) if err != nil { _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName)) return nil, err } - _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, toIterableOptions(opts)...) + _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...) if err != nil { _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName)) @@ -694,7 +702,7 @@ func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any return nil, err } - _, err = c.WaitForTask(tmpIndexName, moveResp.TaskID, toIterableOptions(opts)...) + _, err = c.WaitForTask(tmpIndexName, moveResp.TaskID, replaceAllObjectsToIterableOptions(opts)...) if err != nil { _, _ = c.DeleteIndex(c.NewApiDeleteIndexRequest(tmpIndexName))