Skip to content

Commit 1ce06be

Browse files
authored
Return nullable values from LogsTableRow (Azure#23990)
1 parent 17180e2 commit 1ce06be

File tree

4 files changed

+80
-92
lines changed

4 files changed

+80
-92
lines changed

sdk/monitor/Azure.Monitor.Query/api/Azure.Monitor.Query.netstandard2.0.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,34 +142,34 @@ internal LogsTableColumn() { }
142142
public Azure.Monitor.Query.Models.LogsColumnType Type { get { throw null; } }
143143
public override string ToString() { throw null; }
144144
}
145-
public partial class LogsTableRow
145+
public partial class LogsTableRow : System.Collections.Generic.IEnumerable<object>, System.Collections.Generic.IReadOnlyCollection<object>, System.Collections.Generic.IReadOnlyList<object>, System.Collections.IEnumerable
146146
{
147147
internal LogsTableRow() { }
148148
public int Count { get { throw null; } }
149149
public object this[int index] { get { throw null; } }
150150
public object this[string name] { get { throw null; } }
151-
public bool GetBoolean(int index) { throw null; }
152-
public bool GetBoolean(string name) { throw null; }
153-
public System.DateTimeOffset GetDateTimeOffset(int index) { throw null; }
154-
public System.DateTimeOffset GetDateTimeOffset(string name) { throw null; }
155-
public decimal GetDecimal(int index) { throw null; }
156-
public decimal GetDecimal(string name) { throw null; }
157-
public double GetDouble(int index) { throw null; }
158-
public double GetDouble(string name) { throw null; }
151+
public bool? GetBoolean(int index) { throw null; }
152+
public bool? GetBoolean(string name) { throw null; }
153+
public System.DateTimeOffset? GetDateTimeOffset(int index) { throw null; }
154+
public System.DateTimeOffset? GetDateTimeOffset(string name) { throw null; }
155+
public decimal? GetDecimal(int index) { throw null; }
156+
public decimal? GetDecimal(string name) { throw null; }
157+
public double? GetDouble(int index) { throw null; }
158+
public double? GetDouble(string name) { throw null; }
159159
public System.BinaryData GetDynamic(int index) { throw null; }
160160
public System.BinaryData GetDynamic(string name) { throw null; }
161-
public System.Guid GetGuid(int index) { throw null; }
162-
public System.Guid GetGuid(string name) { throw null; }
163-
public int GetInt32(int index) { throw null; }
164-
public int GetInt32(string name) { throw null; }
165-
public long GetInt64(int index) { throw null; }
166-
public long GetInt64(string name) { throw null; }
161+
public System.Guid? GetGuid(int index) { throw null; }
162+
public System.Guid? GetGuid(string name) { throw null; }
163+
public int? GetInt32(int index) { throw null; }
164+
public int? GetInt32(string name) { throw null; }
165+
public long? GetInt64(int index) { throw null; }
166+
public long? GetInt64(string name) { throw null; }
167167
public string GetString(int index) { throw null; }
168168
public string GetString(string name) { throw null; }
169-
public System.TimeSpan GetTimeSpan(int index) { throw null; }
170-
public System.TimeSpan GetTimeSpan(string name) { throw null; }
171-
public bool IsNull(int index) { throw null; }
172-
public bool IsNull(string name) { throw null; }
169+
public System.TimeSpan? GetTimeSpan(int index) { throw null; }
170+
public System.TimeSpan? GetTimeSpan(string name) { throw null; }
171+
System.Collections.Generic.IEnumerator<object> System.Collections.Generic.IEnumerable<System.Object>.GetEnumerator() { throw null; }
172+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
173173
public override string ToString() { throw null; }
174174
}
175175
public enum MetricAggregationType

