Skip to content

Commit

Permalink
start replacing C defines with category
Browse files Browse the repository at this point in the history
  • Loading branch information
echo-branch committed Feb 1, 2024
1 parent 18cc5aa commit 5854fc4
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 63 deletions.
1 change: 0 additions & 1 deletion BranchSDK/BNCDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#import "BNCConfig.h"
#import "BNCNetworkInterface.h"
#import "BNCReachability.h"
#import "NSMutableDictionary+Branch.h"
#import "BNCDeviceSystem.h"

#if !TARGET_OS_TV
Expand Down
1 change: 0 additions & 1 deletion BranchSDK/BNCServerInterface.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#import "BNCEncodingUtils.h"
#import "NSError+Branch.h"
#import "BranchConstants.h"
#import "NSMutableDictionary+Branch.h"
#import "BNCLog.h"
#import "Branch.h"
#import "BNCSKAdNetwork.h"
Expand Down
26 changes: 11 additions & 15 deletions BranchSDK/BranchEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#import "BNCEventUtils.h"
#import "BNCRequestFactory.h"
#import "BNCServerAPI.h"
#import "NSMutableDictionary+Branch.h"

#pragma mark BranchStandardEvents

Expand Down Expand Up @@ -219,22 +220,17 @@ - (NSString *)jsonStringForAdType:(BranchEventAdType)adType {
- (NSDictionary*) dictionary {
NSMutableDictionary *dictionary = [NSMutableDictionary new];

#define BNCFieldDefinesDictionaryFromSelf
#include "BNCFieldDefines.h"

addString(transactionID, transaction_id);
addString(currency, currency);
addDecimal(revenue, revenue);
addDecimal(shipping, shipping);
addDecimal(tax, tax);
addString(coupon, coupon);
addString(affiliation, affiliation);
addString(eventDescription, description);
addString(searchQuery, search_query);
addDictionary(customData, custom_data);
[dictionary bnc_addString:self.transactionID forKey:@"transaction_id"];
[dictionary bnc_addString:self.currency forKey:@"currency"];
[dictionary bnc_addDecimal:self.revenue forKey:@"revenue"];
[dictionary bnc_addDecimal:self.shipping forKey:@"shipping"];
[dictionary bnc_addDecimal:self.tax forKey:@"tax"];
[dictionary bnc_addString:self.coupon forKey:@"coupon"];
[dictionary bnc_addString:self.affiliation forKey:@"affiliation"];
[dictionary bnc_addString:self.eventDescription forKey:@"description"];
[dictionary bnc_addString:self.searchQuery forKey:@"search_query"];
[dictionary bnc_addDictionary:self.customData forKey:@"custom_data"];

#include "BNCFieldDefines.h"

NSString *adTypeString = [self jsonStringForAdType:self.adType];
if (adTypeString.length > 0) {
[dictionary setObject:adTypeString forKey:@"ad_type"];
Expand Down
79 changes: 35 additions & 44 deletions BranchSDK/BranchUniversalObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import "BNCEncodingUtils.h"
#import "Branch.h"
#import "BranchEvent.h"
#import "NSMutableDictionary+Branch.h"

#if !TARGET_OS_TV
#import "BNCUserAgentCollector.h"
Expand Down Expand Up @@ -74,34 +75,29 @@ - (NSDictionary*_Nonnull) dictionary {
dictionary[key] = value;
}

#define BNCFieldDefinesDictionaryFromSelf
#include "BNCFieldDefines.h"

addString(contentSchema, $content_schema);
addDouble(quantity, $quantity);
addDecimal(price, $price);
addString(currency, $currency);
addString(sku, $sku);
addString(productName, $product_name);
addString(productBrand, $product_brand);
addString(productCategory, $product_category);
addString(productVariant, $product_variant);
addString(condition, $condition);
addDouble(ratingAverage, $rating_average);
addInteger(ratingCount, $rating_count);
addDouble(ratingMax, $rating_max);
addDouble(rating, $rating);
addString(addressStreet, $address_street);
addString(addressCity, $address_city);
addString(addressRegion, $address_region);
addString(addressCountry, $address_country);
addString(addressPostalCode,$address_postal_code);
addDouble(latitude, $latitude);
addDouble(longitude, $longitude);
addStringArray(imageCaptions,$image_captions);

#include "BNCFieldDefines.h"

[dictionary bnc_addString:self.contentSchema forKey:@"$content_schema"];
[dictionary bnc_addDouble:self.quantity forKey:@"$quantity"];
[dictionary bnc_addDecimal:self.price forKey:@"$price"];
[dictionary bnc_addString:self.currency forKey:@"$currency"];
[dictionary bnc_addString:self.sku forKey:@"$sku"];
[dictionary bnc_addString:self.productName forKey:@"$product_name"];
[dictionary bnc_addString:self.productBrand forKey:@"$product_brand"];
[dictionary bnc_addString:self.productCategory forKey:@"$product_category"];
[dictionary bnc_addString:self.productVariant forKey:@"$product_variant"];
[dictionary bnc_addString:self.condition forKey:@"$condition"];
[dictionary bnc_addDouble:self.ratingAverage forKey:@"$rating_average"];
[dictionary bnc_addInteger:self.ratingCount forKey:@"$rating_count"];
[dictionary bnc_addDouble:self.ratingMax forKey:@"$rating_max"];
[dictionary bnc_addDouble:self.rating forKey:@"$rating"];
[dictionary bnc_addString:self.addressStreet forKey:@"$address_street"];
[dictionary bnc_addString:self.addressCity forKey:@"$address_city"];
[dictionary bnc_addString:self.addressRegion forKey:@"$address_region"];
[dictionary bnc_addString:self.addressCountry forKey:@"$address_country"];
[dictionary bnc_addString:self.addressPostalCode forKey:@"$address_postal_code"];
[dictionary bnc_addDouble:self.latitude forKey:@"$latitude"];
[dictionary bnc_addDouble:self.longitude forKey:@"$longitude"];
[dictionary bnc_addStringArray:self.imageCaptions forKey:@"$image_captions"];

return dictionary;
}

Expand Down Expand Up @@ -586,22 +582,17 @@ - (NSDictionary*_Nonnull) dictionary {

NSDictionary *contentDictionary = [self.contentMetadata dictionary];
if (contentDictionary.count) [dictionary addEntriesFromDictionary:contentDictionary];

#define BNCFieldDefinesDictionaryFromSelf
#include "BNCFieldDefines.h"

addString(canonicalIdentifier, $canonical_identifier);
addString(canonicalUrl, $canonical_url);
addDate(creationDate, $creation_timestamp);
addDate(expirationDate, $exp_date);
addStringArray(keywords, $keywords);
addBoolean(locallyIndex, $locally_indexable);
addString(contentDescription, $og_description);
addString(imageUrl, $og_image_url);
addString(title, $og_title);
addBoolean(publiclyIndex, $publicly_indexable);

#include "BNCFieldDefines.h"

[dictionary bnc_addString:self.canonicalIdentifier forKey:@"$canonical_identifier"];
[dictionary bnc_addString:self.canonicalUrl forKey:@"$canonical_url"];
[dictionary bnc_addDate:self.creationDate forKey:@"$creation_timestamp"];
[dictionary bnc_addDate:self.expirationDate forKey:@"$exp_date"];
[dictionary bnc_addStringArray:self.keywords forKey:@"$keywords"];
[dictionary bnc_addBoolean:self.locallyIndex forKey:@"$locally_indexable"];
[dictionary bnc_addString:self.contentDescription forKey:@"$og_description"];
[dictionary bnc_addString:self.imageUrl forKey:@"$og_image_url"];
[dictionary bnc_addString:self.title forKey:@"$og_title"];
[dictionary bnc_addBoolean:self.publiclyIndex forKey:@"$publicly_indexable"];

return dictionary;
}
Expand Down
21 changes: 21 additions & 0 deletions BranchSDK/NSMutableDictionary+Branch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,25 @@ void BNCForceNSMutableDictionaryCategoryToLoad(void) __attribute__((constructor)
- (void) bnc_safeSetObject:(id)anObject forKey:(id<NSCopying>)aKey;
- (void) bnc_safeAddEntriesFromDictionary:(NSDictionary<id<NSCopying>,id> *)otherDictionary;

// replacement methods for BNCFieldDefinesDictionaryFromSelf
- (void)bnc_addString:(NSString *)string forKey:(NSString *)key;

- (void)bnc_addDate:(NSDate *)date forKey:(NSString *)key;

- (void)bnc_addDouble:(double)number forKey:(NSString *)key;

// omits false/NO
- (void)bnc_addBoolean:(BOOL)boolean forKey:(NSString *)key;

- (void)bnc_addDecimal:(NSDecimalNumber *)decimal forKey:(NSString *)key;

// omits 0
- (void)bnc_addInteger:(NSInteger)integer forKey:(NSString *)key;

// omits empty dictionaries
- (void)bnc_addDictionary:(NSDictionary *)dict forKey:(NSString *)key;

// omits empty string array
- (void)bnc_addStringArray:(NSArray<NSString*> *)array forKey:(NSString *)key;

@end
63 changes: 61 additions & 2 deletions BranchSDK/NSMutableDictionary+Branch.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

@implementation NSMutableDictionary (Branch)

- (void) bnc_safeSetObject:(id)anObject forKey:(id<NSCopying>)aKey {
- (void)bnc_safeSetObject:(id)anObject forKey:(id<NSCopying>)aKey {
if (anObject && aKey) {
[self setObject:anObject forKey:aKey];
}
}

- (void) bnc_safeAddEntriesFromDictionary:(NSDictionary<id<NSCopying>,id> *)otherDictionary {
- (void)bnc_safeAddEntriesFromDictionary:(NSDictionary<id<NSCopying>,id> *)otherDictionary {
if ([otherDictionary isKindOfClass:[NSDictionary class]]) {
NSDictionary *deepCopy =
[[NSDictionary alloc]
Expand All @@ -28,6 +28,65 @@ - (void) bnc_safeAddEntriesFromDictionary:(NSDictionary<id<NSCopying>,id> *)othe
}
}

#pragma mark BNCFieldDefinesDictionaryFromSelf replacement methods

- (void)bnc_addString:(NSString *)string forKey:(NSString *)key {
if (string && string.length && key) {
[self setObject:string forKey:key];
}
}

- (void)bnc_addDate:(NSDate *)date forKey:(NSString *)key {
if (date && key) {
NSTimeInterval t = date.timeIntervalSince1970;
NSNumber *tmp = [NSNumber numberWithLongLong:(long long)(t*1000.0)];
[self setObject:tmp forKey:key];
}
}

- (void)bnc_addDouble:(double)number forKey:(NSString *)key {
if (number != 0.0 && key) {
NSNumber *tmp = [NSNumber numberWithDouble:number];
[self setObject:tmp forKey:key];
}
}

// omits false/NO
- (void)bnc_addBoolean:(BOOL)boolean forKey:(NSString *)key {
if (boolean && key) {
NSNumber *tmp = [NSNumber numberWithBool:boolean];
[self setObject:tmp forKey:key];
}
}

- (void)bnc_addDecimal:(NSDecimalNumber *)decimal forKey:(NSString *)key {
if (decimal && key) {
[self setObject:decimal forKey:key];
}
}

// omits 0
- (void)bnc_addInteger:(NSInteger)integer forKey:(NSString *)key {
if (integer != 0) {
NSNumber *tmp = [NSNumber numberWithInteger:integer];
[self setObject:tmp forKey:key];
}
}

// omits empty dictionaries
- (void)bnc_addDictionary:(NSDictionary *)dict forKey:(NSString *)key {
if (dict.count) {
[self setObject:dict forKey:key];
}
}

// omits empty string array
- (void)bnc_addStringArray:(NSArray<NSString*> *)array forKey:(NSString *)key {
if (array.count) {
[self setObject:array forKey:key];
}
}

@end


Expand Down

0 comments on commit 5854fc4

Please sign in to comment.