Skip to content

Commit

Permalink
added: generate opt.ExtraOptions(map[string]interface{}) option
Browse files Browse the repository at this point in the history
  • Loading branch information
aseure committed Jun 24, 2019
1 parent 486e444 commit 39c6ad7
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 5 deletions.
5 changes: 4 additions & 1 deletion algolia/internal/gen/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ func convertInterfaceToString(defaultValue interface{}) string {
s = fmt.Sprintf("%d", v)
case string:
s = fmt.Sprintf("%q", v)
case []string, map[string]string, map[string][]string:
case []string,
map[string]string,
map[string][]string,
map[string]interface{}:
s = fmt.Sprintf("%#v", v)
default:
fmt.Printf("cannot convert interface to string: unhandled type %#v\n", defaultValue)
Expand Down
9 changes: 6 additions & 3 deletions algolia/internal/gen/gen_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (

func main() {
var (
extractLiteralTemplate = createTemplate("templates/extract/literal.go.tmpl")
extractMapTemplate = createTemplate("templates/extract/map_string_string.go.tmpl")
extractLiteralTemplate = createTemplate("templates/extract/literal.go.tmpl")
extractMapStringStringTemplate = createTemplate("templates/extract/map_string_string.go.tmpl")
extractMapStringInterfaceTemplate = createTemplate("templates/extract/map_string_interface.go.tmpl")
)

for _, opt := range options {
Expand All @@ -25,7 +26,9 @@ func main() {
case nil, bool, int, string, []string, map[string][]string:
generateFile(extractLiteralTemplate, opt.Name, filepath)
case map[string]string:
generateFile(extractMapTemplate, opt.Name, filepath)
generateFile(extractMapStringStringTemplate, opt.Name, filepath)
case map[string]interface{}:
generateFile(extractMapStringInterfaceTemplate, opt.Name, filepath)
default:
fmt.Printf("cannot generate extract option file for %s: unhandled type %#v", opt.Name, opt.DefaultValue)
os.Exit(1)
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 @@ -24,6 +24,7 @@ func main() {
optionTestStringSliceTemplate = createTemplate("templates/option_test/string_slice.go.tmpl")
optionTestMapStringStringTemplate = createTemplate("templates/option_test/map_string_string.go.tmpl")
optionTestMapStringStringSliceTemplate = createTemplate("templates/option_test/map_string_string_slice.go.tmpl")
optionTestMapStringInterfaceTemplate = createTemplate("templates/option_test/map_string_interface_slice.go.tmpl")

optionLiteralTemplate = createTemplate("templates/option/literal.go.tmpl")
optionMapTemplate = createTemplate("templates/option/map.go.tmpl")
Expand Down Expand Up @@ -57,7 +58,7 @@ func main() {
switch opt.DefaultValue.(type) {
case bool, int, string:
generateFile(optionLiteralTemplate, data, filepath)
case map[string]string, map[string][]string:
case map[string]string, map[string][]string, map[string]interface{}:
generateFile(optionMapTemplate, data, filepath)
case []string:
generateFile(optionSliceTemplate, data, filepath)
Expand Down Expand Up @@ -85,6 +86,8 @@ func main() {
generateFile(optionTestStringSliceTemplate, data, testFilepath)
case map[string]string:
generateFile(optionTestMapStringStringTemplate, data, testFilepath)
case map[string]interface{}:
generateFile(optionTestMapStringInterfaceTemplate, data, testFilepath)
case map[string][]string:
generateFile(optionTestMapStringStringSliceTemplate, data, testFilepath)
default:
Expand Down
1 change: 1 addition & 0 deletions algolia/internal/gen/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ var options = []Option{
{"forwardToReplicas", Other, false, ""},
{"anchoring", Other, "", ""},
{"extraHeaders", Other, map[string]string{}, ""},
{"extraOptions", Other, map[string]interface{}{}, ""},
{"extraURLParams", Other, map[string]string{}, ""},
{"scopes", Other, []string{}, ""},
{"clusterName", Other, "", ""},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Code generated by go generate. DO NOT EDIT.

package opt

import (
"github.com/algolia/algoliasearch-client-go/algolia/opt"
)

// Extract{{ title .}} returns the first found {{ title . }}Option from the
// given variadic arguments or nil otherwise. If multiple options are found, the
// inner maps are merged.
func Extract{{ title . }}(opts ...interface{}) *opt.{{ title . }}Option {
merged := make(map[string]interface{})

for _, o := range opts {
if v, ok := o.(*opt.{{ title . }}Option); ok {
for key, value := range v.Get() {
merged[key] = value
}
}
}

if len(merged) == 0 {
return nil
}

return opt.{{ title . }}(merged)
}
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/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]interface{}{})},
expected: opt.{{ .Name }}(map[string]interface{}{}),
},
{
opts: []interface{}{opt.{{ .Name }}(map[string]interface{}{"k1": 1, "k2": []string{"2", "3"}})},
expected: opt.{{ .Name }}(map[string]interface{}{"k1": 1.0, "k2": []interface{}{"2", "3"}}),
},
} {
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)
}
}
28 changes: 28 additions & 0 deletions algolia/internal/opt/extra_options.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/extra_options_test.go

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

66 changes: 66 additions & 0 deletions algolia/opt/extra_options.go

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

3 changes: 3 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.

0 comments on commit 39c6ad7

Please sign in to comment.