Skip to content

Commit

Permalink
go is baaaaaad
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Jan 6, 2025
1 parent 6c8e7f1 commit ec582c4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
1 change: 1 addition & 0 deletions clients/algoliasearch-client-go/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ linters:

# Deprecated
- execinquery
- exportloopref

issues:
exclude-generated: disable
Expand Down
66 changes: 63 additions & 3 deletions templates/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,7 +116,7 @@ func WithBatchSize(batchSize int) chunkedBatchOption {
})
}

// --------- ChunkedBatch options ---------
// --------- PartialUpdateObjects options ---------

type PartialUpdateObjectsOption interface {
ChunkedBatchOption
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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}}
Expand Down
32 changes: 20 additions & 12 deletions templates/go/search_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (c *APIClient) WaitForApiKey(
return c.GetApiKey(c.NewApiGetApiKeyRequest(key), toRequestOptions(opts)...)
},
validateFunc,
toIterableOptionsWaitFor(opts)...,
waitForApiKeyToIterableOptions(opts)...,
)
}

Expand Down Expand Up @@ -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)...)
}

/*
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit ec582c4

Please sign in to comment.