Skip to content

Commit aba050a

Browse files
authored
refact(decide-options): Added translate options method to be used by testapp and agent. (#311)
* Added translate options method to be used by testapp and agent. * suggested changes made.
1 parent 61eaa2f commit aba050a

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

pkg/decide/decide_options.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2020, Optimizely, Inc. and contributors *
2+
* Copyright 2020-2021, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -14,23 +14,25 @@
1414
* limitations under the License. *
1515
***************************************************************************/
1616

17-
// Package decide has option definitions for decide api
17+
// Package decide has option definitions and helper methods for decide api
1818
package decide
1919

20+
import "errors"
21+
2022
// OptimizelyDecideOptions controlling flag decisions.
21-
type OptimizelyDecideOptions int
23+
type OptimizelyDecideOptions string
2224

2325
const (
2426
// DisableDecisionEvent when set, disables decision event tracking.
25-
DisableDecisionEvent OptimizelyDecideOptions = 1 + iota
27+
DisableDecisionEvent OptimizelyDecideOptions = "DISABLE_DECISION_EVENT"
2628
// EnabledFlagsOnly when set, returns decisions only for flags which are enabled.
27-
EnabledFlagsOnly
29+
EnabledFlagsOnly OptimizelyDecideOptions = "ENABLED_FLAGS_ONLY"
2830
// IgnoreUserProfileService when set, skips user profile service for decision.
29-
IgnoreUserProfileService
31+
IgnoreUserProfileService OptimizelyDecideOptions = "IGNORE_USER_PROFILE_SERVICE"
3032
// IncludeReasons when set, includes info and debug messages in the decision reasons.
31-
IncludeReasons
33+
IncludeReasons OptimizelyDecideOptions = "INCLUDE_REASONS"
3234
// ExcludeVariables when set, excludes variable values from the decision result.
33-
ExcludeVariables
35+
ExcludeVariables OptimizelyDecideOptions = "EXCLUDE_VARIABLES"
3436
)
3537

3638
// Options defines options for controlling flag decisions.
@@ -41,3 +43,25 @@ type Options struct {
4143
IncludeReasons bool
4244
ExcludeVariables bool
4345
}
46+
47+
// TranslateOptions converts string options array to array of OptimizelyDecideOptions
48+
func TranslateOptions(options []string) ([]OptimizelyDecideOptions, error) {
49+
decideOptions := []OptimizelyDecideOptions{}
50+
for _, val := range options {
51+
switch OptimizelyDecideOptions(val) {
52+
case DisableDecisionEvent:
53+
decideOptions = append(decideOptions, DisableDecisionEvent)
54+
case EnabledFlagsOnly:
55+
decideOptions = append(decideOptions, EnabledFlagsOnly)
56+
case IgnoreUserProfileService:
57+
decideOptions = append(decideOptions, IgnoreUserProfileService)
58+
case ExcludeVariables:
59+
decideOptions = append(decideOptions, ExcludeVariables)
60+
case IncludeReasons:
61+
decideOptions = append(decideOptions, IncludeReasons)
62+
default:
63+
return []OptimizelyDecideOptions{}, errors.New("invalid option: " + val)
64+
}
65+
}
66+
return decideOptions, nil
67+
}

pkg/decide/decide_options_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/****************************************************************************
2+
* Copyright 2021, 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 decide
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{"DISABLE_DECISION_EVENT", "ENABLED_FLAGS_ONLY"}
40+
translatedOptions, err = TranslateOptions(options)
41+
assert.NoError(t, err)
42+
assert.Len(t, translatedOptions, 2)
43+
assert.Equal(t, DisableDecisionEvent, translatedOptions[0])
44+
assert.Equal(t, EnabledFlagsOnly, translatedOptions[1])
45+
46+
// Checking after appending further options
47+
options = append(options, "IGNORE_USER_PROFILE_SERVICE", "EXCLUDE_VARIABLES", "INCLUDE_REASONS")
48+
translatedOptions, err = TranslateOptions(options)
49+
assert.NoError(t, err)
50+
assert.Len(t, translatedOptions, 5)
51+
assert.Equal(t, IgnoreUserProfileService, translatedOptions[2])
52+
assert.Equal(t, ExcludeVariables, translatedOptions[3])
53+
assert.Equal(t, IncludeReasons, translatedOptions[4])
54+
}
55+
56+
func TestTranslateOptionsInvalidCases(t *testing.T) {
57+
// Checking empty value as option
58+
options := []string{""}
59+
translatedOptions, err := TranslateOptions(options)
60+
assert.Error(t, err)
61+
assert.Equal(t, fmt.Errorf("invalid option: %v", options[0]), err)
62+
assert.Len(t, translatedOptions, 0)
63+
64+
// Checking invalid value as option
65+
options[0] = "INVALID"
66+
translatedOptions, err = TranslateOptions(options)
67+
assert.Error(t, err)
68+
assert.Equal(t, fmt.Errorf("invalid option: %v", options[0]), err)
69+
assert.Len(t, translatedOptions, 0)
70+
}

0 commit comments

Comments
 (0)