Skip to content

Commit c20465b

Browse files
authored
[FSSDK-9632] Add helper function for notification center (#381)
* add WithNotificationCenter helper method * add get notification center method * run source clear in each PR * use latest go version for source clear * add setup action * fix linter error * fix linter error * add unit tests * remove unnecessary replace directive * add steps name in CI * skip gocyclo linter for Client
1 parent f8a4b27 commit c20465b

File tree

8 files changed

+53
-9
lines changed

8 files changed

+53
-9
lines changed

.github/workflows/source_clear_cron.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: Source clear
33
on:
44
push:
55
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
68
schedule:
79
# Runs "weekly"
810
- cron: '0 0 * * 0'
@@ -11,8 +13,16 @@ jobs:
1113
source_clear:
1214
runs-on: ubuntu-latest
1315
steps:
14-
- uses: actions/checkout@v3
16+
- name: Checkout Code
17+
uses: actions/checkout@v3
18+
- name: Setup Go
19+
uses: actions/setup-go@v3
20+
with:
21+
go-version: '1.21.0'
22+
check-latest: true
1523
- name: Source clear scan
1624
env:
1725
SRCCLR_API_TOKEN: ${{ secrets.SRCCLR_API_TOKEN }}
18-
run: curl -sSL https://download.sourceclear.com/ci.sh | bash -s - scan
26+
run: |
27+
go mod tidy
28+
curl -sSL https://download.sourceclear.com/ci.sh | bash -s - scan

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cover: ## run unit tests with coverage
2323
GO111MODULE=$(GO111MODULE) $(GOTEST) -race ./pkg/... -coverprofile=profile.cov
2424

2525
install: ## installs dev and ci dependencies
26-
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.52.2
26+
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(GOPATH)/bin v1.54.2
2727

2828
lint: ## runs `golangci-lint` linters defined in `.golangci.yml` file
2929
$(GOLINT) run --out-format=tab --tests=false pkg/...

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,3 @@ require (
2424
github.com/stretchr/objx v0.5.0 // indirect
2525
gopkg.in/yaml.v3 v3.0.1 // indirect
2626
)
27-
28-
// Work around issue with git.apache.org/thrift.git
29-
replace git.apache.org/thrift.git => github.com/apache/thrift v0.12.0

pkg/client/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,11 @@ func (o *OptimizelyClient) GetOptimizelyConfig() (optimizelyConfig *config.Optim
10611061
return o.ConfigManager.GetOptimizelyConfig()
10621062
}
10631063

1064+
// GetNotificationCenter returns Optimizely Notification Center interface
1065+
func (o *OptimizelyClient) GetNotificationCenter() notification.Center {
1066+
return o.notificationCenter
1067+
}
1068+
10641069
// Close closes the Optimizely instance and stops any ongoing tasks from its children components.
10651070
func (o *OptimizelyClient) Close() {
10661071
o.execGroup.TerminateAndWait()

pkg/client/client_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,15 @@ func TestGetOptimizelyConfig(t *testing.T) {
13781378
assert.Equal(t, &config.OptimizelyConfig{Revision: "232"}, optimizelyConfig)
13791379
}
13801380

1381+
func TestGetNotificationCenter(t *testing.T) {
1382+
nc := &MockNotificationCenter{}
1383+
client := OptimizelyClient{
1384+
notificationCenter: nc,
1385+
}
1386+
1387+
assert.Equal(t, client.GetNotificationCenter(), nc)
1388+
}
1389+
13811390
func TestGetFeatureDecisionValid(t *testing.T) {
13821391
testFeatureKey := "test_feature_key"
13831392
testVariableKey := "test_feature_flag_key"

pkg/client/factory.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type OptimizelyFactory struct {
5050
metricsRegistry metrics.Registry
5151
overrideStore decision.ExperimentOverrideStore
5252
userProfileService decision.UserProfileService
53+
notificationCenter notification.Center
5354

5455
// ODP
5556
segmentsCacheSize int
@@ -62,6 +63,8 @@ type OptimizelyFactory struct {
6263
type OptionFunc func(*OptimizelyFactory)
6364

6465
// Client instantiates a new OptimizelyClient with the given options.
66+
//
67+
//nolint:gocyclo // exceeds gocyclo cyclomatic complexity
6568
func (f *OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClient, error) {
6669
// Default values for odp cache
6770
f.segmentsCacheSize = pkgUtils.DefaultSegmentsCacheSize
@@ -101,10 +104,15 @@ func (f *OptimizelyFactory) Client(clientOptions ...OptionFunc) (*OptimizelyClie
101104
appClient := &OptimizelyClient{
102105
defaultDecideOptions: decideOptions,
103106
execGroup: eg,
104-
notificationCenter: registry.GetNotificationCenter(f.SDKKey),
105107
logger: logging.GetLogger(f.SDKKey, "OptimizelyClient"),
106108
}
107109

110+
if f.notificationCenter != nil {
111+
appClient.notificationCenter = f.notificationCenter
112+
} else {
113+
appClient.notificationCenter = registry.GetNotificationCenter(f.SDKKey)
114+
}
115+
108116
if f.configManager != nil {
109117
appClient.ConfigManager = f.configManager
110118
} else {
@@ -285,6 +293,13 @@ func WithMetricsRegistry(metricsRegistry metrics.Registry) OptionFunc {
285293
}
286294
}
287295

296+
// WithNotificationCenter allows user to pass in their own implementation of the notification.Center interface
297+
func WithNotificationCenter(nc notification.Center) OptionFunc {
298+
return func(f *OptimizelyFactory) {
299+
f.notificationCenter = nc
300+
}
301+
}
302+
288303
// StaticClient returns a client initialized with a static project config.
289304
func (f *OptimizelyFactory) StaticClient() (optlyClient *OptimizelyClient, err error) {
290305

pkg/client/factory_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ func TestClientWithDefaultSDKSettings(t *testing.T) {
179179
assert.NotNil(t, optimizelyClient.OdpManager)
180180
}
181181

182+
func TestClientWithNotificationCenterInOptions(t *testing.T) {
183+
factory := OptimizelyFactory{SDKKey: "1212"}
184+
nc := &MockNotificationCenter{}
185+
optimizelyClient, err := factory.Client(WithNotificationCenter(nc))
186+
assert.NoError(t, err)
187+
assert.Equal(t, nc, optimizelyClient.notificationCenter)
188+
}
189+
182190
func TestDummy(t *testing.T) {
183191
factory := OptimizelyFactory{}
184192
configManager := config.NewPollingProjectConfigManager("123")

pkg/config/datafileprojectconfig/mappers/condition_trees.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func buildConditionTree(conditions interface{}) (conditionTree *entities.TreeNod
4444

4545
parsedConditions, retErr := parseConditions(conditions)
4646
if retErr != nil {
47-
return
47+
return nil, nil, retErr
4848
}
4949
odpSegments = []string{}
5050
value := reflect.ValueOf(parsedConditions)
@@ -101,7 +101,7 @@ func buildConditionTree(conditions interface{}) (conditionTree *entities.TreeNod
101101
n := &entities.TreeNode{}
102102
if err := createLeafCondition(v, n); err != nil {
103103
retErr = err
104-
return
104+
return nil, nil, retErr
105105
}
106106
// Extract odp segment from leaf node if applicable
107107
extractSegment(&odpSegments, n)

0 commit comments

Comments
 (0)