mirror of
https://github.com/hanzoai/insights-go.git
synced 2026-09-30 22:46:12 +00:00
The module declares go 1.27.1 and the adapter image follows. go fix applied the modernizers and left ptrString and ptrBool with no callers, so they are gone; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
234 lines
8.7 KiB
Go
234 lines
8.7 KiB
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
|
||
json "github.com/goccy/go-json"
|
||
"github.com/hanzoai/insights-go"
|
||
)
|
||
|
||
func TestIsFeatureEnabled(projectAPIKey, personalAPIKey, endpoint string) {
|
||
client, err := insights.NewWithConfig(projectAPIKey, insights.Config{
|
||
Interval: 30 * time.Second,
|
||
BatchSize: 100,
|
||
Verbose: true,
|
||
PersonalApiKey: personalAPIKey,
|
||
Endpoint: endpoint,
|
||
DefaultFeatureFlagsPollingInterval: 5 * time.Second,
|
||
FeatureFlagRequestTimeout: 3 * time.Second,
|
||
})
|
||
if err != nil {
|
||
fmt.Println("error:", err)
|
||
return
|
||
}
|
||
defer client.Close()
|
||
|
||
boolResult, boolErr := client.IsFeatureEnabled(
|
||
insights.FeatureFlagPayload{
|
||
Key: "multivariate-test",
|
||
DistinctId: "hello",
|
||
})
|
||
|
||
fmt.Println("boolResult:", boolResult)
|
||
|
||
if boolErr != nil || boolResult == nil {
|
||
fmt.Println("error:", boolErr)
|
||
}
|
||
|
||
// Simple flag
|
||
simpleResult, simpleErr := client.GetFeatureFlag(insights.FeatureFlagPayload{
|
||
Key: "simple-test",
|
||
DistinctId: "hello",
|
||
})
|
||
|
||
fmt.Println("simpleResult:", simpleResult)
|
||
if simpleErr != nil || simpleResult == false {
|
||
fmt.Println("error:", simpleErr)
|
||
}
|
||
|
||
// Multivariate flag
|
||
variantResult, variantErr := client.GetFeatureFlag(insights.FeatureFlagPayload{
|
||
Key: "multivariate-test",
|
||
DistinctId: "hello",
|
||
})
|
||
fmt.Println("variantResult:", variantResult)
|
||
if variantErr != nil || variantResult != "variant-value" {
|
||
fmt.Println("error:", variantErr)
|
||
}
|
||
|
||
// Multivariate + simple flag
|
||
variantResult, variantErr = client.GetFeatureFlag(insights.FeatureFlagPayload{
|
||
Key: "multivariate-simple-test",
|
||
DistinctId: "hello",
|
||
})
|
||
fmt.Println("variantResult:", variantResult)
|
||
if variantErr != nil || variantResult == true {
|
||
fmt.Println("error:", variantErr)
|
||
}
|
||
|
||
// Encrypted remote config flag (string payload)
|
||
stringPayloadResult, stringPayloadErr := client.GetRemoteConfigPayload("my_secret_flag_value")
|
||
fmt.Println("stringPayloadResult:", stringPayloadResult)
|
||
if stringPayloadErr != nil {
|
||
fmt.Println("error:", stringPayloadErr)
|
||
}
|
||
|
||
// Encrypted remote config flag (json object payload)
|
||
jsonObjectPayloadResult, _ := client.GetRemoteConfigPayload("my_secret_flag_json_object_value")
|
||
var jsonPayloadMap map[string]any
|
||
json.Unmarshal([]byte(jsonObjectPayloadResult), &jsonPayloadMap)
|
||
|
||
// Encrypted remote config flag (json array payload)
|
||
jsonArrayPayloadResult, _ := client.GetRemoteConfigPayload("my_secret_flag_json_array_value")
|
||
var jsonArrayPayload []string
|
||
json.Unmarshal([]byte(jsonArrayPayloadResult), &jsonArrayPayload)
|
||
|
||
fmt.Println("ā¹ļø Feature flag evaluation completed!")
|
||
fmt.Println(" - IsFeatureEnabled(): Returns boolean for any flag type")
|
||
fmt.Println(" - GetFeatureFlag(): Returns the actual variant value")
|
||
fmt.Println(" - GetRemoteConfigPayload(): Returns encrypted payload data")
|
||
}
|
||
|
||
func TestFlagDependencies(projectAPIKey, personalAPIKey, endpoint string) {
|
||
fmt.Println("š Testing flag dependencies with local evaluation...")
|
||
fmt.Println(" This demonstrates pure flag evaluation with no events sent")
|
||
fmt.Println(" Flag structure: 'test-flag-dependency' depends on 'beta-feature' being enabled")
|
||
fmt.Println("")
|
||
fmt.Println("š Required setup (if 'test-flag-dependency' doesn't exist):")
|
||
fmt.Println(" 1. Create feature flag 'beta-feature':")
|
||
fmt.Println(" - Condition: email contains '@example.com'")
|
||
fmt.Println(" - Rollout: 100%")
|
||
fmt.Println(" 2. Create feature flag 'test-flag-dependency':")
|
||
fmt.Println(" - Condition: flag 'beta-feature' is enabled")
|
||
fmt.Println(" - Rollout: 100%")
|
||
fmt.Println("")
|
||
|
||
client, err := insights.NewWithConfig(projectAPIKey, insights.Config{
|
||
Interval: 30 * time.Second,
|
||
BatchSize: 100,
|
||
Verbose: false, // Disable verbose logging for cleaner output
|
||
PersonalApiKey: personalAPIKey,
|
||
Endpoint: endpoint,
|
||
DefaultFeatureFlagsPollingInterval: 5 * time.Second,
|
||
FeatureFlagRequestTimeout: 3 * time.Second,
|
||
})
|
||
if err != nil {
|
||
fmt.Printf("ā Error creating client: %v\n", err)
|
||
return
|
||
}
|
||
defer client.Close()
|
||
|
||
fmt.Println("ā Testing @example.com user (should satisfy dependency if flags exist)...")
|
||
// Disable automatic $feature_flag_called events for cleaner output
|
||
sendEvents := false
|
||
result1, err1 := client.IsFeatureEnabled(insights.FeatureFlagPayload{
|
||
Key: "test-flag-dependency",
|
||
DistinctId: "example_user",
|
||
PersonProperties: insights.NewProperties().Set("email", "[email protected]"),
|
||
OnlyEvaluateLocally: true,
|
||
SendFeatureFlagEvents: &sendEvents,
|
||
})
|
||
|
||
if err1 != nil {
|
||
fmt.Printf("ā Error evaluating test-flag-dependency for @example.com user: %v\n", err1)
|
||
} else {
|
||
fmt.Printf("ā
@example.com user (test-flag-dependency): %v\n", result1)
|
||
}
|
||
|
||
fmt.Println("ā Testing regular user (dependency should not be satisfied)...")
|
||
result2, err2 := client.IsFeatureEnabled(insights.FeatureFlagPayload{
|
||
Key: "test-flag-dependency",
|
||
DistinctId: "regular_user",
|
||
PersonProperties: insights.NewProperties().Set("email", "[email protected]"),
|
||
OnlyEvaluateLocally: true,
|
||
SendFeatureFlagEvents: &sendEvents,
|
||
})
|
||
|
||
if err2 != nil {
|
||
fmt.Printf("ā Error evaluating test-flag-dependency for regular user: %v\n", err2)
|
||
} else {
|
||
fmt.Printf("ā Regular user (test-flag-dependency): %v\n", result2)
|
||
}
|
||
|
||
fmt.Println("ā Testing beta-feature directly for comparison...")
|
||
|
||
beta1, betaErr1 := client.IsFeatureEnabled(insights.FeatureFlagPayload{
|
||
Key: "beta-feature",
|
||
DistinctId: "example_user",
|
||
PersonProperties: insights.NewProperties().Set("email", "[email protected]"),
|
||
OnlyEvaluateLocally: true,
|
||
SendFeatureFlagEvents: &sendEvents,
|
||
})
|
||
|
||
beta2, betaErr2 := client.IsFeatureEnabled(insights.FeatureFlagPayload{
|
||
Key: "beta-feature",
|
||
DistinctId: "regular_user",
|
||
PersonProperties: insights.NewProperties().Set("email", "[email protected]"),
|
||
OnlyEvaluateLocally: true,
|
||
SendFeatureFlagEvents: &sendEvents,
|
||
})
|
||
|
||
if betaErr1 != nil || betaErr2 != nil {
|
||
fmt.Printf("ā Error evaluating beta-feature: err1=%v, err2=%v\n", betaErr1, betaErr2)
|
||
} else {
|
||
fmt.Printf("š Beta feature comparison - @example.com: %v, regular: %v\n", beta1, beta2)
|
||
}
|
||
|
||
fmt.Println("ā Testing multivariate dependency chains...")
|
||
|
||
// Test pineapple -> blue -> breaking-bad chain
|
||
dependentResult3, err3 := client.GetFeatureFlag(insights.FeatureFlagPayload{
|
||
Key: "multivariate-root-flag",
|
||
DistinctId: "regular_user",
|
||
PersonProperties: insights.NewProperties().Set("email", "[email protected]"),
|
||
OnlyEvaluateLocally: true,
|
||
SendFeatureFlagEvents: &sendEvents,
|
||
})
|
||
|
||
if err3 != nil {
|
||
fmt.Printf("ā Error evaluating 'multivariate-root-flag' with [email protected]: %v\n", err3)
|
||
} else if dependentResult3 != "breaking-bad" {
|
||
fmt.Printf(" ā Something went wrong evaluating 'multivariate-root-flag' with [email protected]. Expected 'breaking-bad', got '%v'\n", dependentResult3)
|
||
} else {
|
||
fmt.Println("ā
'multivariate-root-flag' with email [email protected] succeeded")
|
||
}
|
||
|
||
// Test mango -> red -> the-wire chain
|
||
dependentResult4, err4 := client.GetFeatureFlag(insights.FeatureFlagPayload{
|
||
Key: "multivariate-root-flag",
|
||
DistinctId: "regular_user",
|
||
PersonProperties: insights.NewProperties().Set("email", "[email protected]"),
|
||
OnlyEvaluateLocally: true,
|
||
SendFeatureFlagEvents: &sendEvents,
|
||
})
|
||
|
||
if err4 != nil {
|
||
fmt.Printf("ā Error evaluating 'multivariate-root-flag' with [email protected]: %v\n", err4)
|
||
} else if dependentResult4 != "the-wire" {
|
||
fmt.Printf(" ā Something went wrong evaluating multivariate-root-flag with [email protected]. Expected 'the-wire', got '%v'\n", dependentResult4)
|
||
} else {
|
||
fmt.Println("ā
'multivariate-root-flag' with email [email protected] succeeded")
|
||
}
|
||
|
||
fmt.Println("\nšÆ Results Summary:")
|
||
if err1 == nil && err2 == nil && result1 != nil && result2 != nil {
|
||
// Convert to booleans for comparison
|
||
bool1, ok1 := result1.(bool)
|
||
bool2, ok2 := result2.(bool)
|
||
if ok1 && ok2 {
|
||
if bool1 != bool2 {
|
||
fmt.Println(" - Flag dependencies evaluated locally: ā
YES")
|
||
} else {
|
||
fmt.Println(" - Flag dependencies evaluated locally: ā NO")
|
||
}
|
||
} else {
|
||
fmt.Println(" - Flag dependencies evaluation: ā ļø INCONCLUSIVE (non-boolean results)")
|
||
}
|
||
} else {
|
||
fmt.Println(" - Flag dependencies evaluation: ā ļø INCONCLUSIVE (errors occurred)")
|
||
}
|
||
fmt.Println(" - Zero API calls needed: ā
YES (all evaluated locally)")
|
||
fmt.Println(" - Go SDK supports flag dependencies: ā
YES")
|
||
}
|