Skip to content

Commit 4dead35

Browse files
🔧 Fix(Kscript): 修复 Device/Function/Plugin 服务类的可空引用类型编译警告
1 parent 639b79d commit 4dead35

8 files changed

Lines changed: 24 additions & 27 deletions

File tree

KitX Script/Kscript.CSharp.Parser/CodeGen/MethodEmitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public CollectibleAssemblyLoadContext(string name) : base(name, isCollectible: t
2020
protected override Assembly Load(AssemblyName assemblyName)
2121
{
2222
// 让默认上下文处理核心程序集加载
23-
return null;
23+
return null!;
2424
}
2525
}
2626

KitX Script/Kscript.CSharp.Parser/Kscript.CSharp.Parser.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Serilog" Version="4.2.0" />
13-
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
1413
</ItemGroup>
1514

1615
<ItemGroup>

KitX Script/Kscript.CSharp/Services/Device.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public Device(DeviceInfo info, Connector? connector = null)
2222
public async Task<IPlugin> RequestPlugin(string Name)
2323
{
2424
var plugins = await GetPluginList();
25-
var pluginInfo = plugins.FirstOrDefault(p =>
25+
var pluginInfo = plugins.FirstOrDefault(p =>
2626
p.Name.Equals(Name, StringComparison.OrdinalIgnoreCase));
27-
28-
return pluginInfo != null ? await CreatePluginInstance(pluginInfo) : null;
27+
28+
return pluginInfo != null ? await CreatePluginInstance(pluginInfo) : null!;
2929
}
3030

3131
public async Task<IEnumerable<PluginInfo>> GetPluginList()
@@ -50,15 +50,15 @@ void OnResponse(Request response)
5050

5151
// With the following code
5252
var plugins = JsonSerializer.Deserialize<List<PluginInfo>>(content);
53-
53+
5454
// Update cache
55-
_pluginCache = plugins.ToDictionary(
55+
_pluginCache = plugins!.ToDictionary(
5656
p => p.Name,
5757
p => p,
5858
StringComparer.OrdinalIgnoreCase
5959
);
6060

61-
tcs.SetResult(plugins);
61+
tcs.SetResult(plugins!);
6262
return content;
6363
}),
6464
matchCommand: content =>
@@ -72,15 +72,15 @@ void OnResponse(Request response)
7272
{
7373
var pluginListJson = content.Substring("PluginList:".Length);
7474
var plugins = JsonSerializer.Deserialize<List<PluginInfo>>(pluginListJson);
75-
75+
7676
// Update cache
77-
_pluginCache = plugins.ToDictionary(
77+
_pluginCache = plugins!.ToDictionary(
7878
p => p.Name,
7979
p => p,
8080
StringComparer.OrdinalIgnoreCase
8181
);
8282

83-
tcs.SetResult(plugins);
83+
tcs.SetResult(plugins!);
8484
}
8585
}
8686
);

KitX Script/Kscript.CSharp/Services/Function.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ void OnResponse(Request response)
4848
{
4949
if (string.IsNullOrEmpty(content))
5050
{
51-
tcs.SetResult(null);
51+
tcs.SetResult(null!);
5252
return content;
5353
}
5454

5555
// Parse response based on return type
5656
var returnType = GetReturnType();
5757
var result = ParseResponse(content, returnType);
58-
tcs.SetResult(result);
58+
tcs.SetResult(result!);
5959
return content;
6060
}),
6161
matchCommand: content =>
@@ -70,7 +70,7 @@ void OnResponse(Request response)
7070
var resultJson = content.Substring("Result:".Length);
7171
var returnType = GetReturnType();
7272
var result = ParseResponse(resultJson, returnType);
73-
tcs.SetResult(result);
73+
tcs.SetResult(result!);
7474
}
7575
}
7676
);
@@ -120,7 +120,7 @@ public Type GetReturnType()
120120
}
121121
}
122122

