Skip to content

Commit 27b2772

Browse files
authored
Update segments options with strings (#362)
* update segments options with strings * add tests for segment options * cleanup * remove booleans for segment options
1 parent a96c307 commit 27b2772

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

pkg/odp/segment/segment_option.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,30 @@
1717
// Package segment //
1818
package segment
1919

20+
import "errors"
21+
2022
// OptimizelySegmentOption represents options controlling audience segments.
21-
type OptimizelySegmentOption int
23+
type OptimizelySegmentOption string
2224

2325
const (
2426
// IgnoreCache ignores cache (save/lookup)
25-
IgnoreCache OptimizelySegmentOption = iota
27+
IgnoreCache OptimizelySegmentOption = "IGNORE_CACHE"
2628
// ResetCache resets cache
27-
ResetCache
29+
ResetCache OptimizelySegmentOption = "RESET_CACHE"
2830
)
31+
32+
// TranslateOptions converts string options array to array of OptimizelySegmentOptions
33+
func TranslateOptions(options []string) ([]OptimizelySegmentOption, error) {
34+
segmentOptions := []OptimizelySegmentOption{}
35+
for _, val := range options {
36+
switch OptimizelySegmentOption(val) {
37+
case IgnoreCache:
38+
segmentOptions = append(segmentOptions, IgnoreCache)
39+
case ResetCache:
40+
segmentOptions = append(segmentOptions, ResetCache)
41+
default:
42+
return []OptimizelySegmentOption{}, errors.New("invalid option: " + val)
43+
}
44+
}
45+
return segmentOptions, nil
46+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/****************************************************************************
2+
* Copyright 2023, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
package segment
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestTranslateOptionsValidCases(t *testing.T) {
27+
// Checking nil options
28+
translatedOptions, err := TranslateOptions(nil)
29+
assert.NoError(t, err)
30+
assert.Len(t, translatedOptions, 0)
31+
32+
// Checking empty options
33+
options := []string{}
34+
translatedOptions, err = TranslateOptions(options)
35+
assert.NoError(t, err)
36+
assert.Len(t, translatedOptions, 0)
37+
38+
// Checking correct options
39+
options = []string{"IGNORE_CACHE"}
40+
translatedOptions, err = TranslateOptions(options)
41+
assert.NoError(t, err)
42+
assert.Len(t, translatedOptions, 1)
43+
assert.Equal(t, IgnoreCache, translatedOptions[0])
44+
45+
// Checking after appending further options
46+
options = append(options, "RESET_CACHE")
47+
translatedOptions, err = TranslateOptions(options)
48+
assert.NoError(t, err)
49+
assert.Len(t, translatedOptions, 2)
50+
assert.Equal(t, ResetCache, translatedOptions[1])
51+
}
52+
53+
func TestTranslateOptionsInvalidCases(t *testing.T) {
54+
// Checking empty value as option
55+
options := []string{""}
56+
translatedOptions, err := TranslateOptions(options)
57+
assert.Error(t, err)
58+
assert.Equal(t, fmt.Errorf("invalid option: %v", options[0]), err)
59+
assert.Len(t, translatedOptions, 0)
60+
61+
// Checking invalid value as option
62+
options[0] = "INVALID"
63+
translatedOptions, err = TranslateOptions(options)
64+
assert.Error(t, err)
65+
assert.Equal(t, fmt.Errorf("invalid option: %v", options[0]), err)
66+
assert.Len(t, translatedOptions, 0)
67+
}

0 commit comments

Comments
 (0)