Skip to content

Commit 95f64da

Browse files
authored
[V4] Support Native AOT trimming for DynamoDB DataModel (#3606)
* Add Native AOT support DataModel high level library for DynamoDB
1 parent 1ac6f7e commit 95f64da

File tree

62 files changed

+656
-708
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+656
-708
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"core": {
3+
"changeLogMessages": [
4+
"Backport the .NET trimming attributes like DynamicallyAccessedMembersAttribute into ThirdParty.RuntimeBackports namespace for Target Frameworks before .NET 8. This simplifies the SDK's codebase by removing compilation conditional checks when using the attributes."
5+
],
6+
"type": "patch",
7+
"updateMinimum": true
8+
},
9+
"services": [
10+
{
11+
"serviceName": "DynamoDBv2",
12+
"type": "patch",
13+
"changeLogMessages": [
14+
"Add NativeAOT support for the DataModel namespace also known as the Object Persistence high level library."
15+
]
16+
}
17+
]
18+
}

sdk/src/Core/Amazon.Runtime/ClientContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222
using Amazon.Util.Internal;
2323
using Amazon.Util.Internal.PlatformServices;
2424
using Amazon.Runtime;
25+
using System.Diagnostics.CodeAnalysis;
26+
using ThirdParty.RuntimeBackports;
2527

2628
namespace Amazon.Runtime.Internal
2729
{
2830
/// <summary>
2931
/// This class composes Client Context header for Amazon Web Service client.
3032
/// It contains information like app title, version code, version name, client id, OS platform etc.
3133
/// </summary>
32-
#if NET8_0_OR_GREATER
3334
// This class wasn't updated to use source generators because the object to JSON method uses the non generic IDictionary. Usage of this class is probably very low since it is not used directly
3435
// by the SDK and it is in an internal namespace.
35-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("ClientContext has not been updated to support producing JSON using source generators. For requests that need client context JSON the JSON must be created manually.")]
36-
#endif
36+
[RequiresUnreferencedCode("ClientContext has not been updated to support producing JSON using source generators. For requests that need client context JSON the JSON must be created manually.")]
3737
public partial class ClientContext
3838
{
3939
//client related keys

sdk/src/Core/Amazon.Runtime/ConstantClass.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@
2727
using System.Text;
2828

2929
using Amazon.Util.Internal;
30+
using ThirdParty.RuntimeBackports;
3031

3132
namespace Amazon.Runtime
3233
{
3334
/// <summary>
3435
/// Base class for constant class that holds the value that will be sent to AWS for the static constants.
3536
/// </summary>
36-
#if NET8_0_OR_GREATER
3737
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
38-
#endif
3938
public class ConstantClass
4039
{
4140
static readonly object staticFieldsLock = new object();
@@ -91,11 +90,7 @@ internal ConstantClass Intern()
9190
return map.TryGetValue(this.Value, out foundValue) ? foundValue : this;
9291
}
9392

94-
#if NET8_0_OR_GREATER
9593
protected static T FindValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicConstructors)] T>(string value) where T : ConstantClass
96-
#else
97-
protected static T FindValue<T>(string value) where T : ConstantClass
98-
#endif
9994
{
10095
if (value == null)
10196
return null;
@@ -115,11 +110,7 @@ protected static T FindValue<T>(string value) where T : ConstantClass
115110
return foundValue as T;
116111
}
117112

