Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for retrieving DateTime attributes in UTC timezone from a DynamoDB table #3098

Merged
merged 3 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions generator/.DevConfigs/ac013f8a-bdb9-4a20-8d70-a9b60fe8c011.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"core": {
"changeLogMessages": [
"Add support for retrieving DateTime attributes in UTC timezone from a DynamoDB table."
],
"type": "patch",
"updateMinimum": false
},
"services": [
{
"serviceName": "DynamoDBv2",
"type": "patch",
"changeLogMessages": [
"Add support for retrieving DateTime attributes in UTC timezone from a DynamoDB table."
]
}
]
}
2 changes: 1 addition & 1 deletion sdk/src/Core/AWSConfigs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Amazon
/// <proxy host="localhost" port="8888" username="1" password="1" />
///
/// <dynamoDB>
/// <dynamoDBContext tableNamePrefix="Prod-" metadataCachingMode="Default" disableFetchingTableMetadata="false">
/// <dynamoDBContext tableNamePrefix="Prod-" metadataCachingMode="Default" disableFetchingTableMetadata="false" retrieveDateTimeInUtc="false">
///
/// <tableAliases>
/// <alias fromTable="FakeTable" toTable="People" />
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/AWSConfigs.DynamoDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ public partial class DynamoDBContextConfig
/// </remarks>
public bool? DisableFetchingTableMetadata { get; set; }

/// <summary>
/// If true, all <see cref="DateTime"/> properties are retrieved in UTC timezone while reading data from DynamoDB. Else, the local timezone is used.
96malhar marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <remarks>This setting is only applicable to the high-level library. Service calls made via <see cref="AmazonDynamoDBClient"/> will always return <see cref="DateTime"/> attributes in UTC.</remarks>
public bool? RetrieveDateTimeInUtc { get; set; }

/// <summary>
/// Adds a TableAlias to the TableAliases property.
/// An exception is thrown if there is already a TableAlias with the same FromTable configured.
Expand Down Expand Up @@ -203,6 +209,7 @@ internal void Configure(DynamoDBContextSection section)
TableNamePrefix = section.TableNamePrefix;
MetadataCachingMode = section.MetadataCachingMode;
DisableFetchingTableMetadata = section.DisableFetchingTableMetadata;
RetrieveDateTimeInUtc = section.RetrieveDateTimeInUtc;

InternalSDKUtils.FillDictionary(section.TypeMappings.Items, t => t.Type, t => new TypeMapping(t), TypeMappings);
InternalSDKUtils.FillDictionary(section.TableAliases.Items, t => t.FromTable, t => t.ToTable, TableAliases);
Expand Down Expand Up @@ -422,6 +429,7 @@ internal class DynamoDBContextSection : WritableConfigurationElement
private const string mappingsKey = "mappings";
private const string metadataCachingModeKey = "metadataCachingMode";
private const string disableFetchingTableMetadataKey = "disableFetchingTableMetadata";
private const string retrieveDateTimeInUtcKey = "retrieveDateTimeInUtc";

[ConfigurationProperty(tableNamePrefixKey)]
public string TableNamePrefix
Expand Down Expand Up @@ -458,6 +466,12 @@ public bool? DisableFetchingTableMetadata
set { this[disableFetchingTableMetadataKey] = value; }
}

[ConfigurationProperty(retrieveDateTimeInUtcKey)]
public bool? RetrieveDateTimeInUtc
{
get { return (bool?)this[retrieveDateTimeInUtcKey]; }
set { this[retrieveDateTimeInUtcKey] = value; }
}
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public DynamoDBContextConfig()
Conversion = DynamoDBEntryConversion.CurrentConversion;
MetadataCachingMode = AWSConfigsDynamoDB.Context.MetadataCachingMode;
DisableFetchingTableMetadata = AWSConfigsDynamoDB.Context.DisableFetchingTableMetadata;
RetrieveDateTimeInUtc = AWSConfigsDynamoDB.Context.RetrieveDateTimeInUtc;
}

/// <summary>
Expand Down Expand Up @@ -130,6 +131,12 @@ public DynamoDBContextConfig()
/// otherwise exceptions may be thrown and/or the results of certain DynamoDB operations may change.
/// </remarks>
public bool? DisableFetchingTableMetadata { get; set; }

