diff --git a/kenall.go b/kenall.go index bc303f5..efd0d09 100644 --- a/kenall.go +++ b/kenall.go @@ -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"` @@ -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 } @@ -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. @@ -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 +} diff --git a/kenall_test.go b/kenall_test.go index 4cdf3c3..231d653 100644 --- a/kenall_test.go +++ b/kenall_test.go @@ -21,13 +21,14 @@ 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 { @@ -35,13 +36,24 @@ func TestNewClient(t *testing.T) { 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) } }) }