/*
* Copyright 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.Linq;
namespace Amazon.DynamoDBv2.DataModel
{
///
/// Base DynamoDB attribute.
///
[AttributeUsage(AttributeTargets.All, Inherited=true, AllowMultiple=false)]
public abstract class DynamoDBAttribute : Attribute
{
}
///
/// DynamoDB attribute that marks a class.
/// Specifies that this object can be stored in DynamoDB, the name
/// of the target table, and if attribute names must be automatically
/// converted to lowerCamelCase.
///
/// Need not be declared on subclasses if present on base class.
/// Can be defined on subclasses to specify different target table
/// or specify different attribute casing.
///
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited=true, AllowMultiple=false)]
public sealed class DynamoDBTableAttribute : DynamoDBAttribute
{
///
/// Gets and sets the TableName property.
///
public string TableName { get; set; }
///
/// Gets and sets the LowerCamelCaseProperties property.
///
public bool LowerCamelCaseProperties { get; set; }
///
/// Construct an instance of DynamoDBTableAttribute
///
///
public DynamoDBTableAttribute(string tableName)
: this(tableName, false) { }
///
/// Construct an instance of DynamoDBTableAttribute
///
///
///
public DynamoDBTableAttribute(string tableName, bool lowerCamelCaseProperties)
{
TableName = tableName;
LowerCamelCaseProperties = lowerCamelCaseProperties;
}
}
///
/// DynamoDB attribute that directs the specified attribute not to
/// be included when saving or loading objects.
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class DynamoDBIgnoreAttribute : DynamoDBAttribute
{
}
///
/// DynamoDB property attribute.
/// Can be used to specify an alternative attribute name or configure
/// a custom converter.
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public abstract class DynamoDBRenamableAttribute : DynamoDBAttribute
{
///
/// Default constructor
///
protected DynamoDBRenamableAttribute()
{
}
///
/// Constructor that specifies an alternate attribute name
///
///
/// Name of attribute to be associated with property or field.
///
protected DynamoDBRenamableAttribute(string attributeName)
{
AttributeName = attributeName;
}
///
/// Name of attribute that is associated with this member.
///
public string AttributeName { get; set; }
}
///
/// DynamoDB property that marks up current member as item version.
/// At most one member in a class may be marked with this attribute.
///
/// Members that are marked as version must be of primitive,
/// numeric, integer, nullable type (e.g. int?, long?, byte?)
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class DynamoDBVersionAttribute : DynamoDBRenamableAttribute
{
///
/// Default constructor
///
public DynamoDBVersionAttribute()
: base()
{
}
///
/// Constructor that specifies an alternate attribute name
///
///
/// Name of attribute to be associated with property or field.
///
public DynamoDBVersionAttribute(string attributeName)
: base(attributeName)
{
}
}
///
/// DynamoDB property attribute.
/// Can be used to specify an alternative attribute name or configure
/// a custom converter.
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class DynamoDBPropertyAttribute : DynamoDBRenamableAttribute
{
///
/// Default constructor
///
public DynamoDBPropertyAttribute()
: base()
{
}
///
/// Constructor that specifies an alternate attribute name
///
///
/// Name of attribute to be associated with property or field.
///
public DynamoDBPropertyAttribute(string attributeName)
: base(attributeName)
{
}
///
/// Constructor that specifies a custom converter.
///
/// Converter must be the type of a class that implements IPropertyConverter.
///
/// Custom converter type.
public DynamoDBPropertyAttribute(Type converter)
{
Converter = converter;
}
///
/// Constructor that specifies that this type should be stored as epoch seconds.
///
///
/// Whether the data should be stored as epoch seconds.
/// If false, data is stored as ISO-8601 string.
///
public DynamoDBPropertyAttribute(bool storeAsEpoch)
{
StoreAsEpoch = storeAsEpoch;
}
///
/// Constructor that specifies an alternate attribute name and a custom converter.
///
/// Converter must be the type of a class that implements IPropertyConverter.
///
///
/// Name of attribute to be associated with property or field.
///
/// Custom converter type.
public DynamoDBPropertyAttribute(string attributeName, Type converter)
: base(attributeName)
{
Converter = converter;
}
///
/// Constructor that specifies an alternate attribute name and that this type should
/// be stored as epoch seconds.
///
///
/// Name of attribute to be associated with property or field.
///
///
/// Whether the data should be stored as epoch seconds.
/// If false, data is stored as ISO-8601 string.
///
public DynamoDBPropertyAttribute(string attributeName, bool storeAsEpoch)
: base(attributeName)
{
StoreAsEpoch = storeAsEpoch;
}
///
/// Type of the custom converter.
/// Cannot be set at the same time as StoreAsEpoch.
///
public Type Converter { get; set; }
///
/// Flag that directs DynamoDBContext to store this data as epoch seconds integer.
/// If false, data is stored as ISO-8601 string.
/// Cannot be set at the same time as Converter.
///
public bool StoreAsEpoch { get; set; }
}
///
/// DynamoDB property that marks up current member as a hash key element.
/// Exactly one member in a class must be marked with this attribute.
///
/// Members that are marked as hash key must be convertible to
/// a Primitive object.
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class DynamoDBHashKeyAttribute : DynamoDBPropertyAttribute
{
///
/// Default constructor
///
public DynamoDBHashKeyAttribute()
: base()
{
}
///
/// Constructor that specifies an alternate attribute name
///
///
/// Name of attribute to be associated with property or field.
///
public DynamoDBHashKeyAttribute(string attributeName)
: base(attributeName)
{
}
///
/// Constructor that specifies a custom converter.
///
/// Converter must be the type of a class that implements IPropertyConverter.
///
/// Custom converter type.
public DynamoDBHashKeyAttribute(Type converter)
: base(converter)
{
}
///
/// Constructor that specifies an alternate attribute name and a custom converter.
///
/// Converter must be the type of a class that implements IPropertyConverter.
///
///
/// Name of attribute to be associated with property or field.
///
/// Custom converter type.
public DynamoDBHashKeyAttribute(string attributeName, Type converter)
: base(attributeName, converter)
{
}
}
///
/// DynamoDB property that marks up current member as range key element (for a hash-and-range primary key).
/// At most one member in a class may be marked with this attribute.
///
/// Members that are marked as a range key element must be convertible to
/// a Primitive object.
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class DynamoDBRangeKeyAttribute : DynamoDBPropertyAttribute
{
///
/// Default constructor
///
public DynamoDBRangeKeyAttribute()
: base()
{
}
///
/// Constructor that specifies an alternate attribute name
///
///
/// Name of attribute to be associated with property or field.
///
public DynamoDBRangeKeyAttribute(string attributeName)
: base(attributeName)
{
}
///
/// Constructor that specifies a custom converter.
///
/// Converter must be the type of a class that implements IPropertyConverter.
///
/// Custom converter type.
public DynamoDBRangeKeyAttribute(Type converter)
: base(converter)
{
}
///
/// Constructor that specifies an alternate attribute name and a custom converter.
///
/// Converter must be the type of a class that implements IPropertyConverter.
///
///
/// Name of attribute to be associated with property or field.
///
/// Custom converter type.
public DynamoDBRangeKeyAttribute(string attributeName, Type converter)
: base(attributeName, converter)
{
}
}
/// DynamoDB property attribute that marks up current member as a hash key element for a Global Secondary Index on a table.
///
/// Members that are marked as a Global Secondary Index hash key element must be convertible to a Primitive object.
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class DynamoDBGlobalSecondaryIndexHashKeyAttribute : DynamoDBHashKeyAttribute
{
///
/// Index associated with this range key
///
public string[] IndexNames { get; set; }
///
/// Constructor that accepts a single inde name.
///
/// Name of the Local Secondary Index this range key belongs to.
public DynamoDBGlobalSecondaryIndexHashKeyAttribute(string indexName)
: base()
{
IndexNames = new string[] { indexName };
}
///
/// Constructor that accepts multiple index names.
///
/// Names of the Local Secondary Indexes this range key belongs to.
public DynamoDBGlobalSecondaryIndexHashKeyAttribute(params string[] indexNames)
: base()
{
IndexNames = indexNames.Distinct(StringComparer.Ordinal).ToArray();
}
}
/// DynamoDB property attribute that marks up current member as range key element for a Global Secondary Index on a table.
///
/// Members that are marked as a Global Secondary Index range key element must be convertible to a Primitive object.
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class DynamoDBGlobalSecondaryIndexRangeKeyAttribute : DynamoDBRangeKeyAttribute
{
///
/// Index associated with this range key
///
public string[] IndexNames { get; set; }
///
/// Constructor that accepts a single inde name.
///
/// Name of the Local Secondary Index this range key belongs to.
public DynamoDBGlobalSecondaryIndexRangeKeyAttribute(string indexName)
: base()
{
IndexNames = new string[] { indexName };
}
///
/// Constructor that accepts multiple index names.
///
/// Names of the Local Secondary Indexes this range key belongs to.
public DynamoDBGlobalSecondaryIndexRangeKeyAttribute(params string[] indexNames)
: base()
{
IndexNames = indexNames.Distinct(StringComparer.Ordinal).ToArray();
}
}
///
/// DynamoDB property that marks up current member as range key element for a Local Secondary Index on a table.
///
/// Members that are marked as a Local Secondary Index range key element must be convertible to a Primitive object.
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class DynamoDBLocalSecondaryIndexRangeKeyAttribute : DynamoDBPropertyAttribute
{
///
/// Index associated with this range key
///
public string[] IndexNames { get; set; }
///
/// Constructor that accepts a single inde name.
///
/// Name of the Local Secondary Index this range key belongs to.
public DynamoDBLocalSecondaryIndexRangeKeyAttribute(string indexName)
: base()
{
IndexNames = new string[] { indexName };
}
///
/// Constructor that accepts multiple index names.
///
/// Names of the Local Secondary Indexes this range key belongs to.
public DynamoDBLocalSecondaryIndexRangeKeyAttribute(params string[] indexNames)
: base()
{
IndexNames = indexNames.Distinct(StringComparer.Ordinal).ToArray();
}
}
}