Skip to content

Commit 9703135

Browse files
Add Support for Target App Condition (#266)
kong test: statsig-io/kong#3431
1 parent 5fe3ba1 commit 9703135

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

client_initialize_response.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ func getClientInitializeResponse(
100100
hashAlgorithm = "sha256"
101101
}
102102

103+
var appId string
104+
if context.TargetAppID != "" {
105+
appId = context.TargetAppID
106+
} else {
107+
appId, _ = e.store.getAppIDForSDKKey(context.ClientKey)
108+
context.TargetAppID = appId
109+
}
110+
103111
evalResultToBaseResponse := func(name string, eval *evalResult) (string, baseSpecInitializeResponse) {
104112
hashedName := hashName(hashAlgorithm, name)
105113
result := baseSpecInitializeResponse{
@@ -221,13 +229,6 @@ func getClientInitializeResponse(
221229
return hashedName, result
222230
}
223231

224-
var appId string
225-
if context.TargetAppID != "" {
226-
appId = context.TargetAppID
227-
} else {
228-
appId, _ = e.store.getAppIDForSDKKey(context.ClientKey)
229-
}
230-
231232
filterByEntities := false
232233
gatesLookup := make(map[string]bool)
233234
configsLookup := make(map[string]bool)

evaluator.go

+6
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,12 @@ func (e *evaluator) evalCondition(user User, cond configCondition, depth int, co
563563
}
564564
case strings.EqualFold(condType, "unit_id"):
565565
value = getUnitID(user, cond.IDType)
566+
case strings.EqualFold(condType, "target_app"):
567+
if (context.ClientKey != "") {
568+
value = context.TargetAppID
569+
} else {
570+
value = e.store.getAppID()
571+
}
566572
default:
567573
return &evalResult{Unsupported: true}
568574
}

store.go

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type downloadConfigSpecResponse struct {
9494
HashedSDKKeyUsed string `json:"hashed_sdk_key_used,omitempty"`
9595
SDKFlags map[string]bool `json:"sdk_flags,omitempty"`
9696
SDKConfigs map[string]interface{} `json:"sdk_configs,omitempty"`
97+
AppID string `json:"app_id,omitempty"`
9798
}
9899

99100
type configEntities struct {
@@ -145,6 +146,7 @@ type store struct {
145146
isPolling bool
146147
bootstrapValues string
147148
sdkConfigs *SDKConfigs
149+
AppID string
148150
}
149151

150152
var syncOutdatedMax = 2 * time.Minute
@@ -299,6 +301,12 @@ func (s *store) getAppIDForSDKKey(clientKey string) (string, bool) {
299301
return appId, ok
300302
}
301303

304+
func (s *store) getAppID() string {
305+
s.mu.RLock()
306+
defer s.mu.RUnlock()
307+
return s.AppID
308+
}
309+
302310
func (s *store) getEntitiesForSDKKey(clientKey string) (configEntities, bool) {
303311
s.mu.RLock()
304312
defer s.mu.RUnlock()
@@ -496,6 +504,7 @@ func (s *store) setConfigSpecs(specs downloadConfigSpecResponse) (bool, bool) {
496504
s.hashedSDKKeysToAppID = specs.HashedSDKKeysToAppID
497505
s.hashedSDKKeysToEntities = specs.HashedSDKKeysToEntities
498506
s.lastSyncTime = specs.Time
507+
s.AppID = specs.AppID
499508
s.mu.Unlock()
500509
return true, true
501510
}

0 commit comments

Comments
 (0)