-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCODictionary.m
More file actions
122 lines (93 loc) · 4.11 KB
/
Copy pathCODictionary.m
File metadata and controls
122 lines (93 loc) · 4.11 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
/*
Copyright (C) 2012 Quentin Mathe
Date: July 2013
License: MIT (see COPYING)
*/
#import "CODictionary.h"
#import "COObjectGraphContext+Private.h"
#import "COObject+Private.h"
#import "COSerialization.h"
@interface COObject ()
- (id)serializedValueForPropertyDescription: (ETPropertyDescription *)aPropertyDesc;
- (id)serializedValueForValue: (id)value
univaluedPropertyDescription: (ETPropertyDescription *)aPropertyDesc;
- (COType)serializedTypeForUnivaluedPropertyDescription: (ETPropertyDescription *)aPropertyDesc
ofValue: (id)aValue;
- (id)valueForSerializedValue: (id)value
ofType: (COType)type
univaluedPropertyDescription: (ETPropertyDescription *)aPropertyDesc;
@end
@implementation COObject (CODictionarySerialization)
#pragma mark Serialization -
- (COItem *)storeItemFromDictionaryForPropertyDescription: (ETPropertyDescription *)aPropertyDesc
{
NILARG_EXCEPTION_TEST(aPropertyDesc);
NSDictionary *dict = [self serializedValueForPropertyDescription: aPropertyDesc];
NSMutableDictionary *types =
[NSMutableDictionary dictionaryWithCapacity: dict.count];
NSMutableDictionary *values =
[NSMutableDictionary dictionaryWithCapacity: dict.count];
for (NSString *key in dict.allKeys)
{
NSAssert2(isSerializablePrimitiveValue(key),
@"Unsupported key type %@ in %@. For dictionary serialization, "
"keys must be a primitive CoreObject values (NSString, NSNumber or NSData).",
key, dict);
id value = dict[key];
id serializedValue = [self serializedValueForValue: value
univaluedPropertyDescription: aPropertyDesc];
COType serializedType = [self serializedTypeForUnivaluedPropertyDescription: aPropertyDesc
ofValue: serializedValue];
values[key] = serializedValue;
types[key] = @(serializedType);
}
ETEntityDescription *rootCoreObjectEntity =
[_objectGraphContext.modelDescriptionRepository entityDescriptionForClass: [COObject class]];
return [self storeItemWithUUID: _additionalStoreItemUUIDs[aPropertyDesc.name]
types: types
values: values
entityName: @"CODictionary"
packageDescription: rootCoreObjectEntity.owner];
}
- (NSDictionary *)dictionaryFromStoreItem: (COItem *)aStoreItem
forPropertyDescription: (ETPropertyDescription *)propertyDesc
{
NILARG_EXCEPTION_TEST(aStoreItem);
NILARG_EXCEPTION_TEST(propertyDesc);
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *property in aStoreItem.attributeNames)
{
if ([property isEqualToString: kCOItemEntityNameProperty]
|| [property isEqualToString: kCOItemPackageVersionProperty]
|| [property isEqualToString: kCOItemPackageNameProperty])
{
// HACK
continue;
}
id serializedValue = [aStoreItem valueForAttribute: property];
COType serializedType = [aStoreItem typeForAttribute: property];
if (propertyDesc == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Tried to set serialized value %@ of type %@ "
"for property %@ missing in the metamodel %@",
serializedValue,
@(serializedType),
propertyDesc.name,
self.entityDescription];
}
id value = [self valueForSerializedValue: serializedValue
ofType: serializedType
univaluedPropertyDescription: propertyDesc];
dict[property] = value;
}
// FIXME: Make read-only if needed
return dict;
}
@end
@implementation COItem (CODictionarySerialization)
- (BOOL)isAdditionalItem
{
return [self.entityName isEqualToString: @"CODictionary"];
}
@end