Skip to content

Commit

Permalink
feat(couchbase) Add optional couchbase
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetcanozcan committed Sep 30, 2024
1 parent 41f949c commit 84ecec1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
19 changes: 11 additions & 8 deletions modules/couchbase/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package couchbase

import (
"github.com/Trendyol/chaki"
"github.com/Trendyol/chaki/as"
"github.com/Trendyol/chaki/config"
"github.com/Trendyol/chaki/module"
Expand All @@ -12,10 +13,12 @@ var (
asCouchbaseTracers = as.Interface[gocb.RequestTracer]("gocbrequesttracers")
)

func Module() *module.Module {
func Module(option ...Option) *module.Module {
m := module.New()

m.Provide(
chaki.Valuer(option),
buildOptions,
asClusterOptionsWrapper.Grouper(),
asCouchbaseTracers.Grouper(),
newCluster,
Expand All @@ -31,11 +34,11 @@ func Module() *module.Module {
return m
}

func newCluster(cfg *config.Config, wrappers []ClusterOptionsWrapper, tracers []gocb.RequestTracer) (*gocb.Cluster, error) {
func newCluster(cfg *config.Config, opts *options) (*gocb.Cluster, error) {
cbcfg := cfg.Of("couchbase")
setDefaultConfigs(cbcfg)

opts := gocb.ClusterOptions{
clutserOptions := gocb.ClusterOptions{
Username: cbcfg.GetString("username"),
Password: cbcfg.GetString("password"),
TimeoutsConfig: gocb.TimeoutsConfig{
Expand All @@ -51,13 +54,13 @@ func newCluster(cfg *config.Config, wrappers []ClusterOptionsWrapper, tracers []
},
}

for _, wr := range wrappers {
opts = wr(opts)
for _, wr := range opts.clusterOptionsWrappers {
clutserOptions = wr(clutserOptions)
}

if len(tracers) > 0 {
opts.Tracer = newJoinedTracer(tracers)
if len(opts.tracers) > 0 {
clutserOptions.Tracer = newJoinedTracer(opts.tracers)
}

return gocb.Connect(cbcfg.GetString("host"), opts)
return gocb.Connect(cbcfg.GetString("host"), clutserOptions)
}
42 changes: 42 additions & 0 deletions modules/couchbase/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package couchbase

import "github.com/couchbase/gocb/v2"

type options struct {
clusterOptionsWrappers []ClusterOptionsWrapper
tracers []gocb.RequestTracer
}

type Option interface {
Apply(*options)
}

type withOption func(*options)

func (wf withOption) Apply(opts *options) {
wf(opts)
}

func WithTracer(tr ...gocb.RequestTracer) Option {
return withOption(func(o *options) {
o.tracers = append(o.tracers, tr...)
})
}

func WithClusterOptionWrapper(wrappers ...ClusterOptionsWrapper) Option {
return withOption(func(o *options) {
o.clusterOptionsWrappers = append(o.clusterOptionsWrappers, wrappers...)
})
}

func buildOptions(opt []Option, clusterOptionWrappers []ClusterOptionsWrapper, tracers []gocb.RequestTracer) *options {
opts := &options{}
for _, o := range opt {
o.Apply(opts)
}

WithClusterOptionWrapper(clusterOptionWrappers...).Apply(opts)
WithTracer(tracers...).Apply(opts)

return opts
}

0 comments on commit 84ecec1

Please sign in to comment.