Skip to content

Commit f4e86e4

Browse files
authored
Reduce nullability warnings (#485)
1 parent 8e6c33f commit f4e86e4

20 files changed

+161
-124
lines changed

SDMeta/Auto1111/Auto1111GenerationParams.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace SDMeta.Auto1111
44
{
55
public class Auto1111GenerationParams : GenerationParams
66
{
7-
public string Params { get; set; }
8-
public string Warnings { get; set; }
7+
public string? Params { get; set; }
8+
public string? Warnings { get; set; }
99
}
1010
}

SDMeta/Cache/IImageFileDataSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface IImageFileDataSource : IDisposable
88
IEnumerable<ImageFileSummary> Query(QueryParams queryParams);
99
IEnumerable<string> GetAllFilenames();
1010

11-
ImageFile ReadImageFile(string realFileName);
11+
ImageFile? ReadImageFile(string realFileName);
1212
void WriteImageFile(ImageFile info);
1313
void BeginTransaction();
1414
void CommitTransaction();

SDMeta/Cache/SqliteDataSource.DataRow.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ public partial class SqliteDataSource
77
{
88
private class DataRow
99
{
10-
11-
public string FileName { get; set; }
10+
public required string FileName { get; set; }
1211
public DateTime LastUpdated { get; set; }
1312
public long Length { get; set; }
1413
public bool Exists { get; set; }
1514

16-
public string Prompt { get; set; }
17-
public string PromptFormat { get; set; }
15+
public string? Prompt { get; set; }
16+
public required string PromptFormat { get; set; }
1817

19-
public string ModelHash { get; set; }
20-
public string Model { get; set; }
18+
public string? ModelHash { get; set; }
19+
public string? Model { get; set; }
2120

22-
public string PromptHash { get; set; }
23-
public string NegativePromptHash { get; set; }
21+
public string? PromptHash { get; set; }
22+
public string? NegativePromptHash { get; set; }
2423
public int Version { get; set; }
2524

2625
internal ImageFile ToModel() => new(

SDMeta/Cache/SqliteDataSource.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private SqliteConnection GetConnection()
7373

7474
private T ExecuteOnConnection<T>(Func<SqliteConnection, T> func)
7575
{
76-
if (this.transaction != null)
76+
if (this.transaction?.Connection != null)
7777
{
7878
return func(transaction.Connection);
7979
}
@@ -210,7 +210,7 @@ private static string BuildOrderByClause(QuerySortBy querySort)
210210
};
211211
}
212212

213-
public ImageFile ReadImageFile(string realFileName)
213+
public ImageFile? ReadImageFile(string realFileName)
214214
{
215215
var reader = ExecuteOnConnection(connection => connection.QueryFirstOrDefault<DataRow>(
216216
$@"SELECT *
@@ -268,8 +268,8 @@ public void CommitTransaction()
268268
this.transaction.Commit();
269269
this.transaction.Dispose();
270270
this.transaction = null;
271-
connection.Close();
272-
connection.Dispose();
271+
connection?.Close();
272+
connection?.Dispose();
273273
}
274274
}
275275

SDMeta/CachedImageFileLoader.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55
namespace SDMeta
66
{
77
public class CachedImageFileLoader(
8-
IFileSystem fileSystem,
98
IImageFileLoader inner,
109
IImageFileDataSource imageFileDataSource) : IImageFileLoader
1110
{
12-
public async Task<ImageFile> GetImageFile(string filename)
11+
public async Task<ImageFile> GetImageFile(IFileInfo fileInfo)
1312
{
14-
var fileInfo = fileSystem.FileInfo.New(filename);
15-
var imageFile = imageFileDataSource.ReadImageFile(filename);
13+
var imageFile = imageFileDataSource.ReadImageFile(fileInfo.FullName);
1614
if (imageFile != null && imageFile.LastUpdated == fileInfo.LastWriteTime && imageFile.Exists)
1715
{
1816
return imageFile;
1917
}
2018
else
2119
{
22-
imageFile = await inner.GetImageFile(filename);
20+
imageFile = await inner.GetImageFile(fileInfo);
2321
imageFile.Exists = true;
2422
imageFileDataSource.WriteImageFile(imageFile);
2523
return imageFile;

SDMeta/ComfyUI/ComfyUIParameterDecoder.cs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Microsoft.Extensions.Logging;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text.Json;
@@ -7,7 +8,7 @@
78

89
namespace SDMeta.Comfy
910
{
10-
public class ComfyUIParameterDecoder : IParameterDecoder
11+
public class ComfyUIParameterDecoder(ILogger<ComfyUIParameterDecoder> logger) : IParameterDecoder
1112
{
1213
private static readonly JsonSerializerOptions options = new()
1314
{
@@ -17,10 +18,20 @@ public class ComfyUIParameterDecoder : IParameterDecoder
1718

1819
public GenerationParams GetParameters(ImageFile imageFile)
1920
{
20-
var _parameters = imageFile.Prompt;
21+
if (imageFile.Prompt == null)
22+
{
23+
return GenerationParams.Empty;
24+
}
25+
2126
try
2227
{
23-
var nodes = JsonSerializer.Deserialize<Dictionary<string, UntypedBaseNode>>(_parameters, options);
28+
var nodes = JsonSerializer.Deserialize<Dictionary<string, UntypedBaseNode>>(imageFile.Prompt, options);
29+
30+
if (nodes == null)
31+
{
32+
logger.LogWarning("No nodes found in prompt for file {filename}", imageFile.FileName);
33+
return GenerationParams.Empty;
34+
}
2435

2536
var typedNodes = nodes.Select(p => p.Value.GetInputs(p.Key)).ToList();
2637

@@ -45,6 +56,7 @@ public GenerationParams GetParameters(ImageFile imageFile)
4556
catch (Exception ex)
4657
{
4758
const string errorMessage = "Unable to decode Comfy prompt";
59+
logger.LogError(ex, errorMessage + " for file {filename}", imageFile.FileName);
4860
return new GenerationParams()
4961
{
5062
Model = errorMessage,
@@ -114,37 +126,39 @@ public sealed class CLIPTextEncodeSDXLRefinerNode : UntypedBaseNode<CLIPTextEnco
114126

115127
public class BaseInputs
116128
{
117-
public string NodeId { get; set; }
129+
public string? NodeId { get; set; }
118130
}
119131

120132
public class CheckpointLoaderSimpleInputs : BaseInputs
121133
{
122-
public string ckpt_name { get; set; }
134+
public string? ckpt_name { get; set; }
123135

124136
public string? GetCheckpointName() => ckpt_name?.Replace(".safetensors", "");
125137

126-
public bool IsRefiner() => ckpt_name.ToLower().Contains("refiner");
138+
public bool IsRefiner() =>
139+
ckpt_name != null &&
140+
ckpt_name.Contains("refiner", StringComparison.OrdinalIgnoreCase);
127141
}
128142

129143
public abstract class BaseCLIPTestEncodeInputs : BaseInputs
130144
{
131-
public JsonArray clip { get; set; }
132-
public abstract string GetText();
145+
public JsonArray? clip { get; set; }
146+
public abstract string? GetText();
133147
}
134148

135149
public class CLIPTextEncodeInputs : BaseCLIPTestEncodeInputs
136150
{
137-
public string text { get; set; }
151+
public string? text { get; set; }
138152

139-
public override string GetText() => text;
153+
public override string? GetText() => text;
140154
}
141155

142156
public class KSamplerBase : BaseInputs
143157
{
144-
public JsonArray model { get; set; }
145-
public JsonArray positive { get; set; }
146-
public JsonArray negative { get; set; }
147-
public JsonArray latent_image { get; set; }
158+
public JsonArray? model { get; set; }
159+
public JsonArray? positive { get; set; }
160+
public JsonArray? negative { get; set; }
161+
public JsonArray? latent_image { get; set; }
148162

149163
public (string? positive, string? negative) GetClips(IEnumerable<BaseCLIPTestEncodeInputs> clips)
150164
{
@@ -163,22 +177,22 @@ public class KSamplerInputs : KSamplerBase
163177
public long seed { get; set; }
164178
public int steps { get; set; }
165179
public float cfg { get; set; }
166-
public string sampler_name { get; set; }
167-
public string scheduler { get; set; }
180+
public string? sampler_name { get; set; }
181+
public string? scheduler { get; set; }
168182
public float denoise { get; set; }
169183
}
170184

171185
public class KSamplerAdvancedInputs : KSamplerBase
172186
{
173-
public string add_noise { get; set; }
187+
public string? add_noise { get; set; }
174188
public long noise_seed { get; set; }
175189
public int steps { get; set; }
176190
public float cfg { get; set; }
177-
public string sampler_name { get; set; }
178-
public string scheduler { get; set; }
191+
public string? sampler_name { get; set; }
192+
public string? scheduler { get; set; }
179193
public int start_at_step { get; set; }
180194
public int end_at_step { get; set; }
181-
public string return_with_leftover_noise { get; set; }
195+
public string? return_with_leftover_noise { get; set; }
182196
}
183197

184198
public class CLIPTextEncodeSDXL : BaseCLIPTestEncodeInputs
@@ -189,17 +203,17 @@ public class CLIPTextEncodeSDXL : BaseCLIPTestEncodeInputs
189203
public int crop_h { get; set; }
190204
public int target_width { get; set; }
191205
public int target_height { get; set; }
192-
public string text_g { get; set; }
193-
public string text_l { get; set; }
194-
public override string GetText() => (text_g + " " + text_l).Trim();
206+
public string? text_g { get; set; }
207+
public string? text_l { get; set; }
208+
public override string? GetText() => (text_g + " " + text_l).Trim();
195209
}
196210

197211
public class CLIPTextEncodeSDXLRefiner : BaseCLIPTestEncodeInputs
198212
{
199213
public float ascore { get; set; }
200214
public int width { get; set; }
201215
public int height { get; set; }
202-
public string text { get; set; }
203-
public override string GetText() => text;
216+
public string? text { get; set; }
217+
public override string? GetText() => text;
204218
}
205219
}

SDMeta/FileLister.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace SDMeta
99
{
10-
public class FileLister(IFileSystem fileSystem, ILogger<FileLister> logger) : IFileLister
11-
{
12-
public IEnumerable<string> GetList(string path)
10+
public class FileLister(IFileSystem fileSystem, ILogger<FileLister> logger) : IFileLister
11+
{
12+
public IEnumerable<string> GetList(string path)
1313
{
1414
while (path.EndsWith(fileSystem.Path.DirectorySeparatorChar))
1515
{
@@ -18,8 +18,8 @@ public IEnumerable<string> GetList(string path)
1818

1919
if (fileSystem.Directory.Exists(path) == false)
2020
{
21-
logger.LogError($"{path} does not exist");
22-
return Enumerable.Empty<string>();
21+
logger.LogError("{path} does not exist", path);
22+
return [];
2323
}
2424

2525
var filetypes = new List<string>()
@@ -43,11 +43,11 @@ private string[] GetFileList(string path, string p)
4343
{
4444
IgnoreInaccessible = true,
4545
RecurseSubdirectories = true,
46-
} );
46+
});
4747
}
4848
catch (Exception ex)
4949
{
50-
logger.LogWarning("Unable to scan directory " + path);
50+
logger.LogWarning(ex, "Unable to scan directory {path}", path);
5151
return [];
5252
}
5353
}

SDMeta/GenerationParams.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,31 @@ namespace SDMeta
44
{
55
public class GenerationParams
66
{
7-
public GenerationParams()
7+
public static GenerationParams Empty => new GenerationParams();
8+
9+
public GenerationParams()
810
{
9-
lazyPromptHash = new Lazy<string>(() => GetHash(Prompt));
10-
lazyNegativePromptHash = new Lazy<string>(() => GetHash(NegativePrompt));
11+
lazyPromptHash = new Lazy<string?>(() => GetHash(Prompt));
12+
lazyNegativePromptHash = new Lazy<string?>(() => GetHash(NegativePrompt));
1113
}
1214

13-
private readonly Lazy<string> lazyPromptHash;
14-
private readonly Lazy<string> lazyNegativePromptHash;
15+
private readonly Lazy<string?> lazyPromptHash;
16+
private readonly Lazy<string?> lazyNegativePromptHash;
1517

16-
public string Prompt { get; set; }
17-
public string NegativePrompt { get; set; }
18-
public string ModelHash { get; set; }
19-
public string Model { get; set; }
20-
public string PromptHash => lazyPromptHash.Value;
21-
public string NegativePromptHash => lazyNegativePromptHash.Value;
18+
public string? Prompt { get; set; }
19+
public string? NegativePrompt { get; set; }
20+
public string? ModelHash { get; set; }
21+
public string? Model { get; set; }
22+
public string? PromptHash => lazyPromptHash.Value;
23+
public string? NegativePromptHash => lazyNegativePromptHash.Value;
2224

2325

24-
private string GetHash(string stringToHash)
26+
private string? GetHash(string? stringToHash)
2527
{
26-
if (stringToHash == null) return null;
28+
if (stringToHash == null)
29+
{
30+
return null;
31+
}
2732
return stringToHash.NormalizeString().ComputeSHA256Hash();
2833
}
2934
}

SDMeta/IImageFileLoader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System.Threading.Tasks;
1+
using System.IO.Abstractions;
2+
using System.Threading.Tasks;
23

34
namespace SDMeta
45
{
56
public interface IImageFileLoader
67
{
7-
Task<ImageFile> GetImageFile(string filename);
8+
Task<ImageFile> GetImageFile(IFileInfo fileInfo);
89
}
910
}

SDMeta/ImageFile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ public class ImageFile(string fileName,
66
DateTime lastUpdated,
77
long length,
88
PromptFormat promptFormat,
9-
string prompt,
9+
string? prompt,
1010
bool exists)
1111
{
1212
public string FileName { get; } = fileName;
1313
public DateTime LastUpdated { get; } = lastUpdated;
1414
public long Length { get; } = length;
15-
public string Prompt { get; } = prompt;
15+
public string? Prompt { get; } = prompt;
1616
public PromptFormat PromptFormat { get; } = promptFormat;
1717
/// <summary>
1818
/// Whether the file exists on the most recent scan

0 commit comments

Comments
 (0)