forked from aws/aws-sdk-net
/
InternalModel.cs
254 lines (211 loc) · 10.1 KB
/
InternalModel.cs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Reflection;
using Amazon.DynamoDB.DocumentModel;
using Amazon.DynamoDB.Model;
using Amazon.Runtime;
using Amazon.Runtime.Internal.Util;
namespace Amazon.DynamoDB.DataModel
{
internal class PropertyStorage
{
public string PropertyName { get; set; }
public string AttributeName { get; set; }
public MemberInfo Member { get; set; }
public Type MemberType { get; set; }
public IPropertyConverter Converter { get; set; }
public bool IsHashKey { get; set; }
public bool IsRangeKey { get; set; }
public bool IsVersion { get; set; }
}
internal class ItemStorage
{
public Document Document { get; set; }
public ItemStorageConfig Config { get; set; }
public Primitive CurrentVersion { get; set; }
public ItemStorage(ItemStorageConfig storageConfig)
{
Document = new Document();
Config = storageConfig;
}
}
internal class ItemStorageConfig
{
// table
public string TableName { get; set; }
public bool LowerCamelCaseProperties { get; set; }
// keys
public string HashKeyPropertyName { get; set; }
public string RangeKeyPropertyName { get; set; }
public bool HasRangeKey { get { return !string.IsNullOrEmpty(RangeKeyPropertyName); } }
// version
public string VersionPropertyName { get; set; }
public bool HasVersion { get { return !string.IsNullOrEmpty(VersionPropertyName); } }
// storage mappings
private Dictionary<string, PropertyStorage> PropertyToPropertyStorageMapping { get; set; }
public PropertyStorage GetPropertyStorage(string propertyName)
{
PropertyStorage storage;
if (PropertyToPropertyStorageMapping.TryGetValue(propertyName, out storage))
return storage;
throw new InvalidOperationException("Unable to find storage information for property [" + propertyName + "]");
}
public PropertyStorage HashPropertyStorage
{
get
{
return GetPropertyStorage(HashKeyPropertyName);
}
}
public PropertyStorage RangePropertyStorage
{
get
{
if (!HasRangeKey) throw new InvalidOperationException("No range key defined for this type");
return GetPropertyStorage(RangeKeyPropertyName);
}
}
public PropertyStorage VersionPropertyStorage
{
get
{
if (!HasVersion) throw new InvalidOperationException("No version field defined for this type");
return GetPropertyStorage(VersionPropertyName);
}
}
public void AddPropertyStorage(PropertyStorage value)
{
PropertyToPropertyStorageMapping[value.PropertyName] = value;
}
public IEnumerable<PropertyStorage> AllPropertyStorage
{
get { return PropertyToPropertyStorageMapping.Values; }
}
// properties to get
public List<string> AttributesToGet { get; private set; }
// constructor
public ItemStorageConfig()
{
PropertyToPropertyStorageMapping = new Dictionary<string, PropertyStorage>(StringComparer.Ordinal);
AttributesToGet = new List<string>();
}
}
internal static class ItemStorageConfigCache
{
private static Dictionary<Type, ItemStorageConfig> Cache;
static ItemStorageConfigCache()
{
Cache = new Dictionary<Type, ItemStorageConfig>();
}
public static ItemStorageConfig GetConfig<T>()
{
Type type = typeof(T);
return GetConfig(type);
}
public static ItemStorageConfig GetConfig(Type type)
{
ItemStorageConfig config;
if (!Cache.TryGetValue(type, out config))
{
config = CreateStorageConfig(type);
Cache[type] = config;
}
return config;
}
private static string GetAccurateCase(ItemStorageConfig config, string value)
{
return (config.LowerCamelCaseProperties ? EntityUtils.ToLowerCamelCase(value) : value);
}
internal static ItemStorageConfig CreateStorageConfig(Type type)
{
if (type == null) throw new ArgumentNullException("type");
ItemStorageConfig config = new ItemStorageConfig();
DynamoDBTableAttribute tableAttribute = EntityUtils.GetTableAttribute(type);
if (tableAttribute == null || string.IsNullOrEmpty(tableAttribute.TableName)) throw new InvalidOperationException("No DynamoDBTableAttribute on type");
if (string.IsNullOrEmpty(tableAttribute.TableName)) throw new InvalidOperationException("DynamoDBTableAttribute.Table is empty or null");
config.TableName = tableAttribute.TableName;
config.LowerCamelCaseProperties = tableAttribute.LowerCamelCaseProperties;
foreach (var member in type.GetMembers())
{
// filter out non-fields and non-properties
if (!(member is FieldInfo || member is PropertyInfo)) continue;
// filter out properties that aren't both read and write
if (!EntityUtils.IsReadWrite(member)) continue;
DynamoDBAttribute attribute = EntityUtils.GetAttribute(member);
// filter out ignored properties
if (attribute is DynamoDBIgnoreAttribute) continue;
Type memberType = EntityUtils.GetType(member);
string attributeName = GetAccurateCase(config, member.Name);
string propertyName = member.Name;
PropertyStorage propertyStorage = new PropertyStorage
{
Member = member,
MemberType = memberType,
};
if (attribute is DynamoDBVersionAttribute)
{
EntityUtils.ValidateVersionType(memberType); // no conversion is possible, so type must be a nullable primitive
if (!string.IsNullOrEmpty(config.VersionPropertyName))
throw new InvalidOperationException("Multiple version attributes defined");
config.VersionPropertyName = propertyName;
propertyStorage.IsVersion = true;
}
DynamoDBPropertyAttribute propertyAttribute = attribute as DynamoDBPropertyAttribute;
if (propertyAttribute != null)
{
if (!string.IsNullOrEmpty(propertyAttribute.AttributeName))
attributeName = GetAccurateCase(config, propertyAttribute.AttributeName);
if (propertyAttribute is DynamoDBHashKeyAttribute)
{
if (propertyAttribute.Converter == null && !EntityUtils.IsPrimitive(memberType))
throw new InvalidOperationException("Hash key " + propertyName + " must be of primitive type");
if (!string.IsNullOrEmpty(config.HashKeyPropertyName))
throw new InvalidOperationException("Multiple hash keys defined");
config.HashKeyPropertyName = propertyName;
propertyStorage.IsHashKey = true;
}
if (propertyAttribute is DynamoDBRangeKeyAttribute)
{
if (propertyAttribute.Converter == null && !EntityUtils.IsPrimitive(memberType))
throw new InvalidOperationException("Range key " + propertyName + " must be of primitive type");
if (!string.IsNullOrEmpty(config.RangeKeyPropertyName))
throw new InvalidOperationException("Multiple range keys defined");
config.RangeKeyPropertyName = propertyName;
propertyStorage.IsRangeKey = true;
}
if (propertyAttribute.Converter != null)
{
if (!EntityUtils.CanInstantiate(propertyAttribute.Converter) || !EntityUtils.ImplementsInterface(propertyAttribute.Converter, typeof(IPropertyConverter)))
throw new InvalidOperationException("Converter for " + propertyName + " must be instantiable with no parameters and must implement IPropertyConverter");
propertyStorage.Converter = EntityUtils.Instantiate(propertyAttribute.Converter) as IPropertyConverter;
}
}
propertyStorage.PropertyName = propertyName;
propertyStorage.AttributeName = attributeName;
config.AttributesToGet.Add(attributeName);
config.AddPropertyStorage(propertyStorage);
}
if (string.IsNullOrEmpty(config.HashKeyPropertyName))
throw new InvalidOperationException("No hash key configured on type");
return config;
}
}
}