-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.m
107 lines (85 loc) · 3.18 KB
/
config.m
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
#import "config.h"
@interface Config ()
- (BOOL)validateConfigDictionary:(NSDictionary *)config_dict;
@end
@implementation Config
- (instancetype)initWithConfigPath:(NSString *)config_file_path {
// TODO: check that all of this shit does not return any errors
self = [super init];
// TODO: validate config_file_path
NSError *error = nil;
// Read the content of the file into a string
NSString *config_file_content =
[NSString stringWithContentsOfFile:config_file_path
encoding:NSUTF8StringEncoding
error:&error];
if (error) {
NSLog(@"Error reading file: %@", [error localizedDescription]);
} else {
// Successfully read the file content
NSLog(@"File Content:\n%@", config_file_content);
}
NSData *config_data =
[config_file_content dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", config_data);
id config_nsobject =
[NSJSONSerialization JSONObjectWithData:config_data
options:NSJSONReadingMutableContainers
error:&error];
if (error) {
NSLog(@"Error parsing JSON: %@", error);
}
NSDictionary *config_dict = (NSDictionary *)config_nsobject;
NSLog(@"%@", config_nsobject);
BOOL res = [self validateConfigDictionary:config_dict];
_config = [config_dict mutableCopy];
return self;
}
- (BOOL)validateConfigDictionary:(NSDictionary *)dictionary {
// TODO: built de NSMutableSet here
// Check if the top-level dictionary contains "events" key
NSArray *events = dictionary[@"events"];
if (![events isKindOfClass:[NSArray class]]) {
NSLog(@"Missing or invalid 'events' key");
return NO;
}
// Check if the "enrichment" dictionary is present and valid
NSDictionary *enrichment = dictionary[@"enrichment"];
if (![enrichment isKindOfClass:[NSDictionary class]]) {
NSLog(@"Missing or invalid 'enrichment' key");
return NO;
}
// Check if the "hashes" dictionary is present within "enrichment"
NSDictionary *hashes = enrichment[@"hashes"];
if (![hashes isKindOfClass:[NSDictionary class]]) {
NSLog(@"Missing or invalid 'hashes' key in 'enrichment'");
return NO;
}
// Check if the "sha1" dictionary is present within "hashes"
NSDictionary *sha1 = hashes[@"sha1"];
if (![sha1 isKindOfClass:[NSDictionary class]]) {
NSLog(@"Missing or invalid 'sha1' key in 'hashes'");
return NO;
}
// Check if the "max_file_size" string is present within "sha1"
NSString *maxFileSize = sha1[@"max_file_size"];
if (![maxFileSize isKindOfClass:[NSString class]]) {
NSLog(@"Missing or invalid 'max_file_size' key in 'sha1'");
return NO;
}
// Check if the "block_list" dictionary is present and valid
NSDictionary *blockList = dictionary[@"block_list"];
if (![blockList isKindOfClass:[NSDictionary class]]) {
NSLog(@"Missing or invalid 'block_list' key");
return NO;
}
// Check if the "paths" array is present within "block_list"
NSArray *paths = blockList[@"paths"];
if (![paths isKindOfClass:[NSArray class]]) {
NSLog(@"Missing or invalid 'paths' key in 'block_list'");
return NO;
}
// If all checks pass, return YES
return YES;
}
@end