summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLRPCStruct.m
blob: 9956df03de623f4080ac55c5115b38cbbb592344 (plain)
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
//
//  SDLRPCStruct.m


#import "SDLRPCStruct.h"

#import "SDLEnum.h"
#import "SDLRPCParameterNames.h"

NS_ASSUME_NONNULL_BEGIN

@implementation SDLRPCStruct

- (instancetype)initWithDictionary:(NSDictionary<NSString *, id> *)dict {
    self = [super init];
    if (!self) {
        return nil;
    }

    _store = [dict mutableCopy];
    _payloadProtected = NO;

    return self;
}

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    _store = [NSMutableDictionary dictionary];
    _payloadProtected = NO;

    return self;
}

- (NSDictionary<NSString *, id> *)serializeAsDictionary:(Byte)version {
    if (version >= 2) {
        NSString *messageType = self.store.keyEnumerator.nextObject;
        NSMutableDictionary<NSString *, id> *function = _store[messageType];
        if ([function isKindOfClass:NSMutableDictionary.class]) {
            NSMutableDictionary<NSString *, id> *parameters = function[SDLRPCParameterNameParameters];
            return [self.class sdl_serializeDictionary:parameters version:version];
        } else {
            return [self.class sdl_serializeDictionary:self.store version:version];
        }
    } else {
        return [self.class sdl_serializeDictionary:self.store version:version];
    }
}

- (NSString *)description {
    return [self.store description];
}

+ (NSDictionary<NSString *, id> *)sdl_serializeDictionary:(NSDictionary *)dict version:(Byte)version {
    NSMutableDictionary<NSString *, id> *ret = [NSMutableDictionary dictionaryWithCapacity:dict.count];
    for (NSString *key in dict.keyEnumerator) {
        NSObject *value = dict[key];
        if ([value isKindOfClass:SDLRPCStruct.class]) {
            ret[key] = [(SDLRPCStruct *)value serializeAsDictionary:version];
        } else if ([value isKindOfClass:NSDictionary.class]) {
            ret[key] = [self sdl_serializeDictionary:(NSDictionary *)value version:version];
        } else if ([value isKindOfClass:NSArray.class]) {
            NSArray<NSObject *> *arrayVal = (NSArray<NSObject *> *)value;
            
            if (arrayVal.count > 0 && ([arrayVal.firstObject isKindOfClass:SDLRPCStruct.class])) {
                NSMutableArray<NSDictionary<NSString *, id> *> *serializedList = [NSMutableArray arrayWithCapacity:arrayVal.count];
                for (SDLRPCStruct *serializeable in arrayVal) {
                    [serializedList addObject:[serializeable serializeAsDictionary:version]];
                }
                ret[key] = serializedList;
            } else {
                ret[key] = value;
            }
        } else {
            ret[key] = value;
        }
    }
    return ret;
}

- (id)copyWithZone:(nullable NSZone *)zone {
    SDLRPCStruct *newStruct = [[[self class] allocWithZone:zone] initWithDictionary:_store];
    newStruct.payloadProtected = self.payloadProtected;

    return newStruct;
}

- (BOOL)isEqualToRPC:(SDLRPCStruct *)rpc {
    return [rpc.store isEqualToDictionary:_store];
}

- (BOOL)isEqual:(id)object {
    if (self == object) {
        return YES;
    }

    if (![object isMemberOfClass:self.class]) {
        return NO;
    }

    return [self isEqualToRPC:(SDLRPCStruct *)object];
}

@end

NS_ASSUME_NONNULL_END