Skip to content

Commit 20f6a32

Browse files
author
Timothy Mothra
authored
[AzureMonitor] fix AOT warnings (part 1) (Azure#49601)
* fix AOT warnings * code cleanup * cleanup
1 parent 139963f commit 20f6a32

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/src/Internals/Filtering/DerivedMetric.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ private void CreateProjection()
201201
MethodInfo? parseMethod = typeof(TimeSpan).GetMethod("Parse", new[] { typeof(string) });
202202
fieldExpression = Expression.Call(parseMethod!, fieldExpression);
203203
}
204-
fieldExpression = Expression.Property(fieldExpression, "TotalMilliseconds");
204+
205+
var totalMillisecondsProperty = typeof(TimeSpan).GetProperty(nameof(TimeSpan.TotalMilliseconds))!;
206+
fieldExpression = Expression.Property(fieldExpression, totalMillisecondsProperty);
205207
}
206208
}
207209

sdk/monitor/Azure.Monitor.OpenTelemetry.LiveMetrics/src/Internals/Filtering/Filter.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,22 @@ private Expression ProduceComparatorExpressionForSingleFieldCondition(Expression
447447

448448
Type enumUnderlyingType = fieldType.GetTypeInfo().GetEnumUnderlyingType();
449449

450+
// This block matches the case statements just above.
451+
static Type GetNullableType(Type inputType) => inputType switch
452+
{
453+
_ when inputType == typeof(sbyte) => typeof(sbyte?),
454+
_ when inputType == typeof(short) => typeof(short?),
455+
_ when inputType == typeof(int) => typeof(int?),
456+
_ when inputType == typeof(long) => typeof(long?),
457+
_ when inputType == typeof(byte) => typeof(byte?),
458+
_ when inputType == typeof(ushort) => typeof(ushort?),
459+
_ when inputType == typeof(uint) => typeof(uint?),
460+
_ when inputType == typeof(ulong) => typeof(ulong?),
461+
_ when inputType == typeof(float) => typeof(float?),
462+
_ when inputType == typeof(double) => typeof(double?),
463+
_ => throw new ArgumentException($"Cannot create a nullable type for {inputType.FullName}."),
464+
};
465+
450466
switch (predicate)
451467
{
452468
case Predicate.Equal:
@@ -458,28 +474,28 @@ private Expression ProduceComparatorExpressionForSingleFieldCondition(Expression
458474
case Predicate.LessThan:
459475
// (int)fieldValue < (int)enumValue
460476
// (int?)fieldValue < (int?)enumValue
461-
Type underlyingType = isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(enumUnderlyingType) : enumUnderlyingType;
477+
Type underlyingType = isFieldTypeNullable ? GetNullableType(enumUnderlyingType) : enumUnderlyingType;
462478
return Expression.LessThan(
463479
Expression.Convert(fieldExpression, underlyingType),
464480
Expression.Convert(Expression.Constant(enumValue, fieldType), underlyingType));
465481
case Predicate.GreaterThan:
466482
// (int)fieldValue > (int)enumValue
467483
// (int?)fieldValue > (int?)enumValue
468-
underlyingType = isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(enumUnderlyingType) : enumUnderlyingType;
484+
underlyingType = isFieldTypeNullable ? GetNullableType(enumUnderlyingType) : enumUnderlyingType;
469485
return Expression.GreaterThan(
470486
Expression.Convert(fieldExpression, underlyingType),
471487
Expression.Convert(Expression.Constant(enumValue, fieldType), underlyingType));
472488
case Predicate.LessThanOrEqual:
473489
// (int)fieldValue <= (int)enumValue
474490
// (int?)fieldValue <= (int?)enumValue
475-
underlyingType = isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(enumUnderlyingType) : enumUnderlyingType;
491+
underlyingType = isFieldTypeNullable ? GetNullableType(enumUnderlyingType) : enumUnderlyingType;
476492
return Expression.LessThanOrEqual(
477493
Expression.Convert(fieldExpression, underlyingType),
478494
Expression.Convert(Expression.Constant(enumValue, fieldType), underlyingType));
479495
case Predicate.GreaterThanOrEqual:
480496
// (int)fieldValue >= (int)enumValue
481497
// (int?)fieldValue >= (int?)enumValue
482-
underlyingType = isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(enumUnderlyingType) : enumUnderlyingType;
498+
underlyingType = isFieldTypeNullable ? GetNullableType(enumUnderlyingType) : enumUnderlyingType;
483499
return Expression.GreaterThanOrEqual(
484500
Expression.Convert(fieldExpression, underlyingType),
485501
Expression.Convert(Expression.Constant(enumValue, fieldType), underlyingType));
@@ -513,32 +529,32 @@ private Expression ProduceComparatorExpressionForSingleFieldCondition(Expression
513529
ThrowOnInvalidFilter(fieldType, !comparandDouble.HasValue);
514530
return Expression.Equal(
515531
fieldConvertedExpression,
516-
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(typeof(double)) : typeof(double)));
532+
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(double?) : typeof(double)));
517533
case Predicate.NotEqual:
518534
ThrowOnInvalidFilter(fieldType, !comparandDouble.HasValue);
519535
return Expression.NotEqual(
520536
fieldConvertedExpression,
521-
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(typeof(double)) : typeof(double)));
537+
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(double?) : typeof(double)));
522538
case Predicate.LessThan:
523539
ThrowOnInvalidFilter(fieldType, !comparandDouble.HasValue);
524540
return Expression.LessThan(
525541
fieldConvertedExpression,
526-
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(typeof(double)) : typeof(double)));
542+
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(double?) : typeof(double)));
527543
case Predicate.GreaterThan:
528544
ThrowOnInvalidFilter(fieldType, !comparandDouble.HasValue);
529545
return Expression.GreaterThan(
530546
fieldConvertedExpression,
531-
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(typeof(double)) : typeof(double)));
547+
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(double?) : typeof(double)));
532548
case Predicate.LessThanOrEqual:
533549
ThrowOnInvalidFilter(fieldType, !comparandDouble.HasValue);
534550
return Expression.LessThanOrEqual(
535551
fieldConvertedExpression,
536-
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(typeof(double)) : typeof(double)));
552+
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(double?) : typeof(double)));
537553
case Predicate.GreaterThanOrEqual:
538554
ThrowOnInvalidFilter(fieldType, !comparandDouble.HasValue);
539555
return Expression.GreaterThanOrEqual(
540556
fieldConvertedExpression,
541-
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(Nullable<>).MakeGenericType(typeof(double)) : typeof(double)));
557+
Expression.Constant(comparandDouble.Value, isFieldTypeNullable ? typeof(double?) : typeof(double)));
542558
case Predicate.Contains:
543559
// fieldValue.ToString(CultureInfo.InvariantCulture).IndexOf(this.comparand, StringComparison.OrdinalIgnoreCase) != -1
544560
Expression toStringCall = isFieldTypeNullable

0 commit comments

Comments
 (0)