Skip to content

Commit 5d33445

Browse files
Introduce live variable class and getters (#35)
1 parent c8a57f9 commit 5d33445

29 files changed

+899
-55
lines changed

OptimizelySDKCore/OptimizelySDKCore.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
242C5560BF8316BF4ECE55C7 /* Pods_OptimizelySDKCoreiOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8766E217ECD3BEA353B080F5 /* Pods_OptimizelySDKCoreiOSTests.framework */; };
1111
2D5457521DC903410095E286 /* Pods_OptimizelySDKCoreiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2D5457511DC903410095E286 /* Pods_OptimizelySDKCoreiOS.framework */; };
1212
4FFB468BAFC670CF55514638 /* Pods_OptimizelySDKCoreTVOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A3A368275572C29A76DC80A /* Pods_OptimizelySDKCoreTVOSTests.framework */; };
13+
5E3C0E701DDF7A290025DB85 /* OPTLYVariable.m in Sources */ = {isa = PBXBuildFile; fileRef = 5ECBBE671DDE6A800028FF6B /* OPTLYVariable.m */; };
14+
5E3C0E711DDF7A3C0025DB85 /* OPTLYVariable.h in Headers */ = {isa = PBXBuildFile; fileRef = 5ECBBE661DDE6A800028FF6B /* OPTLYVariable.h */; settings = {ATTRIBUTES = (Public, ); }; };
15+
5ECBBE681DDE6A800028FF6B /* OPTLYVariable.h in Headers */ = {isa = PBXBuildFile; fileRef = 5ECBBE661DDE6A800028FF6B /* OPTLYVariable.h */; settings = {ATTRIBUTES = (Public, ); }; };
16+
5ECBBE691DDE6A800028FF6B /* OPTLYVariable.m in Sources */ = {isa = PBXBuildFile; fileRef = 5ECBBE671DDE6A800028FF6B /* OPTLYVariable.m */; };
1317
701BE1AAFCB6528297D6BCC4 /* Pods_OptimizelySDKCoreTVOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BE0C3BD9DC6186DB98A667C2 /* Pods_OptimizelySDKCoreTVOS.framework */; };
1418
73A56DE44E21EDBF3369AA44 /* Pods_OptimizelySDKCoreiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8B04E0101E180FAD8C9CA7A6 /* Pods_OptimizelySDKCoreiOS.framework */; };
1519
EA064BC71DD3FC8800DF7537 /* OPTLYQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = EA064BC51DD3FC8800DF7537 /* OPTLYQueue.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -230,6 +234,8 @@
230234
4AD98EC23436656DED35EBED /* Pods-OptimizelySDKCoreTVOS.beta.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKCoreTVOS.beta.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKCoreTVOS/Pods-OptimizelySDKCoreTVOS.beta.xcconfig"; sourceTree = "<group>"; };
231235
51B050141BF2E9277B39EEE8 /* Pods-OptimizelySDKCoreiOSTests.rc.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKCoreiOSTests.rc.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKCoreiOSTests/Pods-OptimizelySDKCoreiOSTests.rc.xcconfig"; sourceTree = "<group>"; };
232236
535C603DDFD996B277A0E064 /* Pods-OptimizelySDKCoreiOS.rc.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKCoreiOS.rc.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKCoreiOS/Pods-OptimizelySDKCoreiOS.rc.xcconfig"; sourceTree = "<group>"; };
237+
5ECBBE661DDE6A800028FF6B /* OPTLYVariable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OPTLYVariable.h; sourceTree = "<group>"; };
238+
5ECBBE671DDE6A800028FF6B /* OPTLYVariable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OPTLYVariable.m; sourceTree = "<group>"; };
233239
8766E217ECD3BEA353B080F5 /* Pods_OptimizelySDKCoreiOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_OptimizelySDKCoreiOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
234240
8B04E0101E180FAD8C9CA7A6 /* Pods_OptimizelySDKCoreiOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_OptimizelySDKCoreiOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
235241
924C83E5511EF3E43FC015AF /* Pods-OptimizelySDKCoreTVOSTests.rc.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OptimizelySDKCoreTVOSTests.rc.xcconfig"; path = "../Pods/Target Support Files/Pods-OptimizelySDKCoreTVOSTests/Pods-OptimizelySDKCoreTVOSTests.rc.xcconfig"; sourceTree = "<group>"; };
@@ -481,6 +487,8 @@
481487
EA2FAA7F1DC6F57100B1D81B /* OPTLYProjectConfig.m */,
482488
EA2FAA801DC6F57100B1D81B /* OPTLYTrafficAllocation.h */,
483489
EA2FAA811DC6F57100B1D81B /* OPTLYTrafficAllocation.m */,
490+
5ECBBE661DDE6A800028FF6B /* OPTLYVariable.h */,
491+
5ECBBE671DDE6A800028FF6B /* OPTLYVariable.m */,
484492
EA2FAA821DC6F57100B1D81B /* OPTLYVariation.h */,
485493
EA2FAA831DC6F57100B1D81B /* OPTLYVariation.m */,
486494
);
@@ -653,6 +661,7 @@
653661
EA2FAB281DC6F59100B1D81B /* OPTLYBucketer.h in Headers */,
654662
EA2FAB201DC6F58800B1D81B /* OPTLYBuilder.h in Headers */,
655663
EA2FAB521DC6F5BE00B1D81B /* OPTLYLogger.h in Headers */,
664+
5ECBBE681DDE6A800028FF6B /* OPTLYVariable.h in Headers */,
656665
EA2FAB581DC6F5BE00B1D81B /* OPTLYLoggerMessages.h in Headers */,
657666
EA2FAB181DC6F57200B1D81B /* OPTLYVariation.h in Headers */,
658667
EA2FAB821DC6FA7D00B1D81B /* OptimizelySDKCore.h in Headers */,
@@ -698,6 +707,7 @@
698707
EA2FAB331DC6F59D00B1D81B /* OPTLYErrorHandler.h in Headers */,
699708
EA2FAB211DC6F58800B1D81B /* OPTLYBuilder.h in Headers */,
700709
EA2FAB191DC6F57200B1D81B /* OPTLYVariation.h in Headers */,
710+
5E3C0E711DDF7A3C0025DB85 /* OPTLYVariable.h in Headers */,
701711
EA2FAB531DC6F5BE00B1D81B /* OPTLYLogger.h in Headers */,
702712
EA2FAB591DC6F5BE00B1D81B /* OPTLYLoggerMessages.h in Headers */,
703713
EA2FAB831DC6FCD300B1D81B /* OptimizelySDKCore.h in Headers */,
@@ -1098,6 +1108,7 @@
10981108
EA2FAC221DC6FFC600B1D81B /* OPTLYErrorHandlerMessages.m in Sources */,
10991109
EA2FAC241DC6FFC600B1D81B /* OPTLYEventBuilder.m in Sources */,
11001110
EA2FAC251DC6FFC600B1D81B /* OPTLYLogger.m in Sources */,
1111+
5ECBBE691DDE6A800028FF6B /* OPTLYVariable.m in Sources */,
11011112
EA2FAC261DC6FFC600B1D81B /* OPTLYLoggerMessages.m in Sources */,
11021113
EA2FAC291DC6FFC600B1D81B /* OPTLYLog.m in Sources */,
11031114
EA2FAC2A1DC6FFC600B1D81B /* OPTLYValidator.m in Sources */,
@@ -1162,6 +1173,7 @@
11621173
EA2FABFD1DC6FFA100B1D81B /* OPTLYErrorHandlerMessages.m in Sources */,
11631174
EA2FABFF1DC6FFA100B1D81B /* OPTLYEventBuilder.m in Sources */,
11641175
EA2FAC001DC6FFA100B1D81B /* OPTLYLogger.m in Sources */,
1176+
5E3C0E701DDF7A290025DB85 /* OPTLYVariable.m in Sources */,
11651177
EA2FAC011DC6FFA100B1D81B /* OPTLYLoggerMessages.m in Sources */,
11661178
EA2FAC041DC6FFA100B1D81B /* OPTLYLog.m in Sources */,
11671179
EA2FAC051DC6FFA100B1D81B /* OPTLYValidator.m in Sources */,

OptimizelySDKCore/OptimizelySDKCore/OPTLYDatafileKeys.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@ extern NSString * const OPTLYDatafileKeysTrafficAllocationEndOfRange;
6161
// Variations
6262
extern NSString * const OPTLYDatafileKeysVariationId;
6363
extern NSString * const OPTLYDatafileKeysVariationKey;
64+
extern NSString * const OPTLYDatafileKeysVariationVariables;
6465
// Conditions
6566
extern NSString * const OPTLYDatafileKeysConditionName;
6667
extern NSString * const OPTLYDatafileKeysConditionType;
6768
extern NSString * const OPTLYDatafileKeysConditionValue;
6869
extern NSString * const OPTLYDatafileKeysAndCondition;
6970
extern NSString * const OPTLYDatafileKeysNotCondition;
7071
extern NSString * const OPTLYDatafileKeysOrCondition;
72+
// Live Variables
73+
extern NSString * const OPTLYDatafileKeysVariableId;
74+
extern NSString * const OPTLYDatafileKeysVariableKey;
75+
extern NSString * const OPTLYDatafileKeysVariableType;
76+
extern NSString * const OPTLYDatafileKeysVariableValue;
7177

7278
@interface OPTLYDatafileKeys : NSObject
7379

OptimizelySDKCore/OptimizelySDKCore/OPTLYDatafileKeys.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,19 @@
5757
// Variations
5858
NSString * const OPTLYDatafileKeysVariationId = @"id";
5959
NSString * const OPTLYDatafileKeysVariationKey = @"key";
60+
NSString * const OPTLYDatafileKeysVariationVariables = @"variables";
6061
// Conditions
6162
NSString * const OPTLYDatafileKeysConditionName = @"name";
6263
NSString * const OPTLYDatafileKeysConditionType = @"type";
6364
NSString * const OPTLYDatafileKeysConditionValue = @"value";
6465
NSString * const OPTLYDatafileKeysAndCondition = @"and";
6566
NSString * const OPTLYDatafileKeysNotCondition = @"not";
6667
NSString * const OPTLYDatafileKeysOrCondition = @"or";
68+
// Live Variables
69+
NSString * const OPTLYDatafileKeysVariableId = @"id";
70+
NSString * const OPTLYDatafileKeysVariableKey = @"key";
71+
NSString * const OPTLYDatafileKeysVariableType = @"type";
72+
NSString * const OPTLYDatafileKeysVariableValue = @"value";
6773

6874
@implementation OPTLYDatafileKeys
6975
@end

OptimizelySDKCore/OptimizelySDKCore/OPTLYErrorHandlerMessages.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extern NSString * const OPTLYErrorHandlerMessagesBucketingIdInvalid;
4343
extern NSString * const OPTLYErrorHandlerMessagesTrafficAllocationUnknown;
4444
extern NSString * const OPTLYErrorHandlerMessagesEventDispatchFailed;
4545
extern NSString * const OPTLYErrorHandlerMessagesConfigInvalid;
46+
extern NSString * const OPTLYErrorHandlerMessagesLiveVariableKeyUnknown;
4647

4748
extern NSString * const OPTLYErrorHandlerMessagesManagerBuilderInvalid;
4849

OptimizelySDKCore/OptimizelySDKCore/OPTLYErrorHandlerMessages.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
NSString * const OPTLYErrorHandlerMessagesTrafficAllocationUnknown = @"Traffic allocations for %@ does not exist in datafile."; // experiment or group id
4343
NSString * const OPTLYErrorHandlerMessagesEventDispatchFailed = @"Event %@ failed to dispatch.";
4444
NSString * const OPTLYErrorHandlerMessagesConfigInvalid = @"Project config is nil or invalid";
45+
NSString * const OPTLYErrorHandlerMessagesLiveVariableKeyUnknown = @"Live variable key %@ is not in datafile."; // live variable key
4546

4647
// Manager Errors
4748
NSString *const OPTLYErrorHandlerMessagesManagerBuilderInvalid = @"Provided OPTLYManagerBuilder object is invalid";

OptimizelySDKCore/OptimizelySDKCore/OPTLYLoggerMessages.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ extern NSString *const OPTLYLoggerMessagesAttributeUnknownForAttributeKey;
7070
extern NSString *const OPTLYLoggerMessagesAudienceUnknownForAudienceId;
7171
extern NSString *const OPTLYLoggerMessagesGroupUnknownForGroupId;
7272
extern NSString *const OPTLYLoggerMessagesEventDispatcherInterval;
73+
extern NSString *const OPTLYLoggerMessagesVariableUnknownForVariableKey;
74+
extern NSString *const OPTLYLoggerMessagesNoVariationFoundForExperimentWithLiveVariable;
7375

7476
// ---- debug ----
7577
extern NSString *const OPTLYLoggerMessagesImpressionDispatching;

OptimizelySDKCore/OptimizelySDKCore/OPTLYLoggerMessages.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
NSString *const OPTLYLoggerMessagesNotTrackedDispatchFailed = @"Not tracking event %@ for user %@. Event dispatch failed."; // event key, userId
5252
NSString *const OPTLYLoggerMessagesExperimentIdUnknown = @"Experiment id for %@ is not in the datafile."; // experiment key
5353
NSString *const OPTLYLoggerMessagesEventIdUnknown = @"Event id %@ is not in the datafile."; //event key
54+
NSString *const OPTLYLoggerMessagesNoVariationFoundForExperimentWithLiveVariable = @"Variation not found for user ID: %@ with experiment key: %@ containing live variable: %@.";
5455
// conversion and tracking event creation errors
5556
NSString *const OPTLYLoggerMessagesVariationtNameInvalid = @"Variation name is not valid.";
5657
NSString *const OPTLYLoggerMessagesUserIdInvalid = @"User id is not valid.";
@@ -68,6 +69,7 @@
6869
NSString *const OPTLYLoggerMessagesAttributeUnknownForAttributeKey = @"Attribute not found for attribute key: %@."; // attribute key
6970
NSString *const OPTLYLoggerMessagesAudienceUnknownForAudienceId = @"Audience not found for audience id: %@."; // audience id
7071
NSString *const OPTLYLoggerMessagesGroupUnknownForGroupId = @"Group not found for group id: %@."; // group id
72+
NSString *const OPTLYLoggerMessagesVariableUnknownForVariableKey = @"Live variable not found for variable key: %@."; // live variable key
7173

7274
NSString *const OPTLYLoggerMessagesEventDispatcherInterval = @"Event dispatcher interval set: %ld";
7375

OptimizelySDKCore/OptimizelySDKCore/OPTLYProjectConfig.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#import <Foundation/Foundation.h>
1818
#import <JSONModel/JSONModelLib.h>
1919

20-
@class OPTLYExperiment, OPTLYGroup, OPTLYEvent, OPTLYAttribute, OPTLYAudience, OPTLYVariation, OPTLYBucketer;
21-
@protocol OPTLYExperiment, OPTLYEvent, OPTLYAudience, OPTLYAttribute, OPTLYGroup, OPTLYExperiment, OPTLYLogger, OPTLYErrorHandler, OPTLYBucketer;
20+
@class OPTLYExperiment, OPTLYGroup, OPTLYEvent, OPTLYAttribute, OPTLYAudience, OPTLYVariation, OPTLYVariable, OPTLYBucketer;
21+
@protocol OPTLYExperiment, OPTLYEvent, OPTLYAudience, OPTLYAttribute, OPTLYGroup, OPTLYVariable, OPTLYLogger, OPTLYErrorHandler, OPTLYBucketer;
2222

2323
/*
2424
This class represents all the data contained in the project datafile
@@ -45,6 +45,8 @@
4545
@property (nonatomic, strong, nonnull) NSArray<OPTLYAttribute> *attributes;
4646
/// List of group objects
4747
@property (nonatomic, strong, nonnull) NSArray<OPTLYGroup> *groups;
48+
/// List of live variable objects
49+
@property (nonatomic, strong, nonnull) NSArray<OPTLYVariable> *variables;
4850

4951
/// a comprehensive list of experiments that includes experiments being whitelisted (in Groups)
5052
@property (nonatomic, strong, nullable) NSArray<OPTLYExperiment, Ignore> *allExperiments;
@@ -98,6 +100,11 @@
98100
*/
99101
- (nullable OPTLYAudience *)getAudienceForId:(nonnull NSString *)audienceId;
100102

103+
/**
104+
* Get a variable for a given live variable key.
105+
*/
106+
- (nullable OPTLYVariable *)getVariableForVariableKey:(nonnull NSString *)variableKey;
107+
101108
/**
102109
* Get variation for experiment and user ID with user attributes.
103110
*/

OptimizelySDKCore/OptimizelySDKCore/OPTLYProjectConfig.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "OPTLYLogger.h"
2727
#import "OPTLYProjectConfig.h"
2828
#import "OPTLYValidator.h"
29+
#import "OPTLYVariable.h"
2930

3031
NSString * const kClientEngine = @"objective-c-sdk-core";
3132

@@ -39,6 +40,7 @@ @interface OPTLYProjectConfig()
3940
@property (nonatomic, strong) NSDictionary<NSString *, NSString *><Ignore> *experimentKeyToExperimentIdMap;
4041
@property (nonatomic, strong) NSDictionary<NSString *, OPTLYGroup *><Ignore> *groupIdToGroupMap;
4142
@property (nonatomic, strong) NSDictionary<NSString *, OPTLYAttribute *><Ignore> *attributeKeyToAttributeMap;
43+
@property (nonatomic, strong) NSDictionary<NSString *, OPTLYVariable *><Ignore> *variableKeyToVariableMap;
4244

4345
@end
4446

@@ -185,6 +187,15 @@ - (OPTLYGroup *)getGroupForGroupId:(NSString *)groupId {
185187
return group;
186188
}
187189

190+
- (OPTLYVariable *)getVariableForVariableKey:(NSString *)variableKey {
191+
OPTLYVariable *variable = self.variableKeyToVariableMap[variableKey];
192+
if (!variable) {
193+
NSString *logMessage = [NSString stringWithFormat:OPTLYLoggerMessagesVariableUnknownForVariableKey, variableKey];
194+
[self.logger logMessage:logMessage withLevel:OptimizelyLogLevelWarning];
195+
}
196+
return variable;
197+
}
198+
188199
#pragma mark -- Property Getters --
189200

190201
- (NSArray *)allExperiments
@@ -261,6 +272,13 @@ - (NSDictionary *)eventKeyToEventMap {
261272
return _groupIdToGroupMap;
262273
}
263274

275+
- (NSDictionary<NSString *, OPTLYVariable *> *)variableKeyToVariableMap {
276+
if (!_variableKeyToVariableMap) {
277+
_variableKeyToVariableMap = [self generateVariableKeyToVariableMap];
278+
}
279+
return _variableKeyToVariableMap;
280+
}
281+
264282
#pragma mark -- Generate Mappings --
265283

266284
- (NSDictionary *)generateAudienceIdToAudienceMap
@@ -342,6 +360,14 @@ - (NSDictionary *)generateAttributeToKeyMap
342360
return [NSDictionary dictionaryWithDictionary:map];
343361
}
344362

363+
- (NSDictionary<NSString *, OPTLYVariable *> *)generateVariableKeyToVariableMap {
364+
NSMutableDictionary *map = [[NSMutableDictionary alloc] init];
365+
for (OPTLYVariable *variable in self.variables) {
366+
map[variable.variableKey] = variable;
367+
}
368+
return [NSDictionary dictionaryWithDictionary:map];
369+
}
370+
345371
# pragma mark - Helper Methods
346372

347373
- (OPTLYVariation *)getVariationForExperiment:(NSString *)experimentKey
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/****************************************************************************
2+
* Copyright 2016, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
17+
#import <Foundation/Foundation.h>
18+
#import <JSONModel/JSONModelLib.h>
19+
20+
/**
21+
* This class is a representation of an Optimizely live variable.
22+
*/
23+
24+
@protocol OPTLYVariable
25+
@end
26+
27+
@interface OPTLYVariable : JSONModel
28+
29+
/// The variable's ID.
30+
@property (nonatomic, strong) NSString *variableId;
31+
/// The variable's Key.
32+
@property (nonatomic, strong) NSString *variableKey;
33+
/// The variable's type.
34+
@property (nonatomic, strong) NSString *type;
35+
/// The variable's default value.
36+
@property (nonatomic, strong) NSString *value;
37+
38+
@end

0 commit comments

Comments
 (0)