Skip to content

Commit

Permalink
.Net: Fix a few issues found via compiler warnings (#4332)
Browse files Browse the repository at this point in the history
I temporarily added a net8.0 target to a few projects and fixed the
resulting issues.
  • Loading branch information
stephentoub committed Dec 18, 2023
1 parent 3bb9630 commit 06c6315
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion dotnet/src/SemanticKernel.Abstractions/AI/PromptNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.SemanticKernel;
/// <summary>
/// Class that contains information about node in prompt.
/// </summary>
internal class PromptNode
internal sealed class PromptNode
{
private Dictionary<string, string>? _attributes;
private List<PromptNode>? _childNodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public FunctionResult(KernelFunction function, object? value = null, CultureInfo
{
if (typeof(T) == typeof(string))
{
return (T)(object)content.ToString();
return (T?)(object?)content.ToString();
}

if (content.InnerContent is T innerContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void LogFunctionResultValue(this ILogger logger, object? resultVal
var jsonString = resultValue?.GetType() == typeof(string)
? resultValue.ToString()
: JsonSerializer.Serialize(resultValue);
s_logFunctionResultValue(logger, jsonString, null);
s_logFunctionResultValue(logger, jsonString ?? string.Empty, null);
}
catch (NotSupportedException ex)
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/SemanticKernel.Abstractions/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public CultureInfo Culture
// mapping. We can query for that service and and then use it to try to get a service.
if (this.Services.GetKeyedService<Dictionary<Type, HashSet<object?>>>(KernelServiceTypeToKeyMappings) is { } typeToKeyMappings)
{
if (typeToKeyMappings.TryGetValue(typeof(T), out HashSet<object?> keys))
if (typeToKeyMappings.TryGetValue(typeof(T), out HashSet<object?>? keys))
{
return keys.SelectMany(key => this.Services.GetKeyedServices<T>(key));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

Expand All @@ -24,13 +25,15 @@ public override byte[] ToByteArray()
}

// By default if a native value is not Byte[] we output the UTF8 string representation of the value
return Encoding.UTF8.GetBytes(this.Content?.ToString());
return this.Content?.ToString() is string s ?
Encoding.UTF8.GetBytes(s) :
Array.Empty<byte>();
}

/// <inheritdoc/>
public override string ToString()
{
return this.Content.ToString();
return this.Content.ToString() ?? string.Empty;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,6 @@ private static void ThrowForInvalidSignatureIf([DoesNotReturnIf(true)] bool cond
}
}

/// <summary>Tracks whether a particular kind of parameter has been seen, throwing an exception if it has, and marking it as seen if it hasn't</summary>
private static void TrackUniqueParameterType(ref bool hasParameterType, MethodInfo method, string failureMessage)
{
ThrowForInvalidSignatureIf(hasParameterType, method, failureMessage);
hasParameterType = true;
}

/// <summary>
/// Gets a converter for type to ty conversion. For example, string to int, string to Guid, double to int, CustomType to string, etc.
/// </summary>
Expand Down Expand Up @@ -654,7 +647,7 @@ private static void TrackUniqueParameterType(ref bool hasParameterType, MethodIn
object? Convert(CultureInfo culture)
{
if (converter.CanConvertFrom(input?.GetType()))
if (input?.GetType() is Type type && converter.CanConvertFrom(type))
{
// This line performs string to type conversion
return converter.ConvertFrom(context: null, culture, input);
Expand All @@ -669,13 +662,13 @@ private static void TrackUniqueParameterType(ref bool hasParameterType, MethodIn
// EnumConverter cannot convert integer, so we verify manually
if (targetType.IsEnum &&
(input is int ||
input is uint ||
input is long ||
input is ulong ||
input is short ||
input is ushort ||
input is byte ||
input is sbyte))
input is uint ||
input is long ||
input is ulong ||
input is short ||
input is ushort ||
input is byte ||
input is sbyte))
{
return Enum.ToObject(targetType, input);
}
Expand Down

0 comments on commit 06c6315

Please sign in to comment.