sdk/monitor/Azure.Monitor.Query/src/Models/LogsTableRow.cs

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections;
56
using System.Collections.Generic;
67
using System.Globalization;
78
using System.Text.Json;
@@ -13,7 +14,7 @@ namespace Azure.Monitor.Query.Models
1314
/// Represents a row in the table of results returned from the logs query.
1415
/// </summary>
1516
[CodeGenModel("LogsQueryResultRow")]
16-
public class LogsTableRow
17+
public class LogsTableRow: IReadOnlyList<object>
1718
{
1819
private readonly Dictionary<string, int> _columnMap;
1920
private readonly IReadOnlyList<LogsTableColumn> _columns;
@@ -35,113 +36,106 @@ internal LogsTableRow(Dictionary<string, int> columnMap, IReadOnlyList<LogsTable
3536
/// Gets the value of the column at the specified index as <see cref="int"/>.
3637
/// </summary>
3738
/// <param name="index">The column index.</param>
38-
/// <returns>The <see cref="int"/> value of the column.</returns>
39-
public int GetInt32(int index) => _row[index].GetInt32();
39+
/// <returns>The <see cref="Nullable{Int32}"/> value of the column.</returns>
40+
public int? GetInt32(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : _row[index].GetInt32();
4041

4142
/// <summary>
4243
/// Gets the value of the column at the specified index as <see cref="long"/>.
4344
/// </summary>
4445
/// <param name="index">The column index.</param>
45-
/// <returns>The <see cref="long"/> value of the column.</returns>
46-
public long GetInt64(int index) => _row[index].GetInt64();
46+
/// <returns>The <see cref="Nullable{Int64}"/> value of the column.</returns>
47+
public long? GetInt64(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : _row[index].GetInt64();
4748

4849
/// <summary>
4950
/// Gets the value of the column at the specified index as <see cref="bool"/>.
5051
/// </summary>
5152
/// <param name="index">The column index.</param>
52-
/// <returns>The <see cref="bool"/> value of the column.</returns>
53-
public bool GetBoolean(int index) => _row[index].GetBoolean();
53+
/// <returns>The <see cref="Nullable{Boolean}"/> value of the column.</returns>
54+
public bool? GetBoolean(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : _row[index].GetBoolean();
5455

5556
/// <summary>
5657
/// Gets the value of the column at the specified index as <see cref="decimal"/>.
5758
/// </summary>
5859
/// <param name="index">The column index.</param>
59-
/// <returns>The <see cref="decimal"/> value of the column.</returns>
60-
public decimal GetDecimal(int index) => decimal.Parse(_row[index].GetString(), CultureInfo.InvariantCulture);
60+
/// <returns>The <see cref="Nullable{Decimal}"/> value of the column.</returns>
61+
public decimal? GetDecimal(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : decimal.Parse(_row[index].GetString(), CultureInfo.InvariantCulture);
6162

6263
/// <summary>
6364
/// Gets the value of the column at the specified index as <see cref="double"/>.
6465
/// </summary>
6566
/// <param name="index">The column index.</param>
66-
/// <returns>The <see cref="double"/> value of the column.</returns>
67-
public double GetDouble(int index) => _row[index].GetDouble();
67+
/// <returns>The <see cref="Nullable{Double}"/> value of the column.</returns>
68+
public double? GetDouble(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : _row[index].GetDouble();
6869

6970
/// <summary>
7071
/// Gets the value of the column at the specified index as <see cref="string"/>.
7172
/// </summary>
7273
/// <param name="index">The column index.</param>
7374
/// <returns>The <see cref="string"/> value of the column.</returns>
74-
public string GetString(int index) => _row[index].GetString();
75+
public string GetString(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : _row[index].GetString();
7576

7677
/// <summary>
7778
/// Gets the value of the column at the specified index as <see cref="DateTimeOffset"/>.
7879
/// </summary>
7980
/// <param name="index">The column index.</param>
80-
/// <returns>The <see cref="DateTimeOffset"/> value of the column.</returns>
81-
public DateTimeOffset GetDateTimeOffset(int index) => _row[index].GetDateTimeOffset();
81+
/// <returns>The <see cref="Nullable{DateTimeOffset}"/> value of the column.</returns>
82+
public DateTimeOffset? GetDateTimeOffset(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : _row[index].GetDateTimeOffset();
8283

8384
/// <summary>
8485
/// Gets the value of the column at the specified index as <see cref="TimeSpan"/>.
8586
/// </summary>
8687
/// <param name="index">The column index.</param>
87-
/// <returns>The <see cref="TimeSpan"/> value of the column.</returns>
88-
public TimeSpan GetTimeSpan(int index) => _row[index].GetTimeSpan("c");
88+
/// <returns>The <see cref="Nullable{TimeSpan}"/> value of the column.</returns>
89+
public TimeSpan? GetTimeSpan(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : _row[index].GetTimeSpan("c");
8990

9091
/// <summary>
9192
/// Gets the value of the column at the specified index as <see cref="Guid"/>.
9293
/// </summary>
9394
/// <param name="index">The column index.</param>
94-
/// <returns>The <see cref="Guid"/> value of the column.</returns>
95-
public Guid GetGuid(int index) => _row[index].GetGuid();
95+
/// <returns>The <see cref="Nullable{Guid}"/> value of the column.</returns>
96+
public Guid? GetGuid(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : _row[index].GetGuid();
9697

9798
/// <summary>
9899
/// Gets the value of the column at the specified index as <see cref="BinaryData"/>.
99100
/// </summary>
100101
/// <param name="index">The column index.</param>
101102
/// <returns>The <see cref="BinaryData"/> value of the column.</returns>
102-
public BinaryData GetDynamic(int index) => new BinaryData(_row[index].GetString());
103-
104-
/// <summary>
105-
/// Returns <c>true</c> if the value of the column at the specified index is <c>null</c>, otherwise <c>false</c>.
106-
/// </summary>
107-
/// <param name="index">The column index.</param>
108-
/// <returns><c>true</c> if the value is <c>null</c>, otherwise <c>false</c>.</returns>
109-
public bool IsNull(int index) => _row[index].ValueKind == JsonValueKind.Null;
103+
public BinaryData GetDynamic(int index) => _row[index].ValueKind == JsonValueKind.Null ? null : new BinaryData(_row[index].GetString());
110104

111105
/// <summary>
112106
/// Gets the value of the column with the specified name as <see cref="int"/>.
113107
/// </summary>
114108
/// <param name="name">The column name.</param>
115-
/// <returns>The <see cref="int"/> value of the column.</returns>
116-
public int GetInt32(string name) => GetInt32(_columnMap[name]);
109+
/// <returns>The <see cref="Nullable{Int32}"/> value of the column.</returns>
110+
public int? GetInt32(string name) => GetInt32(_columnMap[name]);
117111

118112
/// <summary>
119113
/// Gets the value of the column with the specified name as <see cref="long"/>.
120114
/// </summary>
121115
/// <param name="name">The column name.</param>
122-
/// <returns>The <see cref="long"/> value of the column.</returns>
123-
public long GetInt64(string name) => GetInt64(_columnMap[name]);
116+
/// <returns>The <see cref="Nullable{Int64}"/> value of the column.</returns>
117+
public long? GetInt64(string name) => GetInt64(_columnMap[name]);
124118

125119
/// <summary>
126120
/// Gets the value of the column with the specified name as <see cref="bool"/>.
127121
/// </summary>
128122
/// <param name="name">The column name.</param>
129-
/// <returns>The <see cref="bool"/> value of the column.</returns>
130-
public bool GetBoolean(string name) => GetBoolean(_columnMap[name]);
123+
/// <returns>The <see cref="Nullable{Boolean}"/> value of the column.</returns>
124+
public bool? GetBoolean(string name) => GetBoolean(_columnMap[name]);
131125

132126
/// <summary>
133127
/// Gets the value of the column with the specified name as <see cref="decimal"/>.
134128
/// </summary>
135129
/// <param name="name">The column name.</param>
136-
/// <returns>The <see cref="decimal"/> value of the column.</returns>
137-
public decimal GetDecimal(string name) => GetDecimal(_columnMap[name]);
130+
/// <returns>The <see cref="Nullable{Decimal}"/> value of the column.</returns>
131+
public decimal? GetDecimal(string name) => GetDecimal(_columnMap[name]);
138132

139133
/// <summary>
140134
/// Gets the value of the column with the specified name as <see cref="double"/>.
141135
/// </summary>
142136
/// <param name="name">The column name.</param>
143-
/// <returns>The <see cref="double"/> value of the column.</returns>
144-
public double GetDouble(string name) => GetDouble(_columnMap[name]);
137+
/// <returns>The <see cref="Nullable{Double}"/> value of the column.</returns>
138+
public double? GetDouble(string name) => GetDouble(_columnMap[name]);
145139

146140
/// <summary>
147141
/// Gets the value of the column with the specified name as <see cref="string"/>.
@@ -154,22 +148,22 @@ internal LogsTableRow(Dictionary<string, int> columnMap, IReadOnlyList<LogsTable
154148
/// Gets the value of the column with the specified name as <see cref="DateTimeOffset"/>.
155149
/// </summary>
156150
/// <param name="name">The column name.</param>
157-
/// <returns>The <see cref="DateTimeOffset"/> value of the column.</returns>
158-
public DateTimeOffset GetDateTimeOffset(string name) => GetDateTimeOffset(_columnMap[name]);
151+
/// <returns>The <see cref="Nullable{DateTimeOffset}"/> value of the column.</returns>
152+
public DateTimeOffset? GetDateTimeOffset(string name) => GetDateTimeOffset(_columnMap[name]);
159153

160154
/// <summary>
161155
/// Gets the value of the column with the specified name as <see cref="TimeSpan"/>.
162156
/// </summary>
163157
/// <param name="name">The column name.</param>
164-
/// <returns>The <see cref="TimeSpan"/> value of the column.</returns>
165-
public TimeSpan GetTimeSpan(string name) => GetTimeSpan(_columnMap[name]);
158+
/// <returns>The <see cref="Nullable{TimeSpan}"/> value of the column.</returns>
159+
public TimeSpan? GetTimeSpan(string name) => GetTimeSpan(_columnMap[name]);
166160

167161
/// <summary>
168162
/// Gets the value of the column with the specified name as <see cref="Guid"/>.
169163
/// </summary>
170164
/// <param name="name">The column name.</param>
171-
/// <returns>The <see cref="Guid"/> value of the column.</returns>
172-
public Guid GetGuid(string name) => GetGuid(_columnMap[name]);
165+
/// <returns>The <see cref="Nullable{Guid}"/> value of the column.</returns>
166+
public Guid? GetGuid(string name) => GetGuid(_columnMap[name]);
173167

174168
/// <summary>
175169
/// Gets the value of the column with the specified name as <see cref="Guid"/>.
@@ -178,25 +172,13 @@ internal LogsTableRow(Dictionary<string, int> columnMap, IReadOnlyList<LogsTable
178172
/// <returns>The <see cref="BinaryData"/> value of the column.</returns>
179173
public BinaryData GetDynamic(string name) => GetDynamic(_columnMap[name]);
180174

181-
/// <summary>
182-
/// Returns true if the value of the column with the specified name is null, otherwise false.
183-
/// </summary>
184-
/// <param name="name">The column name.</param>
185-
/// <returns><c>true</c> if the value is <c>null</c>, otherwise <c>false</c>.</returns>
186-
public bool IsNull(string name) => IsNull(_columnMap[name]);
187-
188175
/// <summary>
189176
/// Gets the value of the column at the specified index as <see cref="object"/>.
190177
/// </summary>
191178
/// <param name="index">The column index.</param>
192179
/// <returns>The <see cref="object"/> value of the column.</returns>
193180
internal object GetObject(int index)
194181
{
195-
if (IsNull(index))
196-
{
197-
return null;
198-
}
199-
200182
var element = _row[index];
201183
switch (_columns[index].Type.ToString())
202184
{
@@ -278,5 +260,20 @@ public override string ToString()
278260
{
279261
return _row.ToString();
280262
}
263+
264+
/// <inheritdoc />
265+
IEnumerator IEnumerable.GetEnumerator()
266+
{
267+
return ((IEnumerable<object>)this).GetEnumerator();
268+
}
269+
270+
/// <inheritdoc />
271+
IEnumerator<object> IEnumerable<object>.GetEnumerator()
272+
{
273+
for (int i = 0; i < Count; i++)
274+
{
275+
yield return GetObject(i);
276+
}
277+
}
281278
}
282279
}

sdk/monitor/Azure.Monitor.Query/src/RowBinder.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ protected override bool TryGet<T>(BoundMemberInfo memberInfo, LogsTableRow sourc
6565
return false;
6666
}
6767

68-
if (source.IsNull(column) &&
69-
(!typeof(T).IsValueType ||
70-
typeof(T).IsGenericType &&
71-
typeof(T).GetGenericTypeDefinition() == typeof(Nullable<>)))
72-
{
73-
value = default;
74-
return true;
75-
}
76-
7768
if (typeof(T) == typeof(int)) value = (T)(object)source.GetInt32(column);
7869
else if (typeof(T) == typeof(string)) value = (T)(object)source.GetString(column);
7970
else if (typeof(T) == typeof(bool)) value = (T)(object)source.GetBoolean(column);
@@ -86,14 +77,14 @@ protected override bool TryGet<T>(BoundMemberInfo memberInfo, LogsTableRow sourc
8677
else if (typeof(T) == typeof(TimeSpan)) value = (T)(object)source.GetTimeSpan(column);
8778
else if (typeof(T) == typeof(BinaryData)) value = (T)(object)source.GetDynamic(column);
8879

89-
else if (typeof(T) == typeof(int?)) value = (T)(object)(int?)source.GetInt32(column);
90-
else if (typeof(T) == typeof(bool?)) value = (T)(object)(bool?)source.GetBoolean(column);
91-
else if (typeof(T) == typeof(long?)) value = (T)(object)(long?)source.GetInt64(column);
92-
else if (typeof(T) == typeof(decimal?)) value = (T)(object)(decimal?)source.GetDecimal(column);
93-
else if (typeof(T) == typeof(double?)) value = (T)(object)(double?)source.GetDouble(column);
94-
else if (typeof(T) == typeof(Guid?)) value = (T)(object)(Guid?)source.GetGuid(column);
95-
else if (typeof(T) == typeof(DateTimeOffset?)) value = (T)(object)(DateTimeOffset?)source.GetDateTimeOffset(column);
96-
else if (typeof(T) == typeof(TimeSpan?)) value = (T)(object)(TimeSpan?)source.GetTimeSpan(column);
80+
else if (typeof(T) == typeof(int?)) value = (T)(object)source.GetInt32(column);
81+
else if (typeof(T) == typeof(bool?)) value = (T)(object)source.GetBoolean(column);
82+
else if (typeof(T) == typeof(long?)) value = (T)(object)source.GetInt64(column);
83+
else if (typeof(T) == typeof(decimal?)) value = (T)(object)source.GetDecimal(column);
84+
else if (typeof(T) == typeof(double?)) value = (T)(object)source.GetDouble(column);
85+
else if (typeof(T) == typeof(Guid?)) value = (T)(object)source.GetGuid(column);
86+
else if (typeof(T) == typeof(DateTimeOffset?)) value = (T)(object)source.GetDateTimeOffset(column);
87+
else if (typeof(T) == typeof(TimeSpan?)) value = (T)(object)source.GetTimeSpan(column);
9788

9889
else
9990
{

sdk/monitor/Azure.Monitor.Query/tests/LogsQueryClientClientLiveTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ public async Task CanQueryAllSupportedTypes()
285285
Assert.AreEqual(0.10101m, row.GetDecimal("Decimal"));
286286
Assert.AreEqual(0.10101m, row.GetDecimal(8));
287287
Assert.AreEqual(0.10101m, row.GetObject("Decimal"));
288-
Assert.True(row.IsNull("NullBool"));
289-
Assert.True(row.IsNull(9));
288+
Assert.Null(row.GetBoolean("NullBool"));
289+
Assert.Null(row.GetBoolean(9));
290290
Assert.IsNull(row.GetObject("NullBool"));
291291
Assert.AreEqual("{\"a\":123,\"b\":\"hello\",\"c\":[1,2,3],\"d\":{}}", row.GetDynamic(10).ToString());
292292
Assert.AreEqual("{\"a\":123,\"b\":\"hello\",\"c\":[1,2,3],\"d\":{}}", row.GetDynamic("Dynamic").ToString());

0 commit comments

Comments
 (0)