Skip to content

Commit

Permalink
Redesign client option structure
Browse files Browse the repository at this point in the history
  • Loading branch information
osamingo committed Feb 15, 2021
1 parent 1447a55 commit 6d67834
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
40 changes: 28 additions & 12 deletions kenall.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ type (

token string
}
// A Option provides a Functional Option Pattern for kenall.Client.
Option func(*Client)

// A ClientOption provides a customize option for kenall.Client.
ClientOption interface {
Apply(*Client)
}
withHTTPClient struct {
client *http.Client
}
withEndpoint struct {
endpoint string
}

// A GetAddressResponse is a result from the kenall service of the API to get the address from the postal code.
GetAddressResponse struct {
Version Version `json:"version"`
Expand Down Expand Up @@ -82,7 +92,7 @@ type (
)

// NewClient creates kenall.Client with the authorization token provided by the kenall.
func NewClient(token string, opts ...Option) (*Client, error) {
func NewClient(token string, opts ...ClientOption) (*Client, error) {
if token == "" {
return nil, ErrInvalidArgument
}
Expand All @@ -94,24 +104,20 @@ func NewClient(token string, opts ...Option) (*Client, error) {
}

for _, opt := range opts {
opt(cli)
opt.Apply(cli)
}

return cli, nil
}

// WithHTTPClient injects optional HTTP Client to kenall.Client.
func WithHTTPClient(cli *http.Client) Option {
return func(c *Client) {
c.HTTPClient = cli
}
func WithHTTPClient(cli *http.Client) ClientOption {
return &withHTTPClient{client: cli}
}

// WithEndpoint injects optional endpoint to kenall.Client.
func WithEndpoint(endpoint string) Option {
return func(c *Client) {
c.Endpoint = endpoint
}
func WithEndpoint(endpoint string) ClientOption {
return &withEndpoint{endpoint: endpoint}
}

// GetAddress requests to the kenall service to get the address by postal code.
Expand Down Expand Up @@ -171,3 +177,13 @@ func (v *Version) UnmarshalJSON(data []byte) error {

return nil
}

// Apply implements kenall.ClientOption interface.
func (w *withHTTPClient) Apply(cli *Client) {
cli.HTTPClient = w.client
}

// Apply implements kenall.ClientOption interface.
func (w *withEndpoint) Apply(cli *Client) {
cli.Endpoint = w.endpoint
}
30 changes: 21 additions & 9 deletions kenall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,39 @@ func TestNewClient(t *testing.T) {
t.Parallel()

cases := map[string]struct {
token string
opts []kenall.Option
want error
token string
httpClient *http.Client
endpoint string
want error
}{
"Empty token": {token: "", opts: nil, want: kenall.ErrInvalidArgument},
"Give token": {token: "dummy", opts: nil, want: nil},
"Give token and opts": {token: "dummy", opts: []kenall.Option{kenall.WithEndpoint(""), kenall.WithHTTPClient(nil)}, want: nil},
"Empty token": {token: "", httpClient: nil, endpoint: "", want: kenall.ErrInvalidArgument},
"Give token": {token: "dummy", httpClient: nil, endpoint: "", want: nil},
"Give token and opts": {token: "dummy", httpClient: &http.Client{}, endpoint: "customize_endpoint", want: nil},
}

for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()

cli, err := kenall.NewClient(c.token, c.opts...)
opts := make([]kenall.ClientOption, 0, 2)
if c.httpClient != nil {
opts = append(opts, kenall.WithHTTPClient(c.httpClient))
}
if c.endpoint != "" {
opts = append(opts, kenall.WithEndpoint(c.endpoint))
}

cli, err := kenall.NewClient(c.token, opts...)
if !errors.Is(c.want, err) {
t.Errorf("give: %v, want: %v", err, c.want)
}

if len(c.opts) != 0 && cli.Endpoint != "" && cli.HTTPClient != nil {
t.Error("option is not reflected")
if c.httpClient != nil && cli.HTTPClient != c.httpClient {
t.Errorf("give: %v, want: %v", cli.HTTPClient, c.httpClient)
}
if c.endpoint != "" && cli.Endpoint != c.endpoint {
t.Errorf("give: %v, want: %v", cli.Endpoint, c.endpoint)
}
})
}
Expand Down

0 comments on commit 6d67834

Please sign in to comment.