From 1235b8a647b16aa09d8b87b1f104dffabfc7257f Mon Sep 17 00:00:00 2001 From: "Nguyen Viet Hoang (Stephen)" Date: Thu, 17 Oct 2024 23:25:47 +0900 Subject: [PATCH] chore: reduce cognitive complexity --- cmd/relayproxy/service/gofeatureflag.go | 83 ++++++++++++++++--------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/cmd/relayproxy/service/gofeatureflag.go b/cmd/relayproxy/service/gofeatureflag.go index 4d21218e380f..f0f41adb9c7f 100644 --- a/cmd/relayproxy/service/gofeatureflag.go +++ b/cmd/relayproxy/service/gofeatureflag.go @@ -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