Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MC-2003] Custom template dictionary swift bool #370

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CleverTapSDK/CleverTap.m
Original file line number Diff line number Diff line change
Expand Up @@ -4331,9 +4331,9 @@ - (void)syncCustomTemplates:(BOOL)isProduction {
- (void)syncWithBlock:(void(^)(void))syncBlock methodName:(NSString *)methodName isProduction:(BOOL)isProduction {
if (isProduction) {
#if DEBUG
CleverTapLogInfo(_config.logLevel, @"%@: Calling %@: with isProduction:YES from Debug configuration/build. Use syncVariables in this case", self, methodName);
CleverTapLogInfo(_config.logLevel, @"%@: Calling %@ with isProduction:YES from Debug configuration/build. Do not use isProduction:YES in this case", self, methodName);
#else
CleverTapLogInfo(_config.logLevel, @"%@: Calling %@: with isProduction:YES from Release configuration/build. Do not release this build and use with caution", self, methodName);
CleverTapLogInfo(_config.logLevel, @"%@: Calling %@ with isProduction:YES from Release configuration/build. Do not release this build and use with caution", self, methodName);
#endif
[self runSerialAsyncEnsureHandshake:syncBlock];
} else {
Expand Down
11 changes: 10 additions & 1 deletion CleverTapSDK/InApps/CustomTemplates/CTCustomTemplateBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,16 @@ - (void)flatten:(NSDictionary *)map name:(NSString *)name {
if ([value isKindOfClass:[NSString class]]) {
[self addArgument:argName withString:value];
} else if ([value isKindOfClass:[NSNumber class]]) {
[self addArgument:argName withNumber:value];
// If the NSNumber is a boolean, use addArgument:withBool:
// so the values are correctly mapped to the type.
// This is required so dictionary arguments with booleans defined in Swift
// have correct type and value.
// Booleans are of class __NSCFBoolean, Numbers are of class __NSCFNumber.
if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
[self addArgument:argName withBool:[value boolValue]];
} else {
[self addArgument:argName withNumber:value];
}
} else if ([value isKindOfClass:[NSDictionary class]]) {
[self flatten:value name:argName];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,77 @@ - (void)testSyncPayload {
XCTAssertEqualObjects(syncPayload, expectedPayload);
}

- (void)testSyncPayloadDictionaryBooleans {
NSMutableSet *templates = [NSMutableSet set];
CTInAppTemplateBuilder *templateBuilder = [[CTInAppTemplateBuilder alloc] init];
[templateBuilder setName:@"Template 1"];
[templateBuilder addArgument:@"dictionary.booleanYes" withBool:YES];
[templateBuilder addArgument:@"dictionary.booleanNo" withBool:NO];
[templateBuilder addArgument:@"dictionary.number" withNumber:@1];
[templateBuilder addArgument:@"dictionary" withDictionary:@{
@"double": @1.0,
@"int": @0,
@"boolYes": @(YES),
@"boolNo": @(NO)
}];
[templateBuilder setPresenter:[CTTemplatePresenterMock new]];
[templates addObject:[templateBuilder build]];

CTTestTemplateProducer *producer = [[CTTestTemplateProducer alloc] initWithTemplates:templates];
[CTCustomTemplatesManager registerTemplateProducer:producer];
CTCustomTemplatesManager *manager = [self templatesManager];

NSDictionary *syncPayload = [manager syncPayload];
NSDictionary *expectedPayload = @{
@"type": @"templatePayload",
@"definitions": @{
@"Template 1": @{
@"type": TEMPLATE_TYPE,
@"vars": @{
@"dictionary.booleanNo": @{
@"defaultValue": @0,
@"order": @0,
@"type": @"boolean"
},
@"dictionary.booleanYes": @{
@"defaultValue": @1,
@"order": @1,
@"type": @"boolean"
},
@"dictionary.boolNo": @{
@"defaultValue": @0,
@"order": @2,
@"type": @"boolean"
},
@"dictionary.boolYes": @{
@"defaultValue": @1,
@"order": @3,
@"type": @"boolean"
},
@"dictionary.double": @{
@"defaultValue": @1.0,
@"order": @4,
@"type": @"number"
},
@"dictionary.int": @{
@"defaultValue": @0,
@"order": @5,
@"type": @"number"
},
@"dictionary.number": @{
@"defaultValue": @1,
@"order": @6,
@"type": @"number"
}
}
}
}
};

XCTAssertEqual([syncPayload[@"definitions"] count], 1);
XCTAssertEqualObjects(syncPayload, expectedPayload);
}

- (void)testTemplatesRegistered {
NSMutableSet *templates = [NSMutableSet set];

Expand Down
39 changes: 39 additions & 0 deletions CleverTapSDKTests/InApps/CustomTemplates/CTTemplateContextTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,45 @@ - (void)testDictionaryArguments {
XCTAssertTrue([innermostMap isEqualToDictionary:[self.templateContext dictionaryNamed:@"map.innerMap.innermostMap"]]);
}

- (void)testDictionaryBoolArguments {
NSDictionary *notificationJson = @{
@"templateName": TEMPLATE_NAME_NESTED,
@"type": @"custom-code",
@"vars": @{
@"map.double": @(0.0),
@"map.boolFalse": @(YES),
@"map.bool": @(NO)
}
};
CTInAppNotification *notification = [[CTInAppNotification alloc] initWithJSON:notificationJson];

CTInAppTemplateBuilder *templateBuilder = [[CTInAppTemplateBuilder alloc] init];
[templateBuilder setName:TEMPLATE_NAME_NESTED];
[templateBuilder addArgument:@"map" withDictionary:@{
@"int": @0,
@"double": @1.0,
@"boolFalse": @(NO),
@"boolTrue": @(YES),
@"bool": @(YES)
}];
[templateBuilder setPresenter:[CTTemplatePresenterMock new]];
CTCustomTemplate *template = [templateBuilder build];

CTTemplateContext *context = [[CTTemplateContext alloc] initWithTemplate:template notification:notification andFileDownloader:self.fileDownloader];

NSDictionary *map = [context dictionaryNamed:@"map"];
NSDictionary *notificationVars = notificationJson[@"vars"];
XCTAssertEqualObjects(@0, map[@"int"]);
XCTAssertEqualObjects(notificationVars[@"map.double"], map[@"double"]);
XCTAssertEqualObjects(@YES, map[@"boolTrue"]);
XCTAssertEqualObjects(notificationVars[@"map.boolFalse"], map[@"boolFalse"]);
XCTAssertEqualObjects(@([context boolNamed:@"map.boolFalse"]), map[@"boolFalse"]);
XCTAssertEqualObjects(@([context boolNamed:@"map.boolFalse"]), @(YES));
XCTAssertEqualObjects(notificationVars[@"map.bool"], map[@"bool"]);
XCTAssertEqualObjects(@([context boolNamed:@"map.bool"]), map[@"bool"]);
XCTAssertEqualObjects(@([context boolNamed:@"map.bool"]), @(NO));
}

- (void)testActionsValueInDictionary {
NSDictionary *actionsMap = [self.templateContext dictionaryNamed:@"map.actions"];
XCTAssertEqualObjects(VARS_ACTION_FUNCTION_NAME, actionsMap[@"function"]);
Expand Down
Loading