-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathAMPUtils.m
More file actions
161 lines (147 loc) · 5.24 KB
/
AMPUtils.m
File metadata and controls
161 lines (147 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// AMPUtil.m
// Pods
//
// Created by Daniel Jih on 10/4/15.
//
//
#ifndef AMPLITUDE_DEBUG
#define AMPLITUDE_DEBUG 0
#endif
#ifndef AMPLITUDE_LOG
#if AMPLITUDE_DEBUG
# define AMPLITUDE_LOG(fmt, ...) NSLog(fmt, ##__VA_ARGS__)
#else
# define AMPLITUDE_LOG(...)
#endif
#endif
#import <Foundation/Foundation.h>
#import "AMPUtils.h"
#import "AMPARCMacros.h"
@interface AMPUtils()
@end
@implementation AMPUtils
+ (id)alloc
{
// Util class cannot be instantiated.
return nil;
}
+ (NSString*)generateUUID
{
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
#if __has_feature(objc_arc)
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);
#else
NSString *uuidStr = (NSString *) CFUUIDCreateString(kCFAllocatorDefault, uuid);
#endif
CFRelease(uuid);
return SAFE_ARC_AUTORELEASE(uuidStr);
}
+ (id) makeJSONSerializable:(id) obj
{
if (obj == nil) {
return [NSNull null];
}
if ([obj isKindOfClass:[NSString class]] ||
[obj isKindOfClass:[NSNull class]]) {
return obj;
}
if ([obj isKindOfClass:[NSNumber class]]) {
if (!isfinite([obj floatValue])) {
return [NSNull null];
} else {
return obj;
}
}
if ([obj isKindOfClass:[NSDate class]]) {
return [obj description];
}
if ([obj isKindOfClass:[NSArray class]]) {
NSMutableArray *arr = [NSMutableArray array];
id objCopy = [obj copy];
for (id i in objCopy) {
[arr addObject:[self makeJSONSerializable:i]];
}
SAFE_ARC_RELEASE(objCopy);
return [NSArray arrayWithArray:arr];
}
if ([obj isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
id objCopy = [obj copy];
for (id key in objCopy) {
NSString *coercedKey = [self coerceToString:key withName:@"property key"];
dict[coercedKey] = [self makeJSONSerializable:objCopy[key]];
}
SAFE_ARC_RELEASE(objCopy);
return [NSDictionary dictionaryWithDictionary:dict];
}
NSString *str = [obj description];
AMPLITUDE_LOG(@"WARNING: Invalid property value type, received %@, coercing to %@", [obj class], str);
return str;
}
+ (BOOL) isEmptyString:(NSString*) str
{
return str == nil || [str isKindOfClass:[NSNull class]] || [str length] == 0;
}
+ (NSString *) coerceToString: (id) obj withName:(NSString *) name
{
NSString *coercedString;
if (![obj isKindOfClass:[NSString class]]) {
coercedString = [obj description];
AMPLITUDE_LOG(@"WARNING: Non-string %@, received %@, coercing to %@", name, [obj class], coercedString);
} else {
coercedString = obj;
}
return coercedString;
}
+ (NSDictionary *) validateGroups:(NSDictionary *) obj
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
id objCopy = [obj copy];
for (id key in objCopy) {
NSString *coercedKey = [self coerceToString:key withName:@"groupType"];
id value = objCopy[key];
if ([value isKindOfClass:[NSString class]]) {
dict[coercedKey] = value;
} else if ([value isKindOfClass:[NSArray class]]) {
NSMutableArray *arr = [NSMutableArray array];
for (id i in value) {
if ([i isKindOfClass:[NSArray class]]) {
AMPLITUDE_LOG(@"WARNING: Skipping nested NSArray in groupName value for groupType %@", coercedKey);
continue;
} else if ([i isKindOfClass:[NSDictionary class]]) {
AMPLITUDE_LOG(@"WARNING: Skipping nested NSDictionary in groupName value for groupType %@", coercedKey);
continue;
} else if ([i isKindOfClass:[NSString class]] || [i isKindOfClass:[NSNumber class]] || [i isKindOfClass:[NSDate class]]) {
[arr addObject:[self coerceToString:i withName:@"groupType"]];
} else {
AMPLITUDE_LOG(@"WARNING: Invalid groupName value in array for groupType %@ (received class %@). Please use NSStrings", coercedKey, [i class]);
}
}
dict[coercedKey] = [NSArray arrayWithArray:arr];
} else if ([value isKindOfClass:[NSNumber class]] || [value isKindOfClass:[NSDate class]]){
dict[coercedKey] = [self coerceToString:value withName:@"groupName"];
} else {
AMPLITUDE_LOG(@"WARNING: Invalid groupName value for groupType %@ (received class %@). Please use NSString or NSArray of NSStrings", coercedKey, [value class]);
}
}
SAFE_ARC_RELEASE(objCopy);
return [NSDictionary dictionaryWithDictionary:dict];
}
+ (BOOL)moveFileIfNotExists:(NSString*)from to:(NSString*)to
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
if (![fileManager fileExistsAtPath:to] &&
[fileManager fileExistsAtPath:from]) {
if ([fileManager copyItemAtPath:from toPath:to error:&error]) {
AMPLITUDE_LOG(@"INFO: copied %@ to %@", from, to);
[fileManager removeItemAtPath:from error:NULL];
} else {
AMPLITUDE_LOG(@"WARN: Copy from %@ to %@ failed: %@", from, to, error);
return NO;
}
}
return YES;
}
@end