/// <summary>
/// If true, all <see cref="DateTime"/> properties are retrieved in UTC timezone while reading data from DynamoDB. Else, the local timezone is used.
96malhar marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <remarks>This setting is only applicable to the high-level library. Service calls made via <see cref="AmazonDynamoDBClient"/> will always return <see cref="DateTime"/> attributes in UTC.</remarks>
public bool? RetrieveDateTimeInUtc { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -334,6 +341,7 @@ public DynamoDBFlatConfig(DynamoDBOperationConfig operationConfig, DynamoDBConte
bool skipVersionCheck = operationConfig.SkipVersionCheck ?? contextConfig.SkipVersionCheck ?? false;
bool ignoreNullValues = operationConfig.IgnoreNullValues ?? contextConfig.IgnoreNullValues ?? false;
bool disableFetchingTableMetadata = contextConfig.DisableFetchingTableMetadata ?? false;
bool retrieveDateTimeInUtc = operationConfig.RetrieveDateTimeInUtc ?? contextConfig.RetrieveDateTimeInUtc ?? false;

bool isEmptyStringValueEnabled = operationConfig.IsEmptyStringValueEnabled ?? contextConfig.IsEmptyStringValueEnabled ?? false;
string overrideTableName =
Expand Down Expand Up @@ -362,6 +370,7 @@ public DynamoDBFlatConfig(DynamoDBOperationConfig operationConfig, DynamoDBConte
Conversion = conversion;
MetadataCachingMode = metadataCachingMode;
DisableFetchingTableMetadata = disableFetchingTableMetadata;
RetrieveDateTimeInUtc = retrieveDateTimeInUtc;

State = new OperationState();
}
Expand Down Expand Up @@ -454,6 +463,9 @@ public DynamoDBFlatConfig(DynamoDBOperationConfig operationConfig, DynamoDBConte
/// <inheritdoc cref="DynamoDBContextConfig.DisableFetchingTableMetadata"/>
public bool DisableFetchingTableMetadata { get; set; }

/// <inheritdoc cref="DynamoDBContextConfig.RetrieveDateTimeInUtc"/>
public bool RetrieveDateTimeInUtc { get; set; }

// Checks if the IndexName is set on the config
internal bool IsIndexOperation { get { return !string.IsNullOrEmpty(IndexName); } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,14 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB
var targetType = propertyStorage.MemberType;

if (conversion.HasConverter(targetType))
return conversion.ConvertFromEntry(targetType, entry);
{
var output = conversion.ConvertFromEntry(targetType, entry);
if (targetType == typeof(DateTime) && flatConfig.RetrieveDateTimeInUtc)
ashovlin marked this conversation as resolved.
Show resolved Hide resolved
{
return ((DateTime)output).ToUniversalTime();
}
return output;
}
else
{
if (entry is DynamoDBNull)
Expand Down
19 changes: 19 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DocumentModel/DynamoDBEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,16 @@ public virtual DateTime AsDateTime()
{
throw new InvalidCastException();
}

/// <summary>
/// Explicitly convert DynamoDBEntry to DateTime in UTC
/// </summary>
/// <returns>DateTime value of this object in UTC</returns>
public virtual DateTime AsDateTimeUtc()
{
throw new InvalidCastException();
}

/// <summary>
/// Implicitly convert DateTime to DynamoDBEntry
/// </summary>
Expand Down Expand Up @@ -1152,6 +1162,15 @@ public override DateTime AsDateTime()
return (DateTime)System.Convert.ChangeType(Value, typeof(DateTime), CultureInfo.InvariantCulture);
}

/// <summary>
/// Return the value as a DateTime in UTC.
/// </summary>
/// <returns>DateTime value of this object in UTC</returns>
public override DateTime AsDateTimeUtc()
{
return AsDateTime().ToUniversalTime();
}

/// <summary>
/// Return the value as a Decimal.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Primitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,16 @@ public override DateTime AsDateTime()
{
return V1Conversion.ConvertFromEntry<DateTime>(this);
}

/// <summary>
/// Explicitly convert Primitive to DateTime in UTC
/// </summary>
/// <returns>DateTime value of this object in UTC</returns>
public override DateTime AsDateTimeUtc()
{
return AsDateTime().ToUniversalTime();
}

/// <summary>
/// Implicitly convert DateTime to Primitive
/// </summary>
Expand Down
Loading