Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
refactor: use config struct instead of functional options
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspore committed Nov 14, 2022
1 parent ce9b1d8 commit 427692a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 51 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (
)

func main() {
client, err := t38c.New("localhost:9851", t38c.Debug)
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true, // print queries to stdout
})
if err != nil {
panic(err)
}
Expand Down
56 changes: 20 additions & 36 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,33 @@ type Client struct {
Server *Server
}

type clientParams struct {
debug bool
password *string
poolSize int
}

// ClientOption ...
type ClientOption func(*clientParams)

// Debug option.
var Debug = ClientOption(func(c *clientParams) {
c.debug = true
})

// WithPassword option.
func WithPassword(password string) ClientOption {
return func(c *clientParams) {
c.password = &password
}
}

// SetPoolSize option.
func SetPoolSize(size int) ClientOption {
return func(c *clientParams) {
c.poolSize = size
}
// Config is a t38c client config.
type Config struct {
// Tile38 server address.
//
// Example: localhost:9851
Address string

// Enables debug logging.
// Executed queries will be printed to stdout.
Debug bool

// Allows to perform password authorization.
Password *string

// ConnectionPoolSize sets number of connections in the pool.
// Defaults to 4.
ConnectionPoolSize int
}

// New creates a new Tile38 client.
// By default uses redis pool with 5 connections.
// In debug mode will also print commands which will be sent to the server.
func New(addr string, opts ...ClientOption) (*Client, error) {
params := &clientParams{poolSize: 5}
for _, opt := range opts {
opt(params)
}

radixPool, err := transport.NewRadix(addr, params.poolSize, params.password)
func New(cfg Config) (*Client, error) {
radixPool, err := transport.NewRadix(cfg.Address, cfg.ConnectionPoolSize, cfg.Password)
if err != nil {
return nil, err
}

return NewWithExecutor(radixPool, params.debug)
return NewWithExecutor(radixPool, cfg.Debug)
}

// NewWithExecutor creates a new Tile38 client with provided executor.
Expand Down
5 changes: 4 additions & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
func TestE2E(t *testing.T) {
skipE2E(t)

client, err := t38c.New(os.Getenv("T38C_TEST_ADDR"), t38c.Debug)
client, err := t38c.New(t38c.Config{
Address: os.Getenv("T38C_TEST_ADDR"),
Debug: true,
})
require.NoError(t, err)

t.Run("TestKeys", func(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion examples/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
)

func main() {
tile38, err := t38c.New("localhost:9851", t38c.Debug, t38c.SetPoolSize(10))
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion examples/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
)

func main() {
tile38, err := t38c.New("localhost:9851", t38c.Debug)
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
Expand Down
11 changes: 4 additions & 7 deletions examples/flush_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ import (
*/

func main() {
// Variables to be used along the way.
var (
err error
tile38 *t38c.Client
)

// Create a Tile38 client.
tile38, err = t38c.New("localhost:9851", t38c.Debug)
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion examples/geofencing.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
)

func main() {
tile38, err := t38c.New("localhost:9851", t38c.Debug)
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion examples/object_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
)

func main() {
tile38, err := t38c.New("localhost:9851", t38c.Debug)
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion examples/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
)

func main() {
tile38, err := t38c.New("localhost:9851", t38c.Debug)
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion examples/searching.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
)

func main() {
tile38, err := t38c.New("localhost:9851", t38c.Debug)
tile38, err := t38c.New(t38c.Config{
Address: "localhost:9851",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 427692a

Please sign in to comment.