forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace.go
46 lines (41 loc) · 1.28 KB
/
namespace.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
package growthbook
import "encoding/json"
// Namespace specifies what part of a namespace an experiment
// includes. If two experiments are in the same namespace and their
// ranges don't overlap, they wil be mutually exclusive.
type Namespace struct {
ID string
Start float64
End float64
}
// Determine whether a user's ID lies within a given namespace.
func (namespace *Namespace) inNamespace(userID string) bool {
n := float64(hashFnv32a(userID+"__"+namespace.ID)%1000) / 1000
return n >= namespace.Start && n < namespace.End
}
// ParseNamespace creates a Namespace value from raw JSON input.
func ParseNamespace(data []byte) *Namespace {
array := []interface{}{}
err := json.Unmarshal(data, &array)
if err != nil {
logError("Failed parsing JSON input", "Namespace")
return nil
}
return BuildNamespace(array)
}
// BuildNamespace creates a Namespace value from a generic JSON value.
func BuildNamespace(val interface{}) *Namespace {
array, ok := val.([]interface{})
if !ok || len(array) != 3 {
logError("Invalid JSON data type", "Namespace")
return nil
}
id, ok1 := array[0].(string)
start, ok2 := array[1].(float64)
end, ok3 := array[2].(float64)
if !ok1 || !ok2 || !ok3 {
logError("Invalid JSON data type", "Namespace")
return nil
}
return &Namespace{id, start, end}
}