/* * 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.Collections.Generic; using System.Linq; using Amazon.DynamoDBv2.Model; using System.IO; using System.Globalization; using Amazon.Runtime.Internal.Util; using Amazon.Util; namespace Amazon.DynamoDBv2.DocumentModel { /// /// Abstract class representing an arbitrary DynamoDB attribute value /// public abstract class DynamoDBEntry : ICloneable { internal class AttributeConversionConfig { public CircularReferenceTracking CRT { get; private set; } public DynamoDBEntryConversion Conversion { get; private set; } public bool IsEmptyStringValueEnabled { get; set; } public AttributeConversionConfig(DynamoDBEntryConversion conversion, bool isEmptyStringValueEnabled) { Conversion = conversion; CRT = new CircularReferenceTracking(); IsEmptyStringValueEnabled = isEmptyStringValueEnabled; } } #region Internal conversion methods internal abstract AttributeValue ConvertToAttributeValue(AttributeConversionConfig conversionConfig); internal ExpectedAttributeValue ConvertToExpectedAttributeValue(AttributeConversionConfig conversionConfig) { AttributeValue attributeValue = ConvertToAttributeValue(conversionConfig); ExpectedAttributeValue expectedAttribute = new ExpectedAttributeValue(); if (attributeValue == null) { expectedAttribute.Exists = false; } else { expectedAttribute.Exists = true; expectedAttribute.Value = attributeValue; } return expectedAttribute; } internal AttributeValueUpdate ConvertToAttributeUpdateValue(AttributeConversionConfig conversionConfig) { AttributeValue attributeValue = ConvertToAttributeValue(conversionConfig); AttributeValueUpdate attributeUpdate = new AttributeValueUpdate(); if (attributeValue == null) { attributeUpdate.Action = "DELETE"; } else { attributeUpdate.Action = "PUT"; attributeUpdate.Value = attributeValue; } return attributeUpdate; } #endregion #region Subclass conversions /// /// Convert DynamoDBEntry to Primitive /// /// /// Primitive if DynamoDBEntry is of Primitive type; otherwise null /// public Primitive AsPrimitive() { return (this as Primitive); } /// /// Convert DynamoDBEntry to PrimitiveList /// /// /// PrimitiveList if DynamoDBEntry is of PrimitiveList type; otherwise null /// public PrimitiveList AsPrimitiveList() { return (this as PrimitiveList); } /// /// Convert DynamoDBEntry to DynamoDBList /// /// /// DynamoDBList if DynamoDBEntry is of DynamoDBList type; otherwise null /// public DynamoDBList AsDynamoDBList() { return (this as DynamoDBList); } /// /// Convert DynamoDBEntry to DynamoDBBool /// /// /// DynamoDBBool if DynamoDBEntry is of DynamoDBBool type; otherwise null /// public DynamoDBBool AsDynamoDBBool() { return (this as DynamoDBBool); } /// /// Convert DynamoDBEntry to DynamoDBNull /// /// /// DynamoDBNull if DynamoDBEntry is of DynamoDBNull type; otherwise null /// public DynamoDBNull AsDynamoDBNull() { return (this as DynamoDBNull); } /// /// Convert DynamoDBEntry to Document /// /// /// Document if DynamoDBEntry is of Document type; otherwise null /// public Document AsDocument() { return (this as Document); } #endregion #region Subclass conversion and validation internal DynamoDBEntry ToConvertedEntry(DynamoDBEntryConversion conversion) { var unconverted = this as UnconvertedDynamoDBEntry; if (unconverted == null) return this; else return unconverted.Convert(conversion); } internal Primitive ToPrimitive() { return Validate(AsPrimitive()); } internal PrimitiveList ToPrimitiveList() { return Validate(AsPrimitiveList()); } internal DynamoDBList ToDynamoDBList() { return Validate(AsDynamoDBList()); } internal DynamoDBBool ToDynamoDBBool() { return Validate(AsDynamoDBBool()); } internal DynamoDBNull ToDynamoDBNull() { return Validate(AsDynamoDBNull()); } internal Document ToDocument() { return Validate(AsDocument()); } private T Validate(T item) { if (item == null) throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, "Unable to cast {0} as {1}", GetType().FullName, typeof(T).FullName)); return item; } #endregion #region Explicit and Implicit conversions, base types /// /// Explicitly convert DynamoDBEntry to Boolean /// /// Boolean value of this object public virtual Boolean AsBoolean() { throw new InvalidCastException(); } /// /// Implicitly convert Boolean to DynamoDBEntry /// /// Boolean data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Boolean data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Boolean /// /// DynamoDBEntry to convert /// Boolean value of DynamoDBEntry public static explicit operator Boolean(DynamoDBEntry p) { return p.AsBoolean(); } /// /// Explicitly convert DynamoDBEntry to Byte /// /// Byte value of this object public virtual Byte AsByte() { throw new InvalidCastException(); } /// /// Implicitly convert Byte to DynamoDBEntry /// /// Byte data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Byte data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Byte /// /// DynamoDBEntry to convert /// Byte value of DynamoDBEntry public static explicit operator Byte(DynamoDBEntry p) { return p.AsByte(); } /// /// Explicitly convert DynamoDBEntry to SByte /// /// SByte value of this object [CLSCompliant(false)] public virtual SByte AsSByte() { throw new InvalidCastException(); } /// /// Implicitly convert SByte to DynamoDBEntry /// /// SByte data to convert /// DynamoDBEntry representing the data [CLSCompliant(false)] public static implicit operator DynamoDBEntry(SByte data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to SByte /// /// DynamoDBEntry to convert /// SByte value of DynamoDBEntry [CLSCompliant(false)] public static explicit operator SByte(DynamoDBEntry p) { return p.AsSByte(); } /// /// Explicitly convert DynamoDBEntry to UInt16 /// /// UInt16 value of this object [CLSCompliant(false)] public virtual UInt16 AsUShort() { throw new InvalidCastException(); } /// /// Implicitly convert UInt16 to DynamoDBEntry /// /// UInt16 data to convert /// DynamoDBEntry representing the data [CLSCompliant(false)] public static implicit operator DynamoDBEntry(UInt16 data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to UInt16 /// /// DynamoDBEntry to convert /// UInt16 value of DynamoDBEntry [CLSCompliant(false)] public static explicit operator UInt16(DynamoDBEntry p) { return p.AsUShort(); } /// /// Explicitly convert DynamoDBEntry to Int16 /// /// Int16 value of this object public virtual Int16 AsShort() { throw new InvalidCastException(); } /// /// Implicitly convert Int16 to DynamoDBEntry /// /// Int16 data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Int16 data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Int16 /// /// DynamoDBEntry to convert /// Int16 value of DynamoDBEntry public static explicit operator Int16(DynamoDBEntry p) { return p.AsShort(); } /// /// Explicitly convert DynamoDBEntry to UInt32 /// /// UInt32 value of this object [CLSCompliant(false)] public virtual UInt32 AsUInt() { throw new InvalidCastException(); } /// /// Implicitly convert UInt32 to DynamoDBEntry /// /// UInt32 data to convert /// DynamoDBEntry representing the data [CLSCompliant(false)] public static implicit operator DynamoDBEntry(UInt32 data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to UInt32 /// /// DynamoDBEntry to convert /// UInt32 value of DynamoDBEntry [CLSCompliant(false)] public static explicit operator UInt32(DynamoDBEntry p) { return p.AsUInt(); } /// /// Explicitly convert DynamoDBEntry to Int32 /// /// Int32 value of this object public virtual Int32 AsInt() { throw new InvalidCastException(); } /// /// Implicitly convert Int32 to DynamoDBEntry /// /// Int32 data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Int32 data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Int32 /// /// DynamoDBEntry to convert /// Int32 value of DynamoDBEntry public static explicit operator Int32(DynamoDBEntry p) { return p.AsInt(); } /// /// Explicitly convert DynamoDBEntry to UInt64 /// /// UInt64 value of this object [CLSCompliant(false)] public virtual UInt64 AsULong() { throw new InvalidCastException(); } /// /// Implicitly convert UInt64 to DynamoDBEntry /// /// UInt64 data to convert /// DynamoDBEntry representing the data [CLSCompliant(false)] public static implicit operator DynamoDBEntry(UInt64 data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to UInt64 /// /// DynamoDBEntry to convert /// UInt64 value of DynamoDBEntry [CLSCompliant(false)] public static explicit operator UInt64(DynamoDBEntry p) { return p.AsULong(); } /// /// Explicitly convert DynamoDBEntry to Int64 /// /// Int64 value of this object public virtual Int64 AsLong() { throw new InvalidCastException(); } /// /// Implicitly convert Int64 to DynamoDBEntry /// /// Int64 data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Int64 data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Int64 /// /// DynamoDBEntry to convert /// Int64 value of DynamoDBEntry public static explicit operator Int64(DynamoDBEntry p) { return p.AsLong(); } /// /// Explicitly convert DynamoDBEntry to Single /// /// Single value of this object public virtual Single AsSingle() { throw new InvalidCastException(); } /// /// Implicitly convert Single to DynamoDBEntry /// /// Single data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Single data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Single /// /// DynamoDBEntry to convert /// Single value of DynamoDBEntry public static explicit operator Single(DynamoDBEntry p) { return p.AsSingle(); } /// /// Explicitly convert DynamoDBEntry to Double /// /// Double value of this object public virtual Double AsDouble() { throw new InvalidCastException(); } /// /// Implicitly convert Double to DynamoDBEntry /// /// Double data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Double data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Double /// /// DynamoDBEntry to convert /// Double value of DynamoDBEntry public static explicit operator Double(DynamoDBEntry p) { return p.AsDouble(); } /// /// Explicitly convert DynamoDBEntry to Decimal /// /// Decimal value of this object public virtual Decimal AsDecimal() { throw new InvalidCastException(); } /// /// Implicitly convert Decimal to DynamoDBEntry /// /// Decimal data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Decimal data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Decimal /// /// DynamoDBEntry to convert /// Decimal value of DynamoDBEntry public static explicit operator Decimal(DynamoDBEntry p) { return p.AsDecimal(); } /// /// Explicitly convert DynamoDBEntry to Char /// /// Char value of this object public virtual Char AsChar() { throw new InvalidCastException(); } /// /// Implicitly convert Char to DynamoDBEntry /// /// Char data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Char data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Char /// /// DynamoDBEntry to convert /// Char value of DynamoDBEntry public static explicit operator Char(DynamoDBEntry p) { return p.AsChar(); } /// /// Explicitly convert DynamoDBEntry to String /// /// String value of this object public virtual String AsString() { throw new InvalidCastException(); } /// /// Implicitly convert String to DynamoDBEntry /// /// String data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(String data) { if(data == null) return new Primitive(); return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to String /// /// DynamoDBEntry to convert /// String value of DynamoDBEntry public static implicit operator String(DynamoDBEntry p) { return p.AsString(); } /// /// Explicitly convert DynamoDBEntry to DateTime /// /// DateTime value of this object public virtual DateTime AsDateTime() { throw new InvalidCastException(); } /// /// Implicitly convert DateTime to DynamoDBEntry /// /// DateTime data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(DateTime data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to DateTime /// /// DynamoDBEntry to convert /// DateTime value of DynamoDBEntry public static explicit operator DateTime(DynamoDBEntry p) { return p.AsDateTime(); } /// /// Explicitly convert DynamoDBEntry to Guid /// /// Guid value of this object public virtual Guid AsGuid() { throw new InvalidCastException(); } /// /// Implicitly convert Guid to DynamoDBEntry /// /// Guid data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Guid data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Guid /// /// DynamoDBEntry to convert /// Guid value of DynamoDBEntry public static explicit operator Guid(DynamoDBEntry p) { return p.AsGuid(); } /// /// Explicitly convert DynamoDBEntry to byte[] /// /// byte[] value of this object public virtual byte[] AsByteArray() { throw new InvalidCastException(); } /// /// Implicitly convert byte[] to DynamoDBEntry /// /// byte[] data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(byte[] data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to byte[] /// /// DynamoDBEntry to convert /// byte[] value of DynamoDBEntry public static explicit operator byte[](DynamoDBEntry p) { return p.AsByteArray(); } /// /// Explicitly convert DynamoDBEntry to MemoryStream /// /// MemoryStream value of this object public virtual MemoryStream AsMemoryStream() { throw new InvalidCastException(); } /// /// Implicitly convert MemoryStream to DynamoDBEntry /// /// MemoryStream data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(MemoryStream data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to MemoryStream /// /// DynamoDBEntry to convert /// MemoryStream value of DynamoDBEntry public static explicit operator MemoryStream(DynamoDBEntry p) { return p.AsMemoryStream(); } #endregion #region Explicit and Implicit conversions, lists and sets /// /// Explicitly convert DynamoDBEntry to DynamoDBEntry[] /// /// DynamoDBEntry[] value of this object public virtual DynamoDBEntry[] AsArrayOfDynamoDBEntry() { return AsListOfDynamoDBEntry().ToArray(); } /// /// Explicitly convert DynamoDBEntry to List<DynamoDBEntry> /// /// List<DynamoDBEntry> value of this object public virtual List AsListOfDynamoDBEntry() { throw new InvalidCastException(); } /// /// Explicitly convert DynamoDBEntry to HashSet<DynamoDBEntry> /// /// HashSet<DynamoDBEntry> value of this object public virtual HashSet AsHashSetOfDynamoDBEntry() { throw new InvalidCastException(); } /// /// Explicitly convert DynamoDBEntry to Primitive[] /// /// Primitive[] value of this object public virtual Primitive[] AsArrayOfPrimitive() { throw new InvalidCastException(); } /// /// Implicitly convert Primitive[] to DynamoDBEntry /// /// Primitive[] data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(Primitive[] data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to Primitive[] /// /// DynamoDBEntry to convert /// Primitive[] value of DynamoDBEntry public static explicit operator Primitive[](DynamoDBEntry p) { return p.AsArrayOfPrimitive(); } /// /// Explicitly convert DynamoDBEntry to List<Primitive> /// /// List<Primitive> value of this object public virtual List AsListOfPrimitive() { throw new InvalidCastException(); } /// /// Implicitly convert List<Primitive> to DynamoDBEntry /// /// List<Primitive> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(List data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to List<Primitive> /// /// DynamoDBEntry to convert /// List<Primitive> value of DynamoDBEntry public static explicit operator List(DynamoDBEntry p) { return p.AsListOfPrimitive(); } /// /// Explicitly convert DynamoDBEntry to HashSet<Primitive> /// /// HashSet<Primitive> value of this object public virtual HashSet AsHashSetOfPrimitive() { throw new InvalidCastException(); } /// /// Implicitly convert HashSet<Primitive> to DynamoDBEntry /// /// HashSet<Primitive> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(HashSet data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to HashSet<Primitive> /// /// DynamoDBEntry to convert /// HashSet<Primitive> value of DynamoDBEntry public static explicit operator HashSet(DynamoDBEntry p) { return p.AsHashSetOfPrimitive(); } /// /// Explicitly convert DynamoDBEntry to List<Document> /// /// List<Document> value of this object public virtual List AsListOfDocument() { throw new InvalidCastException(); } /// /// Implicitly convert List<Document> to DynamoDBEntry /// /// List<Document> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(List data) { return new DynamoDBList(data.Cast()); } /// /// Explicitly convert DynamoDBEntry to List<Document> /// /// DynamoDBEntry to convert /// List<Document> value of DynamoDBEntry public static explicit operator List(DynamoDBEntry p) { return p.AsListOfDocument(); } /// /// Explicitly convert DynamoDBEntry to String[] /// /// String[] value of this object public virtual String[] AsArrayOfString() { throw new InvalidCastException(); } /// /// Implicitly convert String[] to DynamoDBEntry /// /// String[] data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(String[] data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to String[] /// /// DynamoDBEntry to convert /// String[] value of DynamoDBEntry public static explicit operator String[](DynamoDBEntry p) { return p.AsArrayOfString(); } /// /// Explicitly convert DynamoDBEntry to List<String> /// /// List<String> value of this object public virtual List AsListOfString() { throw new InvalidCastException(); } /// /// Implicitly convert List<String> to DynamoDBEntry /// /// List<String> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(List data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to List<String> /// /// DynamoDBEntry to convert /// List<String> value of DynamoDBEntry public static explicit operator List(DynamoDBEntry p) { return p.AsListOfString(); } /// /// Explicitly convert DynamoDBEntry to HashSet<String> /// /// List<String> value of this object public virtual HashSet AsHashSetOfString() { throw new InvalidCastException(); } /// /// Implicitly convert HashSet<String> to DynamoDBEntry /// /// HashSet<String> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(HashSet data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to HashSet<String> /// /// DynamoDBEntry to convert /// HashSet<String> value of DynamoDBEntry public static explicit operator HashSet(DynamoDBEntry p) { return p.AsHashSetOfString(); } /// /// Explicitly convert DynamoDBEntry to List<byte[]> /// /// List<byte[]> value of this object public virtual List AsListOfByteArray() { throw new InvalidCastException(); } /// /// Implicitly convert List<byte[]> to DynamoDBEntry /// /// List<byte[]> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(List data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to List<byte[]> /// /// DynamoDBEntry to convert /// List<byte[]> value of DynamoDBEntry public static explicit operator List(DynamoDBEntry p) { return p.AsListOfByteArray(); } /// /// Explicitly convert DynamoDBEntry to HashSet<byte[]> /// /// HashSet<byte[]> value of this object public virtual HashSet AsHashSetOfByteArray() { throw new InvalidCastException(); } /// /// Implicitly convert HashSet<byte[]> to DynamoDBEntry /// /// HashSet<byte[]> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(HashSet data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to HashSet<byte[]> /// /// DynamoDBEntry to convert /// HashSet<byte[]> value of DynamoDBEntry public static explicit operator HashSet(DynamoDBEntry p) { return p.AsHashSetOfByteArray(); } /// /// Explicitly convert DynamoDBEntry to List<MemoryStream> /// /// List<MemoryStream> value of this object public virtual List AsListOfMemoryStream() { throw new InvalidCastException(); } /// /// Implicitly convert List<MemoryStream> to DynamoDBEntry /// /// List<MemoryStream> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(List data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to List<MemoryStream> /// /// DynamoDBEntry to convert /// List<MemoryStream> value of DynamoDBEntry public static explicit operator List(DynamoDBEntry p) { return p.AsListOfMemoryStream(); } /// /// Explicitly convert DynamoDBEntry to HashSet<MemoryStream> /// /// HashSet<MemoryStream> value of this object public virtual HashSet AsHashSetOfMemoryStream() { throw new InvalidCastException(); } /// /// Implicitly convert HashSet<MemoryStream> to DynamoDBEntry /// /// HashSet<MemoryStream> data to convert /// DynamoDBEntry representing the data public static implicit operator DynamoDBEntry(HashSet data) { return new UnconvertedDynamoDBEntry(data); } /// /// Explicitly convert DynamoDBEntry to HashSet<MemoryStream> /// /// DynamoDBEntry to convert /// HashSet<MemoryStream> value of DynamoDBEntry public static explicit operator HashSet(DynamoDBEntry p) { return p.AsHashSetOfMemoryStream(); } #endregion #region Abstract methods /// /// Declare the Clone method must be implemented. /// /// public abstract object Clone(); #endregion } /// /// A DynamoDBEntry holding an unconverted object. /// The entry is converted to a converted DynamoDBEntry either by the /// consuming Document or Table. /// internal class UnconvertedDynamoDBEntry : DynamoDBEntry { private object Value; private Type ValueType; /// /// Construct instance of UnconvertedDynamoDBEntry /// /// public UnconvertedDynamoDBEntry(object value) { if (value == null) throw new ArgumentNullException("value"); Value = value; ValueType = value.GetType(); } internal override AttributeValue ConvertToAttributeValue(AttributeConversionConfig conversionConfig) { using (conversionConfig.CRT.Track(Value)) { var convertedEntry = Convert(conversionConfig.Conversion); return convertedEntry.ConvertToAttributeValue(conversionConfig); } } /// /// Perform conversion with the given converter. /// /// /// public DynamoDBEntry Convert(DynamoDBEntryConversion conversion) { var convertedEntry = conversion.ConvertToEntry(ValueType, Value); return convertedEntry; } /// /// Implememnt the Clone method. /// /// public override object Clone() { return new UnconvertedDynamoDBEntry(Value); } #region Conversion overrides - Convert.ChangeType /// /// Return the value as a boolean. /// /// public override bool AsBoolean() { return (bool)System.Convert.ChangeType(Value, typeof(bool), CultureInfo.InvariantCulture); } /// /// Return the value as a byte. /// /// public override byte AsByte() { return (byte)System.Convert.ChangeType(Value, typeof(byte), CultureInfo.InvariantCulture); } /// /// Return the value as a string. /// /// public override string AsString() { return System.Convert.ChangeType(Value, typeof(string), CultureInfo.InvariantCulture) as string; } /// /// Return the value as a char. /// /// public override char AsChar() { return (char)System.Convert.ChangeType(Value, typeof(char), CultureInfo.InvariantCulture); } /// /// Return the value as a DateTime. /// /// public override DateTime AsDateTime() { return (DateTime)System.Convert.ChangeType(Value, typeof(DateTime), CultureInfo.InvariantCulture); } /// /// Return the value as a Decimal. /// /// public override decimal AsDecimal() { return (decimal)System.Convert.ChangeType(Value, typeof(decimal), CultureInfo.InvariantCulture); } /// /// Return the value as a double. /// /// public override double AsDouble() { return (double)System.Convert.ChangeType(Value, typeof(double), CultureInfo.InvariantCulture); } /// /// Return the value as int. /// /// public override int AsInt() { return (int)System.Convert.ChangeType(Value, typeof(int), CultureInfo.InvariantCulture); } /// /// Return the value as a long. /// /// public override long AsLong() { return (long)System.Convert.ChangeType(Value, typeof(long), CultureInfo.InvariantCulture); } /// /// Return the value as a sbyte. /// /// public override sbyte AsSByte() { return (sbyte)System.Convert.ChangeType(Value, typeof(sbyte), CultureInfo.InvariantCulture); } /// /// Return the value as a short. /// /// public override short AsShort() { return (short)System.Convert.ChangeType(Value, typeof(short), CultureInfo.InvariantCulture); } /// /// Return the value as single. /// /// public override float AsSingle() { return (float)System.Convert.ChangeType(Value, typeof(float), CultureInfo.InvariantCulture); } /// /// Return the value as uint. /// /// public override uint AsUInt() { return (uint)System.Convert.ChangeType(Value, typeof(uint), CultureInfo.InvariantCulture); } /// /// Return the value as ulong. /// /// public override ulong AsULong() { return (ulong)System.Convert.ChangeType(Value, typeof(ulong), CultureInfo.InvariantCulture); } /// /// Return the value as ushort. /// /// public override ushort AsUShort() { return (ushort)System.Convert.ChangeType(Value, typeof(ushort), CultureInfo.InvariantCulture); } /// /// Return the value as array of strings. /// /// public override string[] AsArrayOfString() { return base.AsArrayOfString(); } #endregion #region Conversion overrides - Cast /// /// Return value as byte[]. /// /// public override byte[] AsByteArray() { return (byte[])Value; } /// /// Return value as Guid. /// /// public override Guid AsGuid() { return (Guid)Value; } /// /// Return value as MemoryStream. /// /// public override MemoryStream AsMemoryStream() { return (MemoryStream)Value; } /// /// Return value as List of strings. /// /// public override List AsListOfString() { return (List)Value; } /// /// Return value as HashSet of byte[]. /// /// public override HashSet AsHashSetOfByteArray() { return (HashSet)Value; } /// /// Return value as HashSet of MemoryStream. /// /// public override HashSet AsHashSetOfMemoryStream() { return (HashSet)Value; } /// /// Return value as HashSet of string. /// /// public override HashSet AsHashSetOfString() { return (HashSet)Value; } /// /// Return value as List of byte[]. /// /// public override List AsListOfByteArray() { return (List)Value; } /// /// Return value as List of MemoryStream. /// /// public override List AsListOfMemoryStream() { return (List)Value; } #endregion #region Public overrides public override int GetHashCode() { return Hashing.Hash(ValueType, Value); } public override bool Equals(object obj) { var uddbe = obj as UnconvertedDynamoDBEntry; if (uddbe == null) return false; return (ValueType.Equals(uddbe.ValueType) && Value.Equals(uddbe.Value)); } #endregion } }