Skip to content

Commit b8577b5

Browse files
authored
[geneva] Nullable annotations for TLDExporter folder (open-telemetry#2085)
1 parent f66086b commit b8577b5

File tree

5 files changed

+149
-163
lines changed

5 files changed

+149
-163
lines changed

src/OpenTelemetry.Exporter.Geneva/TLDExporter/JsonSerializer.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
using System.Globalization;
57
using System.Runtime.CompilerServices;
68
using System.Text;
@@ -46,7 +48,7 @@ public static int SerializeNull(byte[] buffer, int cursor)
4648
}
4749

4850
[MethodImpl(MethodImplOptions.AggressiveInlining)]
49-
public static string SerializeString(string value)
51+
public static string SerializeString(string? value)
5052
{
5153
var buffer = ThreadLocalBuffer.Value;
5254
if (buffer == null)
@@ -60,7 +62,7 @@ public static string SerializeString(string value)
6062
}
6163

6264
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63-
public static int SerializeString(byte[] buffer, int cursor, string value)
65+
public static int SerializeString(byte[] buffer, int cursor, string? value)
6466
{
6567
if (value == null)
6668
{
@@ -90,7 +92,7 @@ public static int SerializeString(byte[] buffer, int cursor, ReadOnlySpan<char>
9092
#endif
9193

9294
[MethodImpl(MethodImplOptions.AggressiveInlining)]
93-
public static string SerializeArray<T>(T[] array)
95+
public static string SerializeArray<T>(T[]? array)
9496
{
9597
var buffer = ThreadLocalBuffer.Value;
9698
if (buffer == null)
@@ -104,7 +106,7 @@ public static string SerializeArray<T>(T[] array)
104106
}
105107

106108
[MethodImpl(MethodImplOptions.AggressiveInlining)]
107-
public static int SerializeArray<T>(byte[] buffer, int cursor, T[] array)
109+
public static int SerializeArray<T>(byte[] buffer, int cursor, T[]? array)
108110
{
109111
if (array == null)
110112
{
@@ -128,7 +130,7 @@ public static int SerializeArray<T>(byte[] buffer, int cursor, T[] array)
128130
}
129131

130132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
131-
public static string SerializeMap(IEnumerable<KeyValuePair<string, object>> map)
133+
public static string SerializeMap(IEnumerable<KeyValuePair<string, object?>>? map)
132134
{
133135
var buffer = ThreadLocalBuffer.Value;
134136
if (buffer == null)
@@ -142,7 +144,7 @@ public static string SerializeMap(IEnumerable<KeyValuePair<string, object>> map)
142144
}
143145

144146
[MethodImpl(MethodImplOptions.AggressiveInlining)]
145-
public static byte[] SerializeKeyValuePairsListAsBytes(List<KeyValuePair<string, object>> listKVp, out int count)
147+
public static byte[] SerializeKeyValuePairsListAsBytes(List<KeyValuePair<string, object?>>? listKVp, out int count)
146148
{
147149
var buffer = ThreadLocalBuffer.Value;
148150
if (buffer == null)
@@ -156,7 +158,7 @@ public static byte[] SerializeKeyValuePairsListAsBytes(List<KeyValuePair<string,
156158
}
157159

158160
[MethodImpl(MethodImplOptions.AggressiveInlining)]
159-
public static int SerializeMap(byte[] buffer, int cursor, IEnumerable<KeyValuePair<string, object>> map)
161+
public static int SerializeMap(byte[] buffer, int cursor, IEnumerable<KeyValuePair<string, object?>>? map)
160162
{
161163
if (map == null)
162164
{
@@ -183,7 +185,7 @@ public static int SerializeMap(byte[] buffer, int cursor, IEnumerable<KeyValuePa
183185
}
184186

185187
[MethodImpl(MethodImplOptions.AggressiveInlining)]
186-
public static int SerializeKeyValuePairList(byte[] buffer, int cursor, List<KeyValuePair<string, object>> listKvp)
188+
public static int SerializeKeyValuePairList(byte[] buffer, int cursor, List<KeyValuePair<string, object?>>? listKvp)
187189
{
188190
if (listKvp == null)
189191
{
@@ -210,7 +212,7 @@ public static int SerializeKeyValuePairList(byte[] buffer, int cursor, List<KeyV
210212
}
211213

212214
[MethodImpl(MethodImplOptions.AggressiveInlining)]
213-
public static string Serialize(object obj)
215+
public static string Serialize(object? obj)
214216
{
215217
var buffer = ThreadLocalBuffer.Value;
216218
if (buffer == null)
@@ -224,7 +226,7 @@ public static string Serialize(object obj)
224226
}
225227

226228
[MethodImpl(MethodImplOptions.AggressiveInlining)]
227-
public static int Serialize(byte[] buffer, int cursor, object obj)
229+
public static int Serialize(byte[] buffer, int cursor, object? obj)
228230
{
229231
if (obj == null)
230232
{
@@ -247,7 +249,7 @@ public static int Serialize(byte[] buffer, int cursor, object obj)
247249
case float:
248250
case double:
249251
Span<char> tmp = stackalloc char[MAX_STACK_ALLOC_SIZE_IN_BYTES / sizeof(char)];
250-
(obj as ISpanFormattable).TryFormat(tmp, out int charsWritten, default, CultureInfo.InvariantCulture);
252+
((ISpanFormattable)obj).TryFormat(tmp, out int charsWritten, default, CultureInfo.InvariantCulture);
251253
return WriteString(buffer, cursor, tmp.Slice(0, charsWritten));
252254
case DateTime dt:
253255
tmp = stackalloc char[MAX_STACK_ALLOC_SIZE_IN_BYTES / sizeof(char)];
@@ -297,7 +299,7 @@ public static int Serialize(byte[] buffer, int cursor, object obj)
297299
return SerializeArray(buffer, cursor, vdtarray);
298300
case string v:
299301
return SerializeString(buffer, cursor, v);
300-
case IEnumerable<KeyValuePair<string, object>> v:
302+
case IEnumerable<KeyValuePair<string, object?>> v:
301303
return SerializeMap(buffer, cursor, v);
302304
case object[] v:
303305
return SerializeArray(buffer, cursor, v);
@@ -314,7 +316,7 @@ public static int Serialize(byte[] buffer, int cursor, object obj)
314316
#endif
315317

316318
default:
317-
string repr;
319+
string? repr;
318320
try
319321
{
320322
repr = Convert.ToString(obj, CultureInfo.InvariantCulture);

src/OpenTelemetry.Exporter.Geneva/TLDExporter/TldExporter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
6+
using System.Diagnostics;
47
using System.Globalization;
58
using System.Runtime.CompilerServices;
69
using System.Text;
@@ -35,6 +38,8 @@ internal abstract class TldExporter
3538
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3639
protected static void Serialize(EventBuilder eb, string key, object value)
3740
{
41+
Debug.Assert(value != null, "value was null");
42+
3843
switch (value)
3944
{
4045
case bool vb:
@@ -124,11 +129,11 @@ protected static void Serialize(EventBuilder eb, string key, object value)
124129
string repr;
125130
try
126131
{
127-
repr = Convert.ToString(value, CultureInfo.InvariantCulture);
132+
repr = Convert.ToString(value, CultureInfo.InvariantCulture) ?? string.Empty;
128133
}
129134
catch
130135
{
131-
repr = $"ERROR: type {value.GetType().FullName} is not supported";
136+
repr = $"ERROR: type {value!.GetType().FullName} is not supported";
132137
}
133138

134139
eb.AddCountedAnsiString(key, repr, Encoding.UTF8, 0, Math.Min(repr.Length, StringLengthLimit));

0 commit comments

Comments
 (0)