22using System . Collections . Generic ;
33using System . Linq ;
44using Amazon . DynamoDBv2 . Model ;
5+ #if NET6_0_OR_GREATER
56using static System . Runtime . InteropServices . CollectionsMarshal ;
7+ #endif
68
79namespace DynamoDBGenerator . Internal ;
810
@@ -17,8 +19,8 @@ public static class MarshallHelper
1719#pragma warning disable CS1591
1820 public static AttributeValue Null { get ; } = new ( ) { NULL = true } ;
1921
20- public static AttributeValue ? ToAttributeValue ( Dictionary < string , AttributeValue > ? dict ) => dict is null
21- ? null
22+ public static AttributeValue ? ToAttributeValue ( Dictionary < string , AttributeValue > ? dict ) => dict is null
23+ ? null
2224 : new AttributeValue { M = dict } ;
2325
2426 public static AttributeValue FromDictionary < T , TArgument > (
@@ -34,8 +36,8 @@ public static AttributeValue FromDictionary<T, TArgument>(
3436 _ => new Dictionary < string , AttributeValue > ( )
3537 } ;
3638
37- foreach ( var ( key , value ) in dictionary )
38- elements [ key ] = resultSelector ( value , argument , $ "{ dataMember } [{ key } ]") ;
39+ foreach ( var x in dictionary )
40+ elements [ x . Key ] = resultSelector ( x . Value , argument , $ "{ dataMember } [{ x . Key } ]") ;
3941
4042 return new AttributeValue { M = elements } ;
4143 }
@@ -89,8 +91,8 @@ public static Dictionary<string, T> ToDictionary<T, TArgument>(
8991 {
9092 var elements = new Dictionary < string , T > ( dictionary . Count ) ;
9193
92- foreach ( var ( key , value ) in dictionary )
93- elements [ key ] = resultSelector ( value , argument , $ "{ dataMember } [{ key } ]") ;
94+ foreach ( var x in dictionary )
95+ elements [ x . Key ] = resultSelector ( x . Value , argument , $ "{ dataMember } [{ x . Key } ]") ;
9496
9597 return elements ;
9698 }
@@ -101,10 +103,17 @@ public static AttributeValue FromArray<T, TArgument>(
101103 string ? dataMember ,
102104 Func < T , TArgument , string ? , AttributeValue > resultSelector )
103105 {
104- var span = array . AsSpan ( ) ;
105- var attributeValues = new List < AttributeValue > ( span . Length ) ;
106- for ( var i = 0 ; i < span . Length ; i ++ )
107- attributeValues . Add ( resultSelector ( span [ i ] , argument , $ "{ dataMember } [{ i } ]") ) ;
106+ #if NET6_0_OR_GREATER
107+ var collection = array . AsSpan ( ) ;
108+ var collectionLength = collection . Length ;
109+ #else
110+ var collectionLength = array . Length ;
111+ var collection = array ;
112+ #endif
113+
114+ var attributeValues = new List < AttributeValue > ( ) ;
115+ for ( var i = 0 ; i < collectionLength ; i ++ )
116+ attributeValues . Add ( resultSelector ( collection [ i ] , argument , $ "{ dataMember } [{ i } ]") ) ;
108117
109118 return new AttributeValue { L = attributeValues } ;
110119 }
@@ -115,10 +124,17 @@ public static AttributeValue FromList<T, TArgument>(
115124 string ? dataMember ,
116125 Func < T , TArgument , string ? , AttributeValue > resultSelector )
117126 {
118- var span = AsSpan ( list ) ;
119- var attributeValues = new List < AttributeValue > ( span . Length ) ;
120- for ( var i = 0 ; i < span . Length ; i ++ )
121- attributeValues . Add ( resultSelector ( span [ i ] , argument , $ "{ dataMember } [{ i } ]") ) ;
127+ #if NET6_0_OR_GREATER
128+ var collection = AsSpan ( list ) ;
129+ var collectionLength = collection . Length ;
130+ #else
131+ var collectionLength = list . Count ;
132+ var collection = list ;
133+ #endif
134+
135+ var attributeValues = new List < AttributeValue > ( collectionLength ) ;
136+ for ( var i = 0 ; i < collectionLength ; i ++ )
137+ attributeValues . Add ( resultSelector ( collection [ i ] , argument , $ "{ dataMember } [{ i } ]") ) ;
122138
123139 return new AttributeValue { L = attributeValues } ;
124140 }
@@ -129,9 +145,13 @@ public static AttributeValue FromEnumerable<T, TArgument>(
129145 string ? dataMember ,
130146 Func < T , TArgument , string ? , AttributeValue > resultSelector )
131147 {
148+ #if NET6_0_OR_GREATER
132149 var attributeValues = enumerable . TryGetNonEnumeratedCount ( out var count )
133150 ? new List < AttributeValue > ( count )
134151 : new List < AttributeValue > ( ) ;
152+ #else
153+ var attributeValues = new List < AttributeValue > ( ) ;
154+ #endif
135155
136156 foreach ( var ( element , i ) in enumerable . Select ( ( x , y ) => ( x , y ) ) )
137157 attributeValues . Add ( resultSelector ( element , argument , $ "{ dataMember } [{ i } ]") ) ;
@@ -146,10 +166,16 @@ public static List<TResult> ToList<TResult, TArgument>(
146166 Func < AttributeValue , TArgument , string ? , TResult > resultSelector
147167 )
148168 {
149- var span = AsSpan ( attributeValues ) ;
150- var elements = new List < TResult > ( span . Length ) ;
151- for ( var i = 0 ; i < span . Length ; i ++ )
152- elements . Add ( resultSelector ( span [ i ] , argument , $ "{ dataMember } [{ i } ]") ) ;
169+ #if NET6_0_OR_GREATER
170+ var collection = AsSpan ( attributeValues ) ;
171+ var collectionLength = collection . Length ;
172+ #else
173+ var collection = attributeValues ;
174+ var collectionLength = collection . Count ;
175+ #endif
176+ var elements = new List < TResult > ( collectionLength ) ;
177+ for ( var i = 0 ; i < collectionLength ; i ++ )
178+ elements . Add ( resultSelector ( collection [ i ] , argument , $ "{ dataMember } [{ i } ]") ) ;
153179
154180 return elements ;
155181 }
@@ -172,10 +198,16 @@ public static TResult[] ToArray<TResult, TArgument>(
172198 Func < AttributeValue , TArgument , string ? , TResult > resultSelector
173199 )
174200 {
175- var span = AsSpan ( attributeValues ) ;
176- var elements = new TResult [ span . Length ] ;
177- for ( var i = 0 ; i < span . Length ; i ++ )
178- elements [ i ] = resultSelector ( span [ i ] , argument , $ "{ dataMember } [{ i } ]") ;
201+ #if NET6_0_OR_GREATER
202+ var collection = AsSpan ( attributeValues ) ;
203+ var collectionLength = collection . Length ;
204+ #else
205+ var collection = attributeValues ;
206+ var collectionLength = collection . Count ;
207+ #endif
208+ var elements = new TResult [ collectionLength ] ;
209+ for ( var i = 0 ; i < collectionLength ; i ++ )
210+ elements [ i ] = resultSelector ( collection [ i ] , argument , $ "{ dataMember } [{ i } ]") ;
179211
180212 return elements ;
181213 }
0 commit comments