Skip to content

Commit 4286894

Browse files
EDsCODEneilkakkar
andauthored
Local Feature Flag Evaluation (#6)
* initial translation * matching func * consistency tests working * working person test * operator tests * regex test * handle groups * getallflags func and more tests * working tests * working tests * add onlyEvaluateLocally and sendFeatureFlagEvents * consistency tests speedup * more tets and stubs * fix * add map limit * use struct payload * fix payloads * Update feature_flags_test.go Co-authored-by: Neil Kakkar <[email protected]> * test dict separately * remove default result * address comments * condense * reorg request functions * make sure isfeatureenabled returns null Co-authored-by: Neil Kakkar <[email protected]>
1 parent f7d0654 commit 4286894

25 files changed

+4220
-122
lines changed

examples/featureflags.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/posthog/posthog-go"
65
"time"
6+
7+
"github.com/posthog/posthog-go"
78
)
89

910
func TestIsFeatureEnabled() {
@@ -15,29 +16,42 @@ func TestIsFeatureEnabled() {
1516
})
1617
defer client.Close()
1718

18-
boolResult, boolErr := client.IsFeatureEnabled("multivariate-test", "hello", false)
19+
boolResult, boolErr := client.IsFeatureEnabled(
20+
posthog.FeatureFlagPayload{
21+
Key: "multivariate-test",
22+
DistinctId: "hello",
23+
})
1924

2025
if boolErr != nil || !boolResult {
2126
fmt.Println("error:", boolErr)
2227
return
2328
}
2429

2530
// Simple flag
26-
simpleResult, simpleErr := client.GetFeatureFlag("simple-test", "hello", false)
31+
simpleResult, simpleErr := client.GetFeatureFlag(posthog.FeatureFlagPayload{
32+
Key: "simple-test",
33+
DistinctId: "hello",
34+
})
2735
if simpleErr != nil || simpleResult == false {
2836
fmt.Println("error:", simpleErr)
2937
return
3038
}
3139

3240
// Multivariate flag
33-
variantResult, variantErr := client.GetFeatureFlag("multivariate-test", "hello", false)
41+
variantResult, variantErr := client.GetFeatureFlag(posthog.FeatureFlagPayload{
42+
Key: "multivariate-test",
43+
DistinctId: "hello",
44+
})
3445
if variantErr != nil || variantResult != "variant-value" {
3546
fmt.Println("error:", variantErr)
3647
return
3748
}
3849

3950
// Multivariate + simple flag
40-
variantResult, variantErr = client.GetFeatureFlag("multivariate-simple-test", "hello", false)
51+
variantResult, variantErr = client.GetFeatureFlag(posthog.FeatureFlagPayload{
52+
Key: "multivariate-simple-test",
53+
DistinctId: "hello",
54+
})
4155
if variantErr != nil || variantResult == true {
4256
fmt.Println("error:", variantErr)
4357
return

feature_flag_config.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package posthog
2+
3+
type FeatureFlagPayload struct {
4+
Key string
5+
DistinctId string
6+
Groups Groups
7+
PersonProperties Properties
8+
GroupProperties map[string]Properties
9+
OnlyEvaluateLocally bool
10+
SendFeatureFlagEvents *bool
11+
}
12+
13+
func (c *FeatureFlagPayload) validate() error {
14+
if len(c.Key) == 0 {
15+
return ConfigError{
16+
Reason: "Feature Flag Key required",
17+
Field: "Key",
18+
Value: c.Key,
19+
}
20+
}
21+
22+
if len(c.DistinctId) == 0 {
23+
return ConfigError{
24+
Reason: "DistinctId required",
25+
Field: "Distinct Id",
26+
Value: c.DistinctId,
27+
}
28+
}
29+
30+
if c.Groups == nil {
31+
c.Groups = Groups{}
32+
}
33+
34+
if c.PersonProperties == nil {
35+
c.PersonProperties = NewProperties()
36+
}
37+
38+
if c.GroupProperties == nil {
39+
c.GroupProperties = map[string]Properties{}
40+
}
41+
42+
if c.SendFeatureFlagEvents == nil {
43+
tempTrue := true
44+
c.SendFeatureFlagEvents = &tempTrue
45+
}
46+
return nil
47+
}
48+
49+
type FeatureFlagPayloadNoKey struct {
50+
DistinctId string
51+
Groups Groups
52+
PersonProperties Properties
53+
GroupProperties map[string]Properties
54+
OnlyEvaluateLocally bool
55+
SendFeatureFlagEvents *bool
56+
}
57+
58+
func (c *FeatureFlagPayloadNoKey) validate() error {
59+
if len(c.DistinctId) == 0 {
60+
return ConfigError{
61+
Reason: "DistinctId required",
62+
Field: "Distinct Id",
63+
Value: c.DistinctId,
64+
}
65+
}
66+
67+
if c.Groups == nil {
68+
c.Groups = Groups{}
69+
}
70+
71+
if c.PersonProperties == nil {
72+
c.PersonProperties = NewProperties()
73+
}
74+
75+
if c.GroupProperties == nil {
76+
c.GroupProperties = map[string]Properties{}
77+
}
78+
79+
if c.SendFeatureFlagEvents == nil {
80+
tempTrue := true
81+
c.SendFeatureFlagEvents = &tempTrue
82+
}
83+
return nil
84+
}

0 commit comments

Comments
 (0)