Skip to content

Commit

Permalink
feat: Dictionaries API methods (#644)
Browse files Browse the repository at this point in the history
* feat: implement dictionaries API methods

Co-authored-by: Chloe Liban <chloe.liban@gmail.com>
Co-authored-by: Jérôme Schneider <jerome.schneider@algolia.com>
  • Loading branch information
3 people authored Mar 26, 2021
1 parent 5ad29f7 commit 7fe9a59
Show file tree
Hide file tree
Showing 30 changed files with 978 additions and 9 deletions.
4 changes: 2 additions & 2 deletions algolia/analytics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ func NewClientWithConfig(config Configuration) *Client {
}
}

func (c *Client) waitTaskSearchClient(index string, taskID int64) error {
return c.searchClient.InitIndex(index).WaitTask(taskID)
func (c *Client) waitTaskSearchClient(index string, taskID int64, opts ...interface{}) error {
return c.searchClient.InitIndex(index).WaitTask(taskID, opts...)
}
2 changes: 1 addition & 1 deletion algolia/analytics/responses_ab_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ABTestTaskRes struct {
ABTestID int `json:"abTestID"`
Index string `json:"index"`
TaskID int64 `json:"taskID"`
wait func(index string, taskID int64) error
wait func(index string, taskID int64, opts ...interface{}) error
}

// Wait blocks until the AB test task completes or if there is an error while
Expand Down
1 change: 1 addition & 0 deletions algolia/internal/gen/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func convertInterfaceToString(defaultValue interface{}) string {
map[string]string,
map[string][]string,
map[string]map[string]string,
map[string]map[string]bool,
map[string]interface{}:
s = fmt.Sprintf("%#v", v)
default:
Expand Down
2 changes: 1 addition & 1 deletion algolia/internal/gen/gen_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {

if shouldBeGenerated(filepath) {
switch opt.DefaultValue.(type) {
case nil, bool, int, string, []string, map[string][]string, map[string]map[string]string:
case nil, bool, int, string, []string, map[string][]string, map[string]map[string]string, map[string]map[string]bool:
generateFile(extractLiteralTemplate, opt.Name, filepath)
case map[string]string:
generateFile(extractMapStringStringTemplate, opt.Name, filepath)
Expand Down
5 changes: 4 additions & 1 deletion algolia/internal/gen/gen_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
optionTestMapStringStringTemplate = createTemplate("templates/option_test/map_string_string.go.tmpl")
optionTestMapStringStringSliceTemplate = createTemplate("templates/option_test/map_string_string_slice.go.tmpl")
optionTestMapStringMapStringStringTemplate = createTemplate("templates/option_test/map_string_map_string_string.go.tmpl")
optionTestMapStringMapStringBoolTemplate = createTemplate("templates/option_test/map_string_map_string_bool.go.tmpl")
optionTestMapStringInterfaceTemplate = createTemplate("templates/option_test/map_string_interface_slice.go.tmpl")

optionLiteralTemplate = createTemplate("templates/option/literal.go.tmpl")
Expand Down Expand Up @@ -59,7 +60,7 @@ func main() {
switch opt.DefaultValue.(type) {
case bool, int, string:
generateFile(optionLiteralTemplate, data, filepath)
case map[string]string, map[string][]string, map[string]interface{}, map[string]map[string]string:
case map[string]string, map[string][]string, map[string]interface{}, map[string]map[string]string, map[string]map[string]bool:
generateFile(optionMapTemplate, data, filepath)
case []string:
generateFile(optionStringSliceTemplate, data, filepath)
Expand Down Expand Up @@ -93,6 +94,8 @@ func main() {
generateFile(optionTestMapStringMapStringStringTemplate, data, testFilepath)
case map[string]interface{}:
generateFile(optionTestMapStringInterfaceTemplate, data, testFilepath)
case map[string]map[string]bool:
generateFile(optionTestMapStringMapStringBoolTemplate, data, testFilepath)
default:
fmt.Printf("cannot generate option test file for %s: unhandled type %#v", opt.Name, opt.DefaultValue)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions algolia/internal/gen/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,6 @@ var options = []Option{
{"safe", Other, false, ""},
{"filterPromotes", Other, false, ""},
{"exposeIntermediateNetworkErrors", Other, false, ""},
{"language", Other, "", ""},
{"disableStandardEntries", Other, map[string]map[string]bool{}, ""},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Code generated by go generate. DO NOT EDIT.

package opt

import (
"encoding/json"
"testing"

"github.com/algolia/algoliasearch-client-go/v3/algolia/opt"
"github.com/stretchr/testify/require"
)

func TestExtract{{ .Name }}(t *testing.T) {
for _, c := range []struct {
opts []interface{}
expected *opt.{{ .Name }}Option
}{
{
opts: []interface{}{nil},
expected: opt.{{ .Name }}({{ .DefaultValue }}),
},
{
opts: []interface{}{opt.{{ .Name }}(map[string]map[string]bool{})},
expected: opt.{{ .Name }}(map[string]map[string]bool{}),
},
{
opts: []interface{}{opt.{{ .Name }}(map[string]map[string]bool{"outer1": map[string]bool{"k1": true, "k2": false}, "outer2": map[string]bool{"k3": true}})},
expected: opt.{{ .Name }}(map[string]map[string]bool{"outer1": map[string]bool{"k1": true, "k2": false}, "outer2": map[string]bool{"k3": true}}),
},
} {
var (
in = Extract{{ .Name }}(c.opts...)
out opt.{{ .Name }}Option
)
data, err := json.Marshal(&in)
require.NoError(t, err)
err = json.Unmarshal(data, &out)
require.NoError(t, err)
require.Equal(t, *c.expected, out)
}
}
18 changes: 18 additions & 0 deletions algolia/internal/opt/disable_standard_entries.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions algolia/internal/opt/disable_standard_entries_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions algolia/internal/opt/language.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions algolia/internal/opt/language_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions algolia/opt/disable_standard_entries.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions algolia/opt/language.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions algolia/opt/option_getters_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions algolia/search/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,23 @@ func (c *Client) CustomRequest(
) error {
return c.transport.Request(&res, method, path, body, k, opts...)
}

// GetStatus retrieves the task status according to the Algolia engine for the
// given task.
func (c *Client) GetStatus(taskID int64, opts ...interface{}) (res TaskStatusRes, err error) {
path := c.path("/task/%d", taskID)
err = c.transport.Request(&res, http.MethodGet, path, nil, call.Read, opts...)
return
}

// WaitTask blocks until the task identified by the given taskID is completed on
// Algolia engine.
func (c *Client) WaitTask(taskID int64, opts ...interface{}) error {
return waitWithRetry(func() (bool, error) {
res, err := c.GetStatus(taskID, opts...)
if err != nil {
return true, err
}
return res.Status == "published", nil
})
}
Loading

0 comments on commit 7fe9a59

Please sign in to comment.