-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added: generate opt.ExtraOptions(map[string]interface{}) option
- Loading branch information
Showing
10 changed files
with
222 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
algolia/internal/gen/templates/extract/map_string_interface.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
41 changes: 41 additions & 0 deletions
41
algolia/internal/gen/templates/option_test/map_string_interface_slice.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.