123-
private object ParseResponse(string content, Type returnType)
123+
private object? ParseResponse(string content, Type returnType)
124124
{
125125
if (string.IsNullOrEmpty(content))
126126
return null;

KitX Script/Kscript.CSharp/Services/Plugin.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public Plugin(PluginInfo info, DeviceInfo deviceInfo, Connector? connector = nul
1919
_connector = connector ?? Connector.Instance;
2020
}
2121

22-
public async Task<IFunction> RequestFunction(string Name)
22+
public async Task<IFunction?> RequestFunction(string Name)
2323
{
2424
var functions = await GetFunctionList();
25-
var function = functions.FirstOrDefault(f =>
25+
var function = functions.FirstOrDefault(f =>
2626
f.Info.Name.Equals(Name, StringComparison.OrdinalIgnoreCase));
2727
return function;
2828
}
@@ -69,7 +69,7 @@ void OnResponse(Request response)
6969
{
7070
var returnType = GetFunctionReturnType(functionName);
7171
var result = ParseFunctionResponse(content, returnType);
72-
tcs.SetResult(result);
72+
tcs.SetResult(result!);
7373
return content;
7474
}),
7575
matchCommand: content =>
@@ -84,7 +84,7 @@ void OnResponse(Request response)
8484
var resultJson = content.Substring("Result:".Length);
8585
var returnType = GetFunctionReturnType(functionName);
8686
var result = ParseFunctionResponse(resultJson, returnType);
87-
tcs.SetResult(result);
87+
tcs.SetResult(result!);
8888
}
8989
}
9090
);
@@ -140,7 +140,7 @@ private Type GetFunctionReturnType(string functionName)
140140
}
141141
}
142142

143-
private object ParseFunctionResponse(string content, Type returnType)
143+
private object? ParseFunctionResponse(string content, Type returnType)
144144
{
145145
if (string.IsNullOrEmpty(content))
146146
return null;

KitX Script/Kscript.CSharp/Utils/DeviceCache.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class DeviceCache : IDisposable
1010
{
1111
private class CacheEntry<T>
1212
{
13-
public T Value { get; set; }
13+
public required T Value { get; set; }
1414
public DateTime Expiration { get; set; }
1515
public DateTime CreatedAt { get; } = DateTime.UtcNow;
1616
private int _accessCount;
@@ -72,8 +72,7 @@ public void Set(string key, IEnumerable<DeviceInfo> value, TimeSpan? expiration
7272
/// </summary>
7373
public IEnumerable<DeviceInfo>? Get(string key)
7474
{
75-
var entry = _cache.GetOrAdd(key, _ => null);
76-
if (entry == null || entry.IsExpired)
75+
if (!_cache.TryGetValue(key, out var entry) || entry.IsExpired)
7776
{
7877
_cache.TryRemove(key, out _);
7978
return null;
@@ -88,8 +87,7 @@ public void Set(string key, IEnumerable<DeviceInfo> value, TimeSpan? expiration
8887
/// </summary>
8988
public bool IsValid(string key)
9089
{
91-
var entry = _cache.GetOrAdd(key, _ => null);
92-
return entry != null && !entry.IsExpired;
90+
return _cache.TryGetValue(key, out var entry) && !entry.IsExpired;
9391
}
9492

9593
/// <summary>

KitX Script/Kscript.CSharp/Utils/DeviceRequestBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public DeviceRequestBuilder Clone()
127127
clone._command = new Command
128128
{
129129
FunctionName = _command.FunctionName,
130-
FunctionArgs = _command.FunctionArgs?.ToList()
130+
FunctionArgs = _command.FunctionArgs?.ToList() ?? new()
131131
};
132132
clone._request = new Request
133133
{

KitX Script/Kscript.CSharp/Utils/ResponseHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static async Task<T> HandleResponse<T>(
3737
result = commandHandler(command);
3838
}
3939
);
40-
return result;
40+
return result!;
4141
}
4242
catch (Exception ex)
4343
{

0 commit comments

Comments
 (0)