Update constructor implementation

This commit is contained in:
kardolus
2023-05-05 13:55:37 -04:00
parent c6977754e6
commit ec26fea3f6
5 changed files with 14 additions and 18 deletions

View File

@@ -29,15 +29,7 @@ type Client struct {
capacity int
}
func New(caller http.Caller, rw history.Store, capacity int) *Client {
return &Client{
caller: caller,
readWriter: rw,
capacity: capacity,
}
}
func NewDefault(caller http.Caller, rw history.Store) *Client {
func New(caller http.Caller, rw history.Store) *Client {
return &Client{
caller: caller,
readWriter: rw,
@@ -45,6 +37,11 @@ func NewDefault(caller http.Caller, rw history.Store) *Client {
}
}
func (c *Client) WithCapacity(capacity int) *Client {
c.capacity = capacity
return c
}
// Query sends a query to the API and returns the response as a string.
// It takes an input string as a parameter and returns a string containing
// the API response or an error if there's any issue during the process.

View File

@@ -36,7 +36,7 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
mockCtrl = gomock.NewController(t)
mockCaller = NewMockCaller(mockCtrl)
mockStore = NewMockStore(mockCtrl)
subject = client.New(mockCaller, mockStore, 50)
subject = client.New(mockCaller, mockStore).WithCapacity(50)
})
it.After(func() {

View File

@@ -52,7 +52,7 @@ func run(cmd *cobra.Command, args []string) error {
}
if clearHistory {
historyHandler := history.NewDefault()
historyHandler := history.New()
err := historyHandler.Delete()
if err != nil {
return err
@@ -72,7 +72,7 @@ func run(cmd *cobra.Command, args []string) error {
if secret == "" {
return errors.New("missing environment variable: " + secretEnv)
}
client := client.NewDefault(http.New().WithSecret(secret), history.NewDefault())
client := client.New(http.New().WithSecret(secret), history.New())
// Check if there is input from the pipe (stdin)
stat, _ := os.Stdin.Stat()

View File

@@ -21,17 +21,16 @@ type FileIO struct {
historyFilePath string
}
func NewDefault() *FileIO {
func New() *FileIO {
path, _ := getPath()
return &FileIO{
historyFilePath: path,
}
}
func New(historyFilePath string) *FileIO {
return &FileIO{
historyFilePath: historyFilePath,
}
func (f *FileIO) WithHistory(historyFilePath string) *FileIO {
f.historyFilePath = historyFilePath
return f
}
func (f *FileIO) Delete() error {

View File

@@ -39,7 +39,7 @@ func testIntegration(t *testing.T, when spec.G, it spec.S) {
tmpFile.Close()
fileIO = history.New(tmpFile.Name())
fileIO = history.New().WithHistory(tmpFile.Name())
messages = []types.Message{
{