PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sdk/src/Services/DynamoDBv2/Custom/DataModel/Attributes.cs

https://gitlab.com/CORP-RESELLER/aws-sdk-net
C# | 396 lines | 149 code | 39 blank | 208 comment | 0 complexity | a9d1dee20ce3118b86d7b7cb69a7bc8e MD5 | raw file
  1. /*
  2. * Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. using System;
  16. using System.Linq;
  17. namespace Amazon.DynamoDBv2.DataModel
  18. {
  19. /// <summary>
  20. /// Base DynamoDB attribute.
  21. /// </summary>
  22. [AttributeUsage(AttributeTargets.All, Inherited=true, AllowMultiple=false)]
  23. public abstract class DynamoDBAttribute : Attribute
  24. {
  25. }
  26. /// <summary>
  27. /// DynamoDB attribute that marks a class.
  28. /// Specifies that this object can be stored in DynamoDB, the name
  29. /// of the target table, and if attribute names must be automatically
  30. /// converted to lowerCamelCase.
  31. ///
  32. /// Need not be declared on subclasses if present on base class.
  33. /// Can be defined on subclasses to specify different target table
  34. /// or specify different attribute casing.
  35. /// </summary>
  36. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited=true, AllowMultiple=false)]
  37. public sealed class DynamoDBTableAttribute : DynamoDBAttribute
  38. {
  39. /// <summary>
  40. /// Gets and sets the TableName property.
  41. /// </summary>
  42. public string TableName { get; set; }
  43. /// <summary>
  44. /// Gets and sets the LowerCamelCaseProperties property.
  45. /// </summary>
  46. public bool LowerCamelCaseProperties { get; set; }
  47. /// <summary>
  48. /// Construct an instance of DynamoDBTableAttribute
  49. /// </summary>
  50. /// <param name="tableName"></param>
  51. public DynamoDBTableAttribute(string tableName)
  52. : this(tableName, false) { }
  53. /// <summary>
  54. /// Construct an instance of DynamoDBTableAttribute
  55. /// </summary>
  56. /// <param name="tableName"></param>
  57. /// <param name="lowerCamelCaseProperties"></param>
  58. public DynamoDBTableAttribute(string tableName, bool lowerCamelCaseProperties)
  59. {
  60. TableName = tableName;
  61. LowerCamelCaseProperties = lowerCamelCaseProperties;
  62. }
  63. }
  64. /// <summary>
  65. /// DynamoDB attribute that directs the specified attribute not to
  66. /// be included when saving or loading objects.
  67. /// </summary>
  68. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  69. public sealed class DynamoDBIgnoreAttribute : DynamoDBAttribute
  70. {
  71. }
  72. /// <summary>
  73. /// DynamoDB property attribute.
  74. /// Can be used to specify an alternative attribute name or configure
  75. /// a custom converter.
  76. /// </summary>
  77. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  78. public abstract class DynamoDBRenamableAttribute : DynamoDBAttribute
  79. {
  80. /// <summary>
  81. /// Default constructor
  82. /// </summary>
  83. protected DynamoDBRenamableAttribute()
  84. {
  85. }
  86. /// <summary>
  87. /// Constructor that specifies an alternate attribute name
  88. /// </summary>
  89. /// <param name="attributeName">
  90. /// Name of attribute to be associated with property or field.
  91. /// </param>
  92. protected DynamoDBRenamableAttribute(string attributeName)
  93. {
  94. AttributeName = attributeName;
  95. }
  96. /// <summary>
  97. /// Name of attribute that is associated with this member.
  98. /// </summary>
  99. public string AttributeName { get; set; }
  100. }
  101. /// <summary>
  102. /// DynamoDB property that marks up current member as item version.
  103. /// At most one member in a class may be marked with this attribute.
  104. ///
  105. /// Members that are marked as version must be of primitive,
  106. /// numeric, integer, nullable type (e.g. int?, long?, byte?)
  107. /// </summary>
  108. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  109. public sealed class DynamoDBVersionAttribute : DynamoDBRenamableAttribute
  110. {
  111. }
  112. /// <summary>
  113. /// DynamoDB property attribute.
  114. /// Can be used to specify an alternative attribute name or configure
  115. /// a custom converter.
  116. /// </summary>
  117. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  118. public class DynamoDBPropertyAttribute : DynamoDBRenamableAttribute
  119. {
  120. /// <summary>
  121. /// Default constructor
  122. /// </summary>
  123. public DynamoDBPropertyAttribute()
  124. : base()
  125. {
  126. }
  127. /// <summary>
  128. /// Constructor that specifies an alternate attribute name
  129. /// </summary>
  130. /// <param name="attributeName">
  131. /// Name of attribute to be associated with property or field.
  132. /// </param>
  133. public DynamoDBPropertyAttribute(string attributeName)
  134. : base(attributeName)
  135. {
  136. }
  137. /// <summary>
  138. /// Constructor that specifies a custom converter.
  139. ///
  140. /// Converter must be the type of a class that implements IPropertyConverter.
  141. /// </summary>
  142. /// <param name="converter">Custom converter type.</param>
  143. public DynamoDBPropertyAttribute(Type converter)
  144. {
  145. Converter = converter;
  146. }
  147. /// <summary>
  148. /// Constructor that specifies an alternate attribute name and a custom converter.
  149. ///
  150. /// Converter must be the type of a class that implements IPropertyConverter.
  151. /// </summary>
  152. /// <param name="attributeName">
  153. /// Name of attribute to be associated with property or field.
  154. /// </param>
  155. /// <param name="converter">Custom converter type.</param>
  156. public DynamoDBPropertyAttribute(string attributeName, Type converter)
  157. : base(attributeName)
  158. {
  159. Converter = converter;
  160. }
  161. /// <summary>
  162. /// Type of the custom converter.
  163. /// </summary>
  164. public Type Converter { get; set; }
  165. }
  166. /// <summary>
  167. /// DynamoDB property that marks up current member as a hash key element.
  168. /// Exactly one member in a class must be marked with this attribute.
  169. ///
  170. /// Members that are marked as hash key must be convertible to
  171. /// a Primitive object.
  172. /// </summary>
  173. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  174. public class DynamoDBHashKeyAttribute : DynamoDBPropertyAttribute
  175. {
  176. /// <summary>
  177. /// Default constructor
  178. /// </summary>
  179. public DynamoDBHashKeyAttribute()
  180. : base()
  181. {
  182. }
  183. /// <summary>
  184. /// Constructor that specifies an alternate attribute name
  185. /// </summary>
  186. /// <param name="attributeName">
  187. /// Name of attribute to be associated with property or field.
  188. /// </param>
  189. public DynamoDBHashKeyAttribute(string attributeName)
  190. : base(attributeName)
  191. {
  192. }
  193. /// <summary>
  194. /// Constructor that specifies a custom converter.
  195. ///
  196. /// Converter must be the type of a class that implements IPropertyConverter.
  197. /// </summary>
  198. /// <param name="converter">Custom converter type.</param>
  199. public DynamoDBHashKeyAttribute(Type converter)
  200. : base(converter)
  201. {
  202. }
  203. /// <summary>
  204. /// Constructor that specifies an alternate attribute name and a custom converter.
  205. ///
  206. /// Converter must be the type of a class that implements IPropertyConverter.
  207. /// </summary>
  208. /// <param name="attributeName">
  209. /// Name of attribute to be associated with property or field.
  210. /// </param>
  211. /// <param name="converter">Custom converter type.</param>
  212. public DynamoDBHashKeyAttribute(string attributeName, Type converter)
  213. : base(attributeName, converter)
  214. {
  215. }
  216. }
  217. /// <summary>
  218. /// DynamoDB property that marks up current member as range key element (for a hash-and-range primary key).
  219. /// At most one member in a class may be marked with this attribute.
  220. ///
  221. /// Members that are marked as a range key element must be convertible to
  222. /// a Primitive object.
  223. /// </summary>
  224. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  225. public class DynamoDBRangeKeyAttribute : DynamoDBPropertyAttribute
  226. {
  227. /// <summary>
  228. /// Default constructor
  229. /// </summary>
  230. public DynamoDBRangeKeyAttribute()
  231. : base()
  232. {
  233. }
  234. /// <summary>
  235. /// Constructor that specifies an alternate attribute name
  236. /// </summary>
  237. /// <param name="attributeName">
  238. /// Name of attribute to be associated with property or field.
  239. /// </param>
  240. public DynamoDBRangeKeyAttribute(string attributeName)
  241. : base(attributeName)
  242. {
  243. }
  244. /// <summary>
  245. /// Constructor that specifies a custom converter.
  246. ///
  247. /// Converter must be the type of a class that implements IPropertyConverter.
  248. /// </summary>
  249. /// <param name="converter">Custom converter type.</param>
  250. public DynamoDBRangeKeyAttribute(Type converter)
  251. : base(converter)
  252. {
  253. }
  254. /// <summary>
  255. /// Constructor that specifies an alternate attribute name and a custom converter.
  256. ///
  257. /// Converter must be the type of a class that implements IPropertyConverter.
  258. /// </summary>
  259. /// <param name="attributeName">
  260. /// Name of attribute to be associated with property or field.
  261. /// </param>
  262. /// <param name="converter">Custom converter type.</param>
  263. public DynamoDBRangeKeyAttribute(string attributeName, Type converter)
  264. : base(attributeName, converter)
  265. {
  266. }
  267. }
  268. /// DynamoDB property attribute that marks up current member as a hash key element for a Global Secondary Index on a table.
  269. ///
  270. /// Members that are marked as a Global Secondary Index hash key element must be convertible to a Primitive object.
  271. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  272. public class DynamoDBGlobalSecondaryIndexHashKeyAttribute : DynamoDBHashKeyAttribute
  273. {
  274. /// <summary>
  275. /// Index associated with this range key
  276. /// </summary>
  277. public string[] IndexNames { get; set; }
  278. /// <summary>
  279. /// Constructor that accepts a single inde name.
  280. /// </summary>
  281. /// <param name="indexName">Name of the Local Secondary Index this range key belongs to.</param>
  282. public DynamoDBGlobalSecondaryIndexHashKeyAttribute(string indexName)
  283. : base()
  284. {
  285. IndexNames = new string[] { indexName };
  286. }
  287. /// <summary>
  288. /// Constructor that accepts multiple index names.
  289. /// </summary>
  290. /// <param name="indexNames">Names of the Local Secondary Indexes this range key belongs to.</param>
  291. public DynamoDBGlobalSecondaryIndexHashKeyAttribute(params string[] indexNames)
  292. : base()
  293. {
  294. IndexNames = indexNames.Distinct(StringComparer.Ordinal).ToArray();
  295. }
  296. }
  297. /// DynamoDB property attribute that marks up current member as range key element for a Global Secondary Index on a table.
  298. ///
  299. /// Members that are marked as a Global Secondary Index range key element must be convertible to a Primitive object.
  300. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  301. public class DynamoDBGlobalSecondaryIndexRangeKeyAttribute : DynamoDBRangeKeyAttribute
  302. {
  303. /// <summary>
  304. /// Index associated with this range key
  305. /// </summary>
  306. public string[] IndexNames { get; set; }
  307. /// <summary>
  308. /// Constructor that accepts a single inde name.
  309. /// </summary>
  310. /// <param name="indexName">Name of the Local Secondary Index this range key belongs to.</param>
  311. public DynamoDBGlobalSecondaryIndexRangeKeyAttribute(string indexName)
  312. : base()
  313. {
  314. IndexNames = new string[] { indexName };
  315. }
  316. /// <summary>
  317. /// Constructor that accepts multiple index names.
  318. /// </summary>
  319. /// <param name="indexNames">Names of the Local Secondary Indexes this range key belongs to.</param>
  320. public DynamoDBGlobalSecondaryIndexRangeKeyAttribute(params string[] indexNames)
  321. : base()
  322. {
  323. IndexNames = indexNames.Distinct(StringComparer.Ordinal).ToArray();
  324. }
  325. }
  326. /// <summary>
  327. /// DynamoDB property that marks up current member as range key element for a Local Secondary Index on a table.
  328. ///
  329. /// Members that are marked as a Local Secondary Index range key element must be convertible to a Primitive object.
  330. /// </summary>
  331. [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  332. public sealed class DynamoDBLocalSecondaryIndexRangeKeyAttribute : DynamoDBPropertyAttribute
  333. {
  334. /// <summary>
  335. /// Index associated with this range key
  336. /// </summary>
  337. public string[] IndexNames { get; set; }
  338. /// <summary>
  339. /// Constructor that accepts a single inde name.
  340. /// </summary>
  341. /// <param name="indexName">Name of the Local Secondary Index this range key belongs to.</param>
  342. public DynamoDBLocalSecondaryIndexRangeKeyAttribute(string indexName)
  343. : base()
  344. {
  345. IndexNames = new string[] { indexName };
  346. }
  347. /// <summary>
  348. /// Constructor that accepts multiple index names.
  349. /// </summary>
  350. /// <param name="indexNames">Names of the Local Secondary Indexes this range key belongs to.</param>
  351. public DynamoDBLocalSecondaryIndexRangeKeyAttribute(params string[] indexNames)
  352. : base()
  353. {
  354. IndexNames = indexNames.Distinct(StringComparer.Ordinal).ToArray();
  355. }
  356. }
  357. }