Skip to content

Commit f385f8c

Browse files
committed
added working changes.
locally tested added working changes. locally tested
1 parent 514c67b commit f385f8c

35 files changed

+1348
-809
lines changed

go.mod

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ require (
88
gopkg.in/natefinch/lumberjack.v2 v2.2.1
99
)
1010

11-
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
11+
require (
12+
github.com/amplitude/analytics-go v1.0.1 // indirect
13+
github.com/google/uuid v1.3.0 // indirect
14+
github.com/spaolacci/murmur3 v1.1.0 // indirect
15+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
16+
)

go.sum

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1+
github.com/amplitude/analytics-go v1.0.1 h1:rrdC5VBctlJigSk0kw7ktwSijob/wyH4bop2SqWduCU=
2+
github.com/amplitude/analytics-go v1.0.1/go.mod h1:kAQG8OQ6aPOxZrEZ3+/NFCfxdYSyjqXZhgkjWFD3/vo=
3+
github.com/amplitude/experiment-go-server v1.3.0 h1:zsrbMiaI6yDwNesDREX6Wy83w4kZ0vqJEdxko69DmU8=
4+
github.com/amplitude/experiment-go-server v1.3.0/go.mod h1:5HERnGGohucx2mNr/0fLeQ9xFWNmeMbtzsxH2beB+hs=
15
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
26
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
37
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
9+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
410
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
511
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
612
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
713
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
814
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
915
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
16+
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
17+
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
1018
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
19+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
20+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
21+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
1222
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
23+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
24+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
25+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
26+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1327
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
1428
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1529
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1630
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
1731
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
18-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1932
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
34+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/cache/cache.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cache
2+
3+
import (
4+
"container/list"
5+
"time"
6+
)
7+
8+
type Item struct {
9+
Key string
10+
Value interface{}
11+
ExpiresAt time.Time
12+
}
13+
14+
type Cache struct {
15+
Capacity int
16+
TTL time.Duration
17+
cacheMap map[string]*list.Element
18+
cacheList *list.List
19+
}
20+
21+
func NewCache(capacity int, ttlMillis time.Duration) *Cache {
22+
return &Cache{
23+
Capacity: capacity,
24+
TTL: ttlMillis,
25+
cacheMap: make(map[string]*list.Element),
26+
cacheList: list.New(),
27+
}
28+
}
29+
30+
func (c *Cache) Get(key string) (interface{}, bool) {
31+
if elem, found := c.cacheMap[key]; found {
32+
cacheItem := elem.Value.(*Item)
33+
if time.Now().Before(cacheItem.ExpiresAt) {
34+
c.cacheList.MoveToFront(elem)
35+
return cacheItem.Value, true
36+
}
37+
c.removeElement(elem)
38+
}
39+
return nil, false
40+
}
41+
42+
func (c *Cache) Set(key string, value interface{}) {
43+
expiresAt := time.Now().Add(time.Millisecond * c.TTL)
44+
if elem, found := c.cacheMap[key]; found {
45+
c.cacheList.MoveToFront(elem)
46+
cacheItem := elem.Value.(*Item)
47+
cacheItem.Value = value
48+
cacheItem.ExpiresAt = expiresAt
49+
} else {
50+
if c.cacheList.Len() >= c.Capacity {
51+
// Evict the least recently used item
52+
backElem := c.cacheList.Back()
53+
if backElem != nil {
54+
c.removeElement(backElem)
55+
}
56+
}
57+
cacheItem := &Item{
58+
Key: key,
59+
Value: value,
60+
ExpiresAt: expiresAt,
61+
}
62+
newElem := c.cacheList.PushFront(cacheItem)
63+
c.cacheMap[key] = newElem
64+
}
65+
}
66+
67+
func (c *Cache) removeElement(elem *list.Element) {
68+
c.cacheList.Remove(elem)
69+
cacheItem := elem.Value.(*Item)
70+
delete(c.cacheMap, cacheItem.Key)
71+
}

internal/evaluation/context.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package evaluation
2+
3+
import "github.com/LambdaTest/lambda-featureflag-go-sdk/pkg/experiment"
4+
5+
func UserToContext(user *experiment.User) map[string]interface{} {
6+
if user == nil {
7+
return nil
8+
}
9+
context := make(map[string]interface{})
10+
userMap := make(map[string]interface{})
11+
if len(user.UserId) != 0 {
12+
userMap["user_id"] = user.UserId
13+
}
14+
if len(user.DeviceId) != 0 {
15+
userMap["device_id"] = user.DeviceId
16+
}
17+
if len(user.Country) != 0 {
18+
userMap["country"] = user.Country
19+
}
20+
if len(user.Region) != 0 {
21+
userMap["region"] = user.Region
22+
}
23+
if len(user.Dma) != 0 {
24+
userMap["dma"] = user.Dma
25+
}
26+
if len(user.City) != 0 {
27+
userMap["city"] = user.City
28+
}
29+
if len(user.Language) != 0 {
30+
userMap["language"] = user.Language
31+
}
32+
if len(user.Platform) != 0 {
33+
userMap["platform"] = user.Platform
34+
}
35+
if len(user.Version) != 0 {
36+
userMap["version"] = user.Version
37+
}
38+
if len(user.Os) != 0 {
39+
userMap["os"] = user.Os
40+
}
41+
if len(user.DeviceManufacturer) != 0 {
42+
userMap["device_manufacturer"] = user.DeviceManufacturer
43+
}
44+
if len(user.DeviceBrand) != 0 {
45+
userMap["device_brand"] = user.DeviceBrand
46+
}
47+
if len(user.DeviceModel) != 0 {
48+
userMap["device_model"] = user.DeviceModel
49+
}
50+
if len(user.Carrier) != 0 {
51+
userMap["carrier"] = user.Carrier
52+
}
53+
if len(user.Library) != 0 {
54+
userMap["library"] = user.Library
55+
}
56+
if len(user.UserProperties) != 0 {
57+
userMap["user_properties"] = user.UserProperties
58+
}
59+
context["user"] = userMap
60+
return context
61+
}

0 commit comments

Comments
 (0)