118-
#if NET8_0_OR_GREATER
119113
private static void LoadFields([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type type)
120-
#else
121-
private static void LoadFields(Type type)
122-
#endif
123114
{
124115
if (staticFields.ContainsKey(type))
125116
return;

sdk/src/Core/Amazon.Runtime/Credentials/AssumeRoleAWSCredentials.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System;
2121
using System.Globalization;
2222
using System.Net;
23+
using System.Diagnostics.CodeAnalysis;
24+
using ThirdParty.RuntimeBackports;
2325

2426
namespace Amazon.Runtime
2527
{
@@ -89,10 +91,8 @@ public AssumeRoleAWSCredentials(AWSCredentials sourceCredentials, string roleArn
8991
PreemptExpiryTime = TimeSpan.FromMinutes(15);
9092
}
9193

92-
#if NET8_0_OR_GREATER
93-
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
94+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
9495
Justification = "Reflection code is only used as a fallback in case the SDK was not trimmed. Trimmed scenarios should register dependencies with Amazon.RuntimeDependencyRegistry.GlobalRuntimeDependencyRegistry")]
95-
#endif
9696
protected override CredentialsRefreshState GenerateNewCredentials()
9797
{
9898
var region = FallbackRegionFactory.GetRegionEndpoint() ?? DefaultSTSClientRegion;

sdk/src/Core/Amazon.Runtime/Credentials/AssumeRoleWithWebIdentityCredentials.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
using Amazon.RuntimeDependencies;
1919
using Amazon.Util;
2020
using Amazon.Util.Internal;
21+
using ThirdParty.RuntimeBackports;
2122
using System;
23+
using System.Diagnostics.CodeAnalysis;
2224
using System.Globalization;
2325
using System.IO;
2426
using System.Net;
@@ -222,10 +224,8 @@ protected override async Task<CredentialsRefreshState> GenerateNewCredentialsAsy
222224
/// Gets a client to be used for AssumeRoleWithWebIdentity requests.
223225
/// </summary>
224226
/// <returns>The STS client.</returns>
225-
#if NET8_0_OR_GREATER
226-
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
227+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
227228
Justification = "Reflection code is only used as a fallback in case the SDK was not trimmed. Trimmed scenarios should register dependencies with Amazon.RuntimeDependencyRegistry.GlobalRuntimeDependencyRegistry")]
228-
#endif
229229
protected virtual ICoreAmazonSTS_WebIdentity CreateClient()
230230
{
231231
var region = FallbackRegionFactory.GetRegionEndpoint() ?? _defaultSTSClientRegion;

sdk/src/Core/Amazon.Runtime/Credentials/FederatedAWSCredentials.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
using Amazon.Util;
2222
using Amazon.RuntimeDependencies;
2323
using Amazon.Util.Internal;
24+
using ThirdParty.RuntimeBackports;
2425
using System;
26+
using System.Diagnostics.CodeAnalysis;
2527
using System.Collections.Generic;
2628
using System.Globalization;
2729
using System.Net;
@@ -177,10 +179,8 @@ protected override CredentialsRefreshState GenerateNewCredentials()
177179
return newState;
178180
}
179181

180-
#if NET8_0_OR_GREATER
181-
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
182+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026",
182183
Justification = "Reflection code is only used as a fallback in case the SDK was not trimmed. Trimmed scenarios should register dependencies with Amazon.RuntimeDependencyRegistry.GlobalRuntimeDependencyRegistry")]
183-
#endif
184184
private CredentialsRefreshState Authenticate(ICredentials userCredential)
185185
{
186186
CredentialsRefreshState state;

sdk/src/Core/Amazon.Runtime/Credentials/URIBasedRefreshingCredentialHelper.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Globalization;
20+
using System.Diagnostics.CodeAnalysis;
2021
using System.IO;
2122
using System.Net;
2223
using ThirdParty.Json.LitJson;
24+
using ThirdParty.RuntimeBackports;
2325

2426
namespace Amazon.Runtime
2527
{
@@ -50,27 +52,21 @@ protected static string GetContents(Uri uri, IWebProxy proxy, Dictionary<string,
5052
}
5153

5254
[Obsolete("This method is not compatible with Native AOT builds. The GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
53-
#if NET8_0_OR_GREATER
54-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
55-
#endif
55+
[RequiresUnreferencedCode("GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
5656
protected static T GetObjectFromResponse<T>(Uri uri)
5757
{
5858
return GetObjectFromResponse<T>(uri, null, null);
5959
}
6060

6161
[Obsolete("This method is not compatible with Native AOT builds. The GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
62-
#if NET8_0_OR_GREATER
63-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
64-
#endif
62+
[RequiresUnreferencedCode("GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
6563
protected static T GetObjectFromResponse<T>(Uri uri, IWebProxy proxy)
6664
{
6765
return GetObjectFromResponse<T>(uri, proxy, null);
6866
}
6967

7068
[Obsolete("This method is not compatible with Native AOT builds. The GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
71-
#if NET8_0_OR_GREATER
72-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
73-
#endif
69+
[RequiresUnreferencedCode("GetObjectFromResponse overload using the generic parameter taking in a JsonSerializerContext should be used instead.")]
7470
protected static T GetObjectFromResponse<T>(Uri uri, IWebProxy proxy, Dictionary<string, string> headers)
7571
{
7672
string json = GetContents(uri, proxy, headers);

sdk/src/Core/Amazon.Runtime/Documents/Document.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
using System;
1818
using System.Collections;
1919
using System.Collections.Generic;
20+
using System.Diagnostics.CodeAnalysis;
2021
using System.Globalization;
2122
using System.Linq;
2223
using ThirdParty.Json.LitJson;
24+
using ThirdParty.RuntimeBackports;
2325

2426
namespace Amazon.Runtime.Documents
2527
{
@@ -410,19 +412,15 @@ public override string ToString()
410412
/// for performance critical work. Additionally, if <paramref name="o"/> is a known primitive (ie <see cref="int"/>),
411413
/// using a <see cref="Document"/> constructor directly will be more performant.
412414
/// </summary>
413-
#if NET8_0_OR_GREATER
414-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("FromObject is not currently supported for Native AOT compilation due unbounded reflection required.")]
415-
#endif
415+
[RequiresUnreferencedCode("FromObject is not currently supported for Native AOT compilation due unbounded reflection required.")]
416416
public static Document FromObject(object o)
417417
{
418418
IJsonWrapper jsonData = JsonMapper.ToObject(JsonMapper.ToJson(o));
419419

420420
return FromObject(jsonData);
421421
}
422422

423-
#if NET8_0_OR_GREATER
424-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("FromObject is not currently supported for Native AOT compilation due unbounded reflection required.")]
425-
#endif
423+
[RequiresUnreferencedCode("FromObject is not currently supported for Native AOT compilation due unbounded reflection required.")]
426424
private static Document FromObject(IJsonWrapper jsonData)
427425
{
428426
switch (jsonData.GetJsonType())
@@ -450,9 +448,7 @@ private static Document FromObject(IJsonWrapper jsonData)
450448
throw new NotSupportedException($"Couldn't convert {jsonData.GetJsonType()}");
451449
}
452450

453-
#if NET8_0_OR_GREATER
454-
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("FromObject is not currently supported for Native AOT compilation due unbounded reflection required.")]
455-
#endif
451+
[RequiresUnreferencedCode("FromObject is not currently supported for Native AOT compilation due unbounded reflection required.")]
456452
private static void Copy(IDictionary source, Dictionary<string, Document> target)
457453
{
458454
foreach (var key in source.Keys)

sdk/src/Core/Amazon.Runtime/EventStreams/Internal/EnumerableEventStream.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
using System.Threading.Tasks;
3131
#endif
3232

33+
using ThirdParty.RuntimeBackports;
34+
3335
namespace Amazon.Runtime.EventStreams.Internal
3436
{
3537
/// <summary>
@@ -49,11 +51,7 @@ namespace Amazon.Runtime.EventStreams.Internal
4951
/// <typeparam name="TE">An implementation of EventStreamException (e.g. S3EventStreamException).</typeparam>
5052
[SuppressMessage("Microsoft.Naming", "CA1710", Justification = "EventStreamCollection is not descriptive.")]
5153
[SuppressMessage("Microsoft.Design", "CA1063", Justification = "IDisposable is a transient interface from IEventStream. Users need to be able to call Dispose.")]
52-
#if NET8_0_OR_GREATER
5354
public abstract class EnumerableEventStream<T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TE> : EventStream<T, TE>, IEnumerableEventStream<T, TE>, IAsyncEnumerable<T> where T : IEventStreamEvent where TE : EventStreamException, new()
54-
#else
55-
public abstract class EnumerableEventStream<T, TE> : EventStream<T, TE>, IEnumerableEventStream<T, TE>, IAsyncEnumerable<T> where T : IEventStreamEvent where TE : EventStreamException, new()
56-
#endif
5755
{
5856
private const string MutuallyExclusiveExceptionMessage = "Stream has already begun processing. Event-driven and Enumerable traversals of the stream are mutually exclusive. " +
5957
"You can either use the event driven or enumerable interface, but not both.";

sdk/src/Core/Amazon.Runtime/EventStreams/Internal/EventStream.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Diagnostics.CodeAnalysis;
1919
using System.IO;
2020
using System.Threading;
21+
using ThirdParty.RuntimeBackports;
2122

2223
#if AWS_ASYNC_API
2324
using System.Threading.Tasks;
@@ -67,11 +68,7 @@ namespace Amazon.Runtime.EventStreams.Internal
6768
/// </summary>
6869
/// <typeparam name="T">An implementation of IEventStreamEvent (e.g. IS3Event).</typeparam>
6970
/// <typeparam name="TE">An implementation of EventStreamException (e.g. S3EventStreamException).</typeparam>
70-
#if NET8_0_OR_GREATER
7171
public abstract class EventStream<T, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TE> : IEventStream<T, TE> where T : IEventStreamEvent where TE : EventStreamException, new()
72-
#else
73-
public abstract class EventStream<T, TE> : IEventStream<T, TE> where T : IEventStreamEvent where TE : EventStreamException, new()
74-
#endif
7572
{
7673
/// <summary>
7774
/// "Unique" key for unknown event lookup.

0 commit comments

Comments
 (0)