3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using Microsoft . Azure . Cosmos . Table ;
6
+ using Azure ;
7
+ using Azure . Data . Tables ;
7
8
using Newtonsoft . Json . Linq ;
8
9
9
10
namespace Microsoft . Azure . WebJobs . Script . WebHost . Scale
10
11
{
11
12
/// <summary>
12
- /// Class providing methods to convert between DynamicTableEntity and custom pocos
13
+ /// Class providing methods to convert between <see cref="TableEntity"/> and custom pocos.
13
14
/// </summary>
14
15
internal static class TableEntityConverter
15
16
{
16
- public static DynamicTableEntity ToEntity ( object o ,
17
+ public static TableEntity ToEntity ( object o ,
17
18
string partitionKey = null ,
18
19
string rowKey = null ,
19
20
DateTimeOffset ? timeStamp = null ,
20
21
string etag = null )
21
22
{
22
- var entity = new DynamicTableEntity
23
+ var entity = new TableEntity
23
24
{
24
25
RowKey = rowKey ,
25
- PartitionKey = partitionKey ,
26
- Properties = new Dictionary < string , EntityProperty > ( )
26
+ PartitionKey = partitionKey
27
27
} ;
28
28
29
29
if ( timeStamp . HasValue )
@@ -33,108 +33,70 @@ public static DynamicTableEntity ToEntity(object o,
33
33
34
34
if ( ! string . IsNullOrWhiteSpace ( etag ) )
35
35
{
36
- entity . ETag = etag ;
36
+ entity . ETag = new ETag ( etag ) ;
37
37
}
38
38
39
39
var jo = JObject . FromObject ( o ) ;
40
40
foreach ( var prop in jo . Properties ( ) )
41
41
{
42
- if ( TryGetEntityProperty ( prop , out EntityProperty entityProperty ) )
42
+ if ( TryGetEntityProperty ( prop , out object value ) )
43
43
{
44
- entity . Properties . Add ( prop . Name , entityProperty ) ;
44
+ entity . Add ( prop . Name , value ) ;
45
45
}
46
46
}
47
47
48
48
return entity ;
49
49
}
50
50
51
- public static object ToObject ( Type type , DynamicTableEntity entity )
52
- {
53
- return ToObject ( type , entity . Properties ) ;
54
- }
55
-
56
- public static TOutput ToObject < TOutput > ( IDictionary < string , EntityProperty > properties )
51
+ public static TOutput ToObject < TOutput > ( IDictionary < string , object > properties )
57
52
{
58
53
return ( TOutput ) ToObject ( typeof ( TOutput ) , properties ) ;
59
54
}
60
55
61
- public static object ToObject ( Type type , IDictionary < string , EntityProperty > properties )
56
+ public static object ToObject ( Type type , IDictionary < string , object > properties )
62
57
{
63
58
var jo = new JObject ( ) ;
64
59
foreach ( var pair in properties )
65
60
{
66
- ApplyProperty ( jo , pair . Key , pair . Value ) ;
61
+ jo . Add ( pair . Key , new JValue ( pair . Value ) ) ;
67
62
}
68
63
return jo . ToObject ( type ) ;
69
64
}
70
65
71
- public static bool TryGetEntityProperty ( JProperty property , out EntityProperty entityProperty )
66
+ public static bool TryGetEntityProperty ( JProperty property , out object entityProperty )
72
67
{
73
68
entityProperty = null ;
74
69
var value = property . Value ;
75
70
76
71
switch ( value . Type )
77
72
{
78
73
case JTokenType . Bytes :
79
- entityProperty = new EntityProperty ( value . ToObject < byte [ ] > ( ) ) ;
74
+ entityProperty = value . ToObject < byte [ ] > ( ) ;
80
75
return true ;
81
76
case JTokenType . Boolean :
82
- entityProperty = new EntityProperty ( value . ToObject < bool > ( ) ) ;
77
+ entityProperty = value . ToObject < bool > ( ) ;
83
78
return true ;
84
79
case JTokenType . Date :
85
- entityProperty = new EntityProperty ( value . ToObject < DateTime > ( ) ) ;
80
+ entityProperty = value . ToObject < DateTime > ( ) ;
86
81
return true ;
87
82
case JTokenType . Float :
88
- entityProperty = new EntityProperty ( value . ToObject < double > ( ) ) ;
83
+ entityProperty = value . ToObject < double > ( ) ;
89
84
return true ;
90
85
case JTokenType . Guid :
91
- entityProperty = new EntityProperty ( value . ToObject < Guid > ( ) ) ;
86
+ entityProperty = value . ToObject < Guid > ( ) ;
92
87
return true ;
93
88
case JTokenType . Integer :
94
89
// to handle both ints and longs, we normalize integer values
95
90
// to type long
96
- entityProperty = new EntityProperty ( value . ToObject < long > ( ) ) ;
91
+ entityProperty = value . ToObject < long > ( ) ;
97
92
return true ;
98
93
case JTokenType . String :
99
94
case JTokenType . TimeSpan :
100
- entityProperty = new EntityProperty ( value . ToObject < string > ( ) ) ;
95
+ entityProperty = value . ToObject < string > ( ) ;
101
96
return true ;
102
97
default :
103
98
return false ;
104
99
}
105
100
}
106
-
107
- public static void ApplyProperty ( JObject jo , string name , EntityProperty entityProperty )
108
- {
109
- switch ( entityProperty . PropertyType )
110
- {
111
- case EdmType . Binary :
112
- jo . Add ( name , new JValue ( entityProperty . BinaryValue ) ) ;
113
- return ;
114
- case EdmType . Boolean :
115
- jo . Add ( name , new JValue ( entityProperty . BooleanValue ) ) ;
116
- return ;
117
- case EdmType . DateTime :
118
- jo . Add ( name , new JValue ( entityProperty . DateTime ) ) ;
119
- return ;
120
- case EdmType . Double :
121
- jo . Add ( name , new JValue ( entityProperty . DoubleValue ) ) ;
122
- return ;
123
- case EdmType . Guid :
124
- jo . Add ( name , new JValue ( entityProperty . GuidValue ) ) ;
125
- return ;
126
- case EdmType . Int32 :
127
- jo . Add ( name , new JValue ( entityProperty . Int32Value ) ) ;
128
- return ;
129
- case EdmType . Int64 :
130
- jo . Add ( name , new JValue ( entityProperty . Int64Value ) ) ;
131
- return ;
132
- case EdmType . String :
133
- jo . Add ( name , new JValue ( entityProperty . StringValue ) ) ;
134
- return ;
135
- default :
136
- return ;
137
- }
138
- }
139
101
}
140
- }
102
+ }
0 commit comments