...
1 package growthbook
2
3 import "encoding/json"
4
5
6
7
8 type Namespace struct {
9 ID string
10 Start float64
11 End float64
12 }
13
14
15 func (namespace *Namespace) inNamespace(userID string) bool {
16 n := float64(hashFnv32a(userID+"__"+namespace.ID)%1000) / 1000
17 return n >= namespace.Start && n < namespace.End
18 }
19
20
21 func ParseNamespace(data []byte) *Namespace {
22 array := []interface{}{}
23 err := json.Unmarshal(data, &array)
24 if err != nil {
25 logError("Failed parsing JSON input", "Namespace")
26 return nil
27 }
28 return BuildNamespace(array)
29 }
30
31
32 func BuildNamespace(val interface{}) *Namespace {
33 array, ok := val.([]interface{})
34 if !ok || len(array) != 3 {
35 logError("Invalid JSON data type", "Namespace")
36 return nil
37 }
38 id, ok1 := array[0].(string)
39 start, ok2 := array[1].(float64)
40 end, ok3 := array[2].(float64)
41 if !ok1 || !ok2 || !ok3 {
42 logError("Invalid JSON data type", "Namespace")
43 return nil
44 }
45 return &Namespace{id, start, end}
46 }
47
View as plain text