Skip to content

Commit

Permalink
chore: reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangnv-bkhn committed Oct 17, 2024
1 parent 1c17a54 commit 1235b8a
Showing 1 changed file with 54 additions and 29 deletions.
83 changes: 54 additions & 29 deletions cmd/relayproxy/service/gofeatureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,75 +41,100 @@ func NewGoFeatureFlagClient(
logger *zap.Logger,
notifiers []notifier.Notifier,
) (*ffclient.GoFeatureFlag, error) {
var mainRetriever retriever.Retriever
var err error

if proxyConf == nil {
return nil, fmt.Errorf("proxy config is empty")
}

mainRetriever, retrievers, err := initRetrievers(proxyConf)
if err != nil {
return nil, err
}

mainDataExporter, dataExporters, err := initExporters(proxyConf)
if err != nil {
return nil, err
}

notif, err := initNotifiers(proxyConf.Notifiers, notifiers)
if err != nil {
return nil, err
}

f := ffclient.Config{
PollingInterval: time.Duration(proxyConf.PollingInterval) * time.Millisecond,
LeveledLogger: slog.New(slogzap.Option{Level: slog.LevelDebug, Logger: logger}.NewZapHandler()),
Context: context.Background(),
Retriever: mainRetriever,
Retrievers: retrievers,
Notifiers: notif,
FileFormat: proxyConf.FileFormat,
DataExporter: mainDataExporter,
DataExporters: dataExporters,
StartWithRetrieverError: proxyConf.StartWithRetrieverError,
EnablePollingJitter: proxyConf.EnablePollingJitter,
EvaluationContextEnrichment: proxyConf.EvaluationContextEnrichment,
PersistentFlagConfigurationFile: proxyConf.PersistentFlagConfigurationFile,
}

return ffclient.New(f)
}

func initRetrievers(proxyConf *config.Config) (retriever.Retriever, []retriever.Retriever, error) {
var mainRetriever retriever.Retriever
var err error

if proxyConf.Retriever != nil {
mainRetriever, err = initRetriever(proxyConf.Retriever)
if err != nil {
return nil, err
return nil, nil, err
}
}

// Manage if we have more than 1 retriever
retrievers := make([]retriever.Retriever, 0)
if proxyConf.Retrievers != nil {
for _, r := range *proxyConf.Retrievers {
currentRetriever, err := initRetriever(&r)
if err != nil {
return nil, err
return nil, nil, err
}
retrievers = append(retrievers, currentRetriever)
}
}

return mainRetriever, retrievers, nil
}

func initExporters(proxyConf *config.Config) (ffclient.DataExporter, []ffclient.DataExporter, error) {
var mainDataExporter ffclient.DataExporter
var err error

if proxyConf.Exporter != nil {
mainDataExporter, err = initDataExporter(proxyConf.Exporter)
if err != nil {
return nil, err
return ffclient.DataExporter{}, nil, err
}
}

// Manage the case where we have multiple data exporters
dataExporters := make([]ffclient.DataExporter, 0)
if proxyConf.Exporters != nil {
for _, e := range *proxyConf.Exporters {
currentExporter, err := initDataExporter(&e)
if err != nil {
return nil, err
return ffclient.DataExporter{}, nil, err
}
dataExporters = append(dataExporters, currentExporter)
}
}

notif, err := initNotifier(proxyConf.Notifiers)
return mainDataExporter, dataExporters, nil
}

func initNotifiers(configNotifiers []config.NotifierConf, additionalNotifiers []notifier.Notifier) ([]notifier.Notifier, error) {
notif, err := initNotifier(configNotifiers)
if err != nil {
return nil, err
}
notif = append(notif, notifiers...)

f := ffclient.Config{
PollingInterval: time.Duration(proxyConf.PollingInterval) * time.Millisecond,
LeveledLogger: slog.New(slogzap.Option{Level: slog.LevelDebug, Logger: logger}.NewZapHandler()),
Context: context.Background(),
Retriever: mainRetriever,
Retrievers: retrievers,
Notifiers: notif,
FileFormat: proxyConf.FileFormat,
DataExporter: mainDataExporter,
DataExporters: dataExporters,
StartWithRetrieverError: proxyConf.StartWithRetrieverError,
EnablePollingJitter: proxyConf.EnablePollingJitter,
EvaluationContextEnrichment: proxyConf.EvaluationContextEnrichment,
PersistentFlagConfigurationFile: proxyConf.PersistentFlagConfigurationFile,
}

return ffclient.New(f)
return append(notif, additionalNotifiers...), nil
}

// initRetriever initialize the retriever based on the configuration
Expand Down

0 comments on commit 1235b8a

Please sign in to comment.