2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
- using System . Collections . Generic ;
6
- using System . Diagnostics ;
7
5
using System . Globalization ;
8
6
using System . Reflection ;
9
7
using Azure . Data . Tables ;
10
- using Microsoft . Azure . WebJobs . Host ;
11
- using ITableEntity = Azure . Data . Tables . ITableEntity ;
12
8
13
9
namespace Microsoft . Azure . WebJobs . Extensions . Tables
14
10
{
15
11
internal class TableEntityToPocoConverter < TOutput > : IConverter < TableEntity , TOutput > where TOutput : new ( )
16
12
{
17
- // private readonly IPropertySetter<TOutput, string> _partitionKeySetter;
18
- // private readonly IPropertySetter<TOutput, string> _rowKeySetter;
19
- // private readonly IPropertySetter<TOutput, DateTimeOffset> _timestampSetter;
20
- // private readonly IPropertySetter<TOutput, string> _eTagSetter;
21
- // private readonly IReadOnlyDictionary<string, IPropertySetter<TOutput, EntityProperty>> _otherPropertySetters;
22
-
23
- // private TableEntityToPocoConverter(
24
- // IPropertySetter<TOutput, string> partitionKeySetter,
25
- // IPropertySetter<TOutput, string> rowKeySetter,
26
- // IPropertySetter<TOutput, DateTimeOffset> timestampSetter,
27
- // IPropertySetter<TOutput, string> eTagSetter,
28
- // IReadOnlyDictionary<string, IPropertySetter<TOutput, EntityProperty>> otherPropertySetters)
29
- // {
30
- // Debug.Assert(otherPropertySetters != null);
31
- // _partitionKeySetter = partitionKeySetter;
32
- // _rowKeySetter = rowKeySetter;
33
- // _timestampSetter = timestampSetter;
34
- // _eTagSetter = eTagSetter;
35
- // _otherPropertySetters = otherPropertySetters;
36
- // }
13
+ public TableEntityToPocoConverter ( )
14
+ {
15
+ CheckSetter < string > ( "PartitionKey" ) ;
16
+ CheckSetter < string > ( "RowKey" ) ;
17
+ CheckSetter < DateTimeOffset > ( "Timestamp" ) ;
18
+ CheckSetter < string > ( "ETag" ) ;
19
+ }
37
20
38
21
public TOutput Convert ( TableEntity input )
39
22
{
40
- if ( input == null )
41
- {
42
- return default ( TOutput ) ;
43
- }
44
-
45
- TOutput result = new TOutput ( ) ;
46
- // if (_partitionKeySetter != null)
47
- // {
48
- // _partitionKeySetter.SetValue(ref result, input.PartitionKey);
49
- // }
50
- //
51
- // if (_rowKeySetter != null)
52
- // {
53
- // _rowKeySetter.SetValue(ref result, input.RowKey);
54
- // }
55
- //
56
- // if (_timestampSetter != null)
57
- // {
58
- // _timestampSetter.SetValue(ref result, input.Timestamp);
59
- // }
60
- //
61
- // IDictionary<string, EntityProperty> properties = input.WriteEntity(operationContext: null);
62
- // if (properties != null)
63
- // {
64
- // foreach (KeyValuePair<string, IPropertySetter<TOutput, EntityProperty>> pair in _otherPropertySetters)
65
- // {
66
- // string propertyName = pair.Key;
67
- // if (properties.ContainsKey(propertyName))
68
- // {
69
- // IPropertySetter<TOutput, EntityProperty> setter = pair.Value;
70
- // Debug.Assert(setter != null);
71
- // EntityProperty propertyValue = properties[propertyName];
72
- // setter.SetValue(ref result, propertyValue);
73
- // }
74
- // }
75
- // }
76
- //
77
- // if (_eTagSetter != null)
78
- // {
79
- // _eTagSetter.SetValue(ref result, input.ETag);
80
- // }
81
-
82
- return PocoTypeBinder . Shared . Deserialize < TOutput > ( input ) ;
23
+ return input == null ? default : PocoTypeBinder . Shared . Deserialize < TOutput > ( input ) ;
83
24
}
84
25
//
85
26
// public static TableEntityToPocoConverter<TOutput> Create()
@@ -109,49 +50,30 @@ public TOutput Convert(TableEntity input)
109
50
// eTagSetter, otherPropertySetters);
110
51
// }
111
52
//
112
- // private static IPropertySetter<TOutput, TProperty> GetSetter<TProperty>(string propertyName)
113
- // {
114
- // PropertyInfo property = typeof(TOutput).GetProperty(propertyName,
115
- // BindingFlags.Instance | BindingFlags.Public);
116
- // if (property == null || !property.CanWrite || !property.HasPublicSetMethod())
117
- // {
118
- // return null;
119
- // }
120
- //
121
- // if (property.PropertyType != typeof(TProperty))
122
- // {
123
- // string message = String.Format(CultureInfo.InvariantCulture,
124
- // "If the {0} property is present, it must be a {1}.", propertyName, typeof(TProperty).Name);
125
- // throw new InvalidOperationException(message);
126
- // }
127
- //
128
- // if (property.GetIndexParameters().Length != 0)
129
- // {
130
- // string message = String.Format(CultureInfo.InvariantCulture,
131
- // "If the {0} property is present, it must not be an indexer.", propertyName);
132
- // throw new InvalidOperationException(message);
133
- // }
134
- //
135
- // return PropertyAccessorFactory<TOutput>.CreateSetter<TProperty>(property);
136
- // }
137
- //
138
- // private static IPropertySetter<TOutput, EntityProperty> GetOtherSetter(PropertyInfo property)
139
- // {
140
- // MethodInfo genericMethodTemplate = typeof(TableEntityToPocoConverter<TOutput>).GetMethod(
141
- // "GetOtherSetterGeneric", BindingFlags.Static | BindingFlags.NonPublic);
142
- // MethodInfo genericMethod = genericMethodTemplate.MakeGenericMethod(property.PropertyType);
143
- // Func<PropertyInfo, IPropertySetter<TOutput, EntityProperty>> invoker =
144
- // (Func<PropertyInfo, IPropertySetter<TOutput, EntityProperty>>)genericMethod.CreateDelegate(
145
- // typeof(Func<PropertyInfo, IPropertySetter<TOutput, EntityProperty>>));
146
- // return invoker.Invoke(property);
147
- // }
148
- //
149
- // private static IPropertySetter<TOutput, EntityProperty> GetOtherSetterGeneric<TProperty>(PropertyInfo property)
150
- // {
151
- // IConverter<EntityProperty, TProperty> converter = EntityPropertyToTConverterFactory.Create<TProperty>();
152
- // IPropertySetter<TOutput, TProperty> propertySetter =
153
- // PropertyAccessorFactory<TOutput>.CreateSetter<TProperty>(property);
154
- // return new ConverterPropertySetter<TOutput, TProperty, EntityProperty>(converter, propertySetter);
155
- // }
53
+ private static bool CheckSetter < TProperty > ( string propertyName )
54
+ {
55
+ PropertyInfo property = typeof ( TOutput ) . GetProperty ( propertyName ,
56
+ BindingFlags . Instance | BindingFlags . Public ) ;
57
+ if ( property == null || ! property . CanWrite || ! property . HasPublicSetMethod ( ) )
58
+ {
59
+ return false ;
60
+ }
61
+
62
+ if ( property . PropertyType != typeof ( TProperty ) )
63
+ {
64
+ string message = String . Format ( CultureInfo . InvariantCulture ,
65
+ "If the {0} property is present, it must be a {1}." , propertyName , typeof ( TProperty ) . Name ) ;
66
+ throw new InvalidOperationException ( message ) ;
67
+ }
68
+
69
+ if ( property . GetIndexParameters ( ) . Length != 0 )
70
+ {
71
+ string message = String . Format ( CultureInfo . InvariantCulture ,
72
+ "If the {0} property is present, it must not be an indexer." , propertyName ) ;
73
+ throw new InvalidOperationException ( message ) ;
74
+ }
75
+
76
+ return true ;
77
+ }
156
78
}
157
79
}
0 commit comments