forked from albertoleal/features
/
model.go
63 lines (51 loc) · 1.2 KB
/
model.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2015 Features authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package engine
import (
"errors"
"hash/crc32"
)
type User struct {
Id string `json:"id"`
}
type FeatureFlag struct {
Enabled bool `json:"enabled"`
Key string `json:"key"`
Users []*User `json:"users,omitempty"`
Percentage uint32 `json:"percentage"`
}
func (ff *FeatureFlag) ContainsUser(user *User) bool {
for _, u := range ff.Users {
if u.Id == user.Id {
return true
}
}
return false
}
func (ff *FeatureFlag) UserInPercentage(user *User) bool {
return crc32.ChecksumIEEE([]byte(user.Id))%100 <= ff.Percentage
}
func NewFeatureFlag(key string, enabled bool, users []*User, percentage uint32) (*FeatureFlag, error) {
if key == "" {
return nil, errors.New("Key cannot be empty.")
}
return &FeatureFlag{
Key: key,
Enabled: enabled,
Users: users,
Percentage: percentage,
}, nil
}
type FeatureFlagKey struct {
Key string
}
type NotFoundError struct {
Message string
}
func (n *NotFoundError) Error() string {
if n.Message != "" {
return n.Message
}
return "Feature flag not found."
}