-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathnamespace.go
46 lines (38 loc) · 1.02 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"
"fmt"
)
// 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
}
func (namespace *Namespace) UnmarshalJSON(data []byte) error {
arr := []any{}
err := json.Unmarshal(data, &arr)
if err != nil {
return err
}
if len(arr) != 3 {
return fmt.Errorf("invalid namespace format: %v", arr)
}
id, ok1 := arr[0].(string)
start, ok2 := arr[1].(float64)
end, ok3 := arr[2].(float64)
if !ok1 || !ok2 || !ok3 {
return fmt.Errorf("invalid namespace format: %v", arr)
}
namespace.Id = id
namespace.Start = start
namespace.End = end
return nil
}