Skip to content

Commit 17c1db5

Browse files
Added OPTLYVariationVariable JSON model to represent the variable object scoped within a variation. It's better to enforce a JSON model type than to rely on a dictionary because the JSON deserializer will help throw an error if the keys are missing or invalid. Also updated the live variable getter return values to return the correct types.
1 parent b6d2a84 commit 17c1db5

File tree

9 files changed

+123
-24
lines changed

9 files changed

+123
-24
lines changed

OptimizelySDKCore/OptimizelySDKCore.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
EA2C242E1DE6A2470063ADA0 /* OPTLYProjectConfigBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = EA2C242B1DE6A2470063ADA0 /* OPTLYProjectConfigBuilder.h */; settings = {ATTRIBUTES = (Public, ); }; };
2727
EA2C242F1DE6A2470063ADA0 /* OPTLYProjectConfigBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = EA2C242C1DE6A2470063ADA0 /* OPTLYProjectConfigBuilder.m */; };
2828
EA2C24301DE6A2470063ADA0 /* OPTLYProjectConfigBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = EA2C242C1DE6A2470063ADA0 /* OPTLYProjectConfigBuilder.m */; };
29+
EA2C24331DE7887C0063ADA0 /* OPTLYVariationVariable.h in Headers */ = {isa = PBXBuildFile; fileRef = EA2C24311DE7887C0063ADA0 /* OPTLYVariationVariable.h */; };
30+
EA2C24341DE7887C0063ADA0 /* OPTLYVariationVariable.h in Headers */ = {isa = PBXBuildFile; fileRef = EA2C24311DE7887C0063ADA0 /* OPTLYVariationVariable.h */; };
31+
EA2C24351DE7887C0063ADA0 /* OPTLYVariationVariable.m in Sources */ = {isa = PBXBuildFile; fileRef = EA2C24321DE7887C0063ADA0 /* OPTLYVariationVariable.m */; };
32+
EA2C24361DE7887C0063ADA0 /* OPTLYVariationVariable.m in Sources */ = {isa = PBXBuildFile; fileRef = EA2C24321DE7887C0063ADA0 /* OPTLYVariationVariable.m */; };
2933
EA2FA8071DC5DBA000B1D81B /* OptimizelySDKCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA2FA7FE1DC5DBA000B1D81B /* OptimizelySDKCore.framework */; };
3034
EA2FA8271DC5DBB100B1D81B /* OptimizelySDKCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA2FA81E1DC5DBB100B1D81B /* OptimizelySDKCore.framework */; };
3135
EA2FA9771DC6F3CF00B1D81B /* murmur3.h in Headers */ = {isa = PBXBuildFile; fileRef = EA2FA9701DC6F3CF00B1D81B /* murmur3.h */; };
@@ -262,6 +266,8 @@
262266
EA064BCB1DD3FC9F00DF7537 /* OPTLYQueueTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OPTLYQueueTest.m; sourceTree = "<group>"; };
263267
EA2C242B1DE6A2470063ADA0 /* OPTLYProjectConfigBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OPTLYProjectConfigBuilder.h; sourceTree = "<group>"; };
264268
EA2C242C1DE6A2470063ADA0 /* OPTLYProjectConfigBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OPTLYProjectConfigBuilder.m; sourceTree = "<group>"; };
269+
EA2C24311DE7887C0063ADA0 /* OPTLYVariationVariable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OPTLYVariationVariable.h; sourceTree = "<group>"; };
270+
EA2C24321DE7887C0063ADA0 /* OPTLYVariationVariable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OPTLYVariationVariable.m; sourceTree = "<group>"; };
265271
EA2FA7FE1DC5DBA000B1D81B /* OptimizelySDKCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OptimizelySDKCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
266272
EA2FA8061DC5DBA000B1D81B /* OptimizelySDKCoreiOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OptimizelySDKCoreiOSTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
267273
EA2FA81E1DC5DBB100B1D81B /* OptimizelySDKCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OptimizelySDKCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -503,6 +509,8 @@
503509
5ECBBE671DDE6A800028FF6B /* OPTLYVariable.m */,
504510
EA2FAA821DC6F57100B1D81B /* OPTLYVariation.h */,
505511
EA2FAA831DC6F57100B1D81B /* OPTLYVariation.m */,
512+
EA2C24311DE7887C0063ADA0 /* OPTLYVariationVariable.h */,
513+
EA2C24321DE7887C0063ADA0 /* OPTLYVariationVariable.m */,
506514
);
507515
name = "Data Models";
508516
sourceTree = "<group>";
@@ -710,6 +718,7 @@
710718
EA2FAAEE1DC6F57200B1D81B /* OPTLYEventRelatedEvent.h in Headers */,
711719
EA2FAAAC1DC6F57200B1D81B /* OPTLYEvent.h in Headers */,
712720
EA2FAAD01DC6F57200B1D81B /* OPTLYEventFeature.h in Headers */,
721+
EA2C24331DE7887C0063ADA0 /* OPTLYVariationVariable.h in Headers */,
713722
EA2FAABE1DC6F57200B1D81B /* OPTLYEventDecision.h in Headers */,
714723
EA2FAAA61DC6F57200B1D81B /* OPTLYDecisionEventTicket.h in Headers */,
715724
EA2FAAC41DC6F57200B1D81B /* OPTLYEventDecisionTicket.h in Headers */,
@@ -758,6 +767,7 @@
758767
EA2FAB071DC6F57200B1D81B /* OPTLYGroup.h in Headers */,
759768
EA2FAAE91DC6F57200B1D81B /* OPTLYEventParameterKeys.h in Headers */,
760769
EA2FAAFB1DC6F57200B1D81B /* OPTLYEventView.h in Headers */,
770+
EA2C24341DE7887C0063ADA0 /* OPTLYVariationVariable.h in Headers */,
761771
EA2FAABF1DC6F57200B1D81B /* OPTLYEventDecision.h in Headers */,
762772
EA2FAAD71DC6F57200B1D81B /* OPTLYEventHeader.h in Headers */,
763773
EA2FAAD11DC6F57200B1D81B /* OPTLYEventFeature.h in Headers */,
@@ -1142,6 +1152,7 @@
11421152
5ECBBE691DDE6A800028FF6B /* OPTLYVariable.m in Sources */,
11431153
EA2FAC261DC6FFC600B1D81B /* OPTLYLoggerMessages.m in Sources */,
11441154
EA2FAC291DC6FFC600B1D81B /* OPTLYLog.m in Sources */,
1155+
EA2C24351DE7887C0063ADA0 /* OPTLYVariationVariable.m in Sources */,
11451156
EA2FAC2A1DC6FFC600B1D81B /* OPTLYValidator.m in Sources */,
11461157
);
11471158
runOnlyForDeploymentPostprocessing = 0;
@@ -1209,6 +1220,7 @@
12091220
5E3C0E701DDF7A290025DB85 /* OPTLYVariable.m in Sources */,
12101221
EA2FAC011DC6FFA100B1D81B /* OPTLYLoggerMessages.m in Sources */,
12111222
EA2FAC041DC6FFA100B1D81B /* OPTLYLog.m in Sources */,
1223+
EA2C24361DE7887C0063ADA0 /* OPTLYVariationVariable.m in Sources */,
12121224
EA2FAC051DC6FFA100B1D81B /* OPTLYValidator.m in Sources */,
12131225
);
12141226
runOnlyForDeploymentPostprocessing = 0;

OptimizelySDKCore/OptimizelySDKCore/OPTLYDatafileKeys.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ extern NSString * const OPTLYDatafileKeysVariableId;
7474
extern NSString * const OPTLYDatafileKeysVariableKey;
7575
extern NSString * const OPTLYDatafileKeysVariableType;
7676
extern NSString * const OPTLYDatafileKeysVariableValue;
77+
// Variation Live Variable
78+
extern NSString * const OPTLYDatafileKeysVariationVariableId;
79+
extern NSString * const OPTLYDatafileKeysVariationVariableValue;
7780

7881
@interface OPTLYDatafileKeys : NSObject
7982

OptimizelySDKCore/OptimizelySDKCore/OPTLYDatafileKeys.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
NSString * const OPTLYDatafileKeysVariableKey = @"key";
7171
NSString * const OPTLYDatafileKeysVariableType = @"type";
7272
NSString * const OPTLYDatafileKeysVariableValue = @"value";
73+
// Variation Live Variable
74+
NSString * const OPTLYDatafileKeysVariationVariableId = @"id";
75+
NSString * const OPTLYDatafileKeysVariationVariableValue = @"value";
7376

7477
@implementation OPTLYDatafileKeys
7578
@end

OptimizelySDKCore/OptimizelySDKCore/OPTLYVariable.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@
1818
#import <JSONModel/JSONModelLib.h>
1919

2020
/**
21-
* This class is a representation of an Optimizely live variable.
21+
* This class is a representation of an Optimizely live variable scoped within a project:
22+
* "variables": [
23+
* {
24+
* "id": "6384881128",
25+
* "key": "someString",
26+
* "type": "string",
27+
* "value": "defaultStringValue"
28+
* },
29+
* ...
30+
* ]
2231
*/
2332

2433
@protocol OPTLYVariable

OptimizelySDKCore/OptimizelySDKCore/OPTLYVariation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* This class is a representation of an Optimizely variation.
2222
*/
23-
23+
@protocol OPTLYVariationVariable;
2424
@protocol OPTLYVariation
2525
@end
2626

@@ -32,6 +32,6 @@
3232
@property (nonatomic, strong) NSString *variationKey;
3333
/// The array containing the variation's live variable information -- variable ID and variable value.
3434
/// TODO: Make variables required and add in NSDictionary * back into NSArray
35-
@property (nonatomic, strong) NSArray<Optional> *variables;
35+
@property (nonatomic, strong) NSArray<OPTLYVariationVariable, Optional> *variables;
3636

3737
@end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 scoped within a variation:
22+
* "variations": [
23+
* {
24+
* "id": "6451680205",
25+
* "key": "a",
26+
* "variables": [
27+
* {
28+
* "id": "73483201090",
29+
* "value": "testValue"
30+
* },
31+
* ...
32+
* ]
33+
* }
34+
*/
35+
36+
@protocol OPTLYVariationVariable
37+
@end
38+
39+
@interface OPTLYVariationVariable : JSONModel
40+
41+
/// The variable's ID.
42+
@property (nonatomic, strong) NSString *variableId;
43+
/// The variable's assigned value within that variation
44+
@property (nonatomic, strong) NSString *value;
45+
46+
@end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 "OPTLYVariationVariable.h"
18+
#import "OPTLYDatafileKeys.h"
19+
20+
@implementation OPTLYVariationVariable
21+
+ (JSONKeyMapper*)keyMapper
22+
{
23+
return [[JSONKeyMapper alloc] initWithDictionary:@{ OPTLYDatafileKeysVariationVariableId : @"variableId",
24+
}];
25+
}
26+
27+
@end

OptimizelySDKCore/OptimizelySDKCore/Optimizely.m

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#import "Optimizely.h"
1818
#import "OPTLYBucketer.h"
19+
#import "OPTLYDatafileKeys.h"
1920
#import "OPTLYDecisionEventTicket.h"
2021
#import "OPTLYErrorHandler.h"
2122
#import "OPTLYEventBuilder.h"
@@ -30,10 +31,7 @@
3031
#import "OPTLYValidator.h"
3132
#import "OPTLYVariable.h"
3233
#import "OPTLYVariation.h"
33-
34-
static NSString *const kExperimentKey = @"experimentKey";
35-
static NSString *const kId = @"id";
36-
static NSString *const kValue = @"value";
34+
#import "OPTLYVariationVariable.h"
3735

3836
@implementation Optimizely
3937

@@ -203,7 +201,7 @@ - (void)trackEvent:(NSString *)eventKey
203201
}];
204202
}
205203

206-
#pragma mark Live variable getters
204+
#pragma mark - Live variable getters
207205

208206
/**
209207
* Finds experiment(s) that contain the live variable.
@@ -222,10 +220,10 @@ - (NSArray *)getExperimentKeysForLiveVariable:(NSString *)variableId
222220
OPTLYVariation *firstVariation = [experiment.variations objectAtIndex:0];
223221
NSArray *firstVariationVariables = firstVariation.variables;
224222

225-
for (OPTLYVariable *firstVariationVariable in firstVariationVariables) {
226-
NSString *firstVariationVariableId = [firstVariationVariable valueForKey:kId];
223+
for (OPTLYVariationVariable *firstVariationVariable in firstVariationVariables) {
224+
NSString *firstVariationVariableId = firstVariationVariable.variableId;
227225
if ([firstVariationVariableId isEqualToString:variableId]) {
228-
NSString *experimentKey = [experiment valueForKey:kExperimentKey];
226+
NSString *experimentKey = experiment.experimentKey;
229227
[experimentsForLiveVariable addObject:experimentKey];
230228
}
231229
}
@@ -269,10 +267,10 @@ - (OPTLYVariation *)getVariationForExperiment:(NSString *)experimentKey
269267
*/
270268
- (NSString *)getValueForLiveVariable:(NSString *)variableId
271269
variation:(OPTLYVariation *)variation {
272-
for (OPTLYVariable *variable in variation.variables) {
273-
NSString *variationVariableId = [variable valueForKey:kId];
270+
for (OPTLYVariationVariable *variable in variation.variables) {
271+
NSString *variationVariableId = variable.variableId;
274272
if ([variationVariableId isEqualToString:variableId]) {
275-
NSString *variableValue = [variable valueForKey:kValue];
273+
NSString *variableValue = variable.value;
276274
return variableValue;
277275
}
278276
}
@@ -324,58 +322,59 @@ - (BOOL)getVariableBool:(nonnull NSString *)variableKey
324322
userId:(nonnull NSString *)userId
325323
attributes:(nullable NSDictionary *)attributes
326324
error:(NSError * _Nullable * _Nullable)error {
325+
BOOL variableValue = false;
327326
NSString *variableValueStringOrNil = [self getVariableString:variableKey
328327
activateExperiments:activateExperiments
329328
userId:userId
330329
attributes:attributes
331330
error:error];
332331

333332
if (variableValueStringOrNil != nil) {
334-
BOOL variableValue = [[variableValueStringOrNil lowercaseString] boolValue];
335-
return variableValue;
333+
variableValue = [variableValueStringOrNil boolValue];
336334
}
337335

338-
return variableValueStringOrNil;
336+
return variableValue;
339337
}
340338

341339
- (NSInteger)getVariableInteger:(nonnull NSString *)variableKey
342340
activateExperiments:(bool)activateExperiments
343341
userId:(nonnull NSString *)userId
344342
attributes:(nullable NSDictionary *)attributes
345343
error:(NSError * _Nullable * _Nullable)error {
344+
NSInteger variableValue = 0;
346345
NSString *variableValueStringOrNil = [self getVariableString:variableKey
347346
activateExperiments:activateExperiments
348347
userId:userId
349348
attributes:attributes
350349
error:error];
351350

352351
if (variableValueStringOrNil != nil) {
353-
NSInteger variableValue = [variableValueStringOrNil intValue];
354-
return variableValue;
352+
variableValue = [variableValueStringOrNil integerValue];
355353
}
356354

357-
return variableValueStringOrNil;
355+
return variableValue;
358356
}
359357

360358
- (double)getVariableFloat:(nonnull NSString *)variableKey
361359
activateExperiments:(bool)activateExperiments
362360
userId:(nonnull NSString *)userId
363361
attributes:(nullable NSDictionary *)attributes
364362
error:(NSError * _Nullable * _Nullable)error {
363+
double variableValue = 0.0;
365364
NSString *variableValueStringOrNil = [self getVariableString:variableKey
366365
activateExperiments:activateExperiments
367366
userId:userId
368367
attributes:attributes
369368
error:error];
370369

371370
if (variableValueStringOrNil != nil) {
372-
double variableValue = [variableValueStringOrNil doubleValue];
373-
return variableValue;
371+
variableValue = [variableValueStringOrNil doubleValue];
374372
}
375373

376-
return 0;
374+
return variableValue;
377375
}
378376

377+
# pragma mark - Helper methods
379378
// log and propagate error for a track failure
380379
- (void)handleErrorLogsForTrackEvent:(NSString *)eventKey
381380
userId:(NSString *)userId

OptimizelySDKUserProfile/OptimizelySDKUserProfile/OPTLYUserProfile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
/**
3737
* Default initializer for Optimizely User Profile object
3838
*/
39-
- (instancetype)init;
39+
- (nullable instancetype)init;
4040

4141
/**
4242
* Saves a user ID's project-to-experiment-to-variation mapping.

0 commit comments

Comments
 (0)