-
Notifications
You must be signed in to change notification settings - Fork 54
/
example_custom_strategy_test.go
67 lines (54 loc) · 1.4 KB
/
example_custom_strategy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package unleash_test
import (
"fmt"
"github.com/Unleash/unleash-client-go/v4"
"github.com/Unleash/unleash-client-go/v4/context"
"strings"
"time"
)
type ActiveForUserWithEmailStrategy struct{}
func (s ActiveForUserWithEmailStrategy) Name() string {
return "ActiveForUserWithEmail"
}
func (s ActiveForUserWithEmailStrategy) IsEnabled(params map[string]interface{}, ctx *context.Context) bool {
if ctx == nil {
return false
}
value, found := params["emails"]
if !found {
return false
}
emails, ok := value.(string)
if !ok {
return false
}
for _, e := range strings.Split(emails, ",") {
if e == ctx.Properties["emails"] {
return true
}
}
return false
}
// ExampleCustomStrategy demonstrates using a custom strategy.
func Example_customStrategy() {
unleash.Initialize(
unleash.WithListener(&unleash.DebugListener{}),
unleash.WithAppName("my-application"),
unleash.WithUrl("https://unleash.herokuapp.com/api/"),
unleash.WithRefreshInterval(5*time.Second),
unleash.WithMetricsInterval(5*time.Second),
unleash.WithStrategies(&ActiveForUserWithEmailStrategy{}),
)
ctx := context.Context{
Properties: map[string]string{
"emails": "[email protected]",
},
}
timer := time.NewTimer(1 * time.Second)
for {
<-timer.C
enabled := unleash.IsEnabled("unleash.me", unleash.WithContext(ctx))
fmt.Printf("feature is enabled? %v\n", enabled)
timer.Reset(1 * time.Second)
}
}