Skip to content

Commit bbeaf1a

Browse files
committed
fix(Serialization): Change Dictionary and List to Array Types
JsonSerializer does not support Tuples, which results in null values for the List<(int,int)> also by using JsonSerializable(typeof(List<(int,int)>)
1 parent e8498c5 commit bbeaf1a

File tree

5 files changed

+177
-160
lines changed

5 files changed

+177
-160
lines changed
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
namespace DevTKSS.Uno.Samples.MvuxGallery.Models.CodeSamples;
22
public record CodeSampleOption
33
{
4+
public string SampleID { get; init; } = string.Empty;
45
public string Description { get; init; } = string.Empty;
56
public string FilePath { get; init; } = string.Empty;
6-
public List<(int Start, int End)> LineRanges { get; init; } = [];
7+
public Lines[] LineRanges { get; init; } = [];
78
}
9+
public record Lines
10+
{
11+
public int Start { get; init; }
12+
public int End { get; init; }
13+
}
14+
15+
[JsonSerializable(typeof(CodeSampleOptionsConfiguration))]
816
[JsonSerializable(typeof(CodeSampleOption))]
17+
[JsonSerializable(typeof(ListboardSampleOptions))]
18+
[JsonSerializable(typeof(MainSampleOptions))]
19+
[JsonSerializable(typeof(CounterSampleOptions))]
20+
[JsonSerializable(typeof(SimpleCardsSampleOptions))]
21+
[JsonSerializable(typeof(DashboardSampleOptions))]
922
public partial class CodeSampleOptionContext : JsonSerializerContext
1023
{
1124
}
Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,28 @@
11
namespace DevTKSS.Uno.Samples.MvuxGallery.Models.CodeSamples;
22

3-
public record CodeSampleOptionsConfiguration()
4-
{
5-
public Dictionary<string, CodeSampleOption> Samples { get; init; } = new();
6-
}
7-
[JsonSerializable(typeof(CodeSampleOptionsConfiguration))]
8-
public partial class CodeSampleOptionsConfigurationContext : JsonSerializerContext
9-
{
10-
}
11-
12-
public record ListboardCodeSampleOptions : CodeSampleOptionsConfiguration
13-
{
14-
}
15-
16-
[JsonSerializable(typeof(ListboardCodeSampleOptions))]
17-
public partial class ListboardCodeSampleOptionsContext : JsonSerializerContext
3+
public record CodeSampleOptionsConfiguration
184
{
5+
public CodeSampleOption[] Samples { get; init; } = Array.Empty<CodeSampleOption>();
196
}
207

21-
public record MainCodeSampleOptions : CodeSampleOptionsConfiguration
8+
public record ListboardSampleOptions : CodeSampleOptionsConfiguration
229
{
2310
}
2411

25-
[JsonSerializable(typeof(MainCodeSampleOptions))]
26-
public partial class MainCodeSampleOptionsContext : JsonSerializerContext
12+
public record MainSampleOptions : CodeSampleOptionsConfiguration
2713
{
2814
}
2915

30-
public record CounterCodeSampleOptions : CodeSampleOptionsConfiguration
16+
public record CounterSampleOptions : CodeSampleOptionsConfiguration
3117
{
3218
}
3319

34-
[JsonSerializable(typeof(CounterCodeSampleOptions))]
35-
public partial class CounterCodeSampleOptionsContext : JsonSerializerContext
20+
public record SimpleCardsSampleOptions : CodeSampleOptionsConfiguration
3621
{
3722
}
3823

39-
public record SimpleCardsCodeSampleOptions : CodeSampleOptionsConfiguration
40-
{
41-
}
42-
[JsonSerializable(typeof(SimpleCardsCodeSampleOptions))]
43-
public partial class SimpleCardsCodeSampleOptionsContext : JsonSerializerContext
44-
{
45-
}
4624

47-
public record DashboardCodeSampleOptions : CodeSampleOptionsConfiguration
25+
public record DashboardSampleOptions : CodeSampleOptionsConfiguration
4826
{
4927
}
5028

51-
[JsonSerializable(typeof(DashboardCodeSampleOptions))]
52-
public partial class DashboardCodeSampleOptionsContext : JsonSerializerContext
53-
{
54-
}

src/DevTKSS.Uno.Samples.MvuxGallery/Models/CodeSamples/CodeSampleService.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Microsoft.Extensions.Configuration;
2+
using Windows.Networking.BackgroundTransfer;
23

34
namespace DevTKSS.Uno.Samples.MvuxGallery.Models.CodeSamples;
4-
public record CodeSampleService<SampleOptions> : ICodeSampleService<SampleOptions>
5+
public partial record CodeSampleService<SampleOptions> : ICodeSampleService<SampleOptions>
56
where SampleOptions : CodeSampleOptionsConfiguration
67
{
78
public CodeSampleService(
@@ -10,9 +11,21 @@ public CodeSampleService(
1011
IStorage storage)
1112
{
1213
_options = options.Value;
13-
1414
_logger = logger;
1515
_storage = storage;
16+
if (_logger.IsEnabled(LogLevel.Trace))
17+
{
18+
// Log LineRanges for each sample option
19+
foreach (var sample in _options.Samples)
20+
{
21+
_logger.LogTrace("SampleID: {sampleID},\nDescription: {description},\nFilePath: {filePath},\nLineRanges: {lineRanges}",
22+
sample.SampleID,
23+
sample.Description,
24+
sample.FilePath,
25+
sample.LineRanges);
26+
27+
}
28+
}
1629
}
1730

1831
private readonly IStorage _storage;
@@ -36,24 +49,29 @@ public CodeSampleService(
3649
public async ValueTask<IImmutableList<string>> GetCodeSampleOptionsAsync(CancellationToken ct = default)
3750
{
3851
await Task.Delay(1);
39-
_logger.LogInformation("Options: {options}", _options.Samples);
40-
return _options.Samples.Keys.ToImmutableList();
52+
var sampleOptions = _options.Samples.Select(sample => sample.SampleID).ToImmutableList();
53+
_logger.LogInformation("Options:\n{options}", sampleOptions.JoinBy("," + Environment.NewLine));
54+
return sampleOptions;
4155
}
4256

4357
public async ValueTask<string> GetCodeSampleAsync(string? sampleID, CancellationToken ct = default)
4458
{
45-
if (sampleID is not null && _options.Samples.TryGetValue(sampleID, out CodeSampleOption? sampleOption))
59+
if (_options.Samples.FirstOrDefault(sample => sample.SampleID == sampleID) is CodeSampleOption sampleOption)
4660
{
47-
return await _storage.ReadLinesFromPackageFile(sampleOption.FilePath, sampleOption.LineRanges);
61+
if(_logger.IsEnabled(LogLevel.Trace))
62+
{
63+
_logger.LogTrace("SampleID: {sampleID},\nDescription: {description},\nFilePath: {filePath},\nLineRanges: {lineRanges}",
64+
sampleOption.SampleID,
65+
sampleOption.Description,
66+
sampleOption.FilePath,
67+
sampleOption.LineRanges);
68+
}
69+
70+
return await _storage.ReadLinesFromPackageFile(sampleOption.FilePath,sampleOption.LineRanges.Select(lr => (lr.Start, lr.End)));
4871
}
72+
4973

5074
_logger.LogWarning("Code sample with ID {sampleID} not found", sampleID);
5175
return string.Empty;
5276
}
53-
public async ValueTask<string> SwitchCodeSampleAsync(string? selectedSampleOption, CancellationToken ct = default)
54-
{
55-
string sample = await GetCodeSampleAsync(selectedSampleOption,ct);
56-
_logger.LogInformation("Switched to code sample {sample}", selectedSampleOption);
57-
return sample;
58-
}
5977
}

src/DevTKSS.Uno.Samples.MvuxGallery/Models/CodeSamples/ICodeSampleService.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,4 @@ public interface ICodeSampleService<SampleOptions> where SampleOptions : class
1717
/// <param name="ct">A cancellation token for the operation.</param>
1818
/// <returns>A list of available code sample options.</returns>
1919
ValueTask<IImmutableList<string>> GetCodeSampleOptionsAsync(CancellationToken ct = default);
20-
21-
22-
/// <summary>
23-
/// Switches the current code sample to the specified option asynchronously.
24-
/// </summary>
25-
/// <param name="selectedSampleOption">The selected code sample option to switch to.</param>
26-
/// <param name="ct">A cancellation token for the operation.</param>
27-
/// <returns>
28-
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation, with the content of the switched code sample as a string.
29-
/// </returns>
30-
ValueTask<string> SwitchCodeSampleAsync(string? selectedSampleOption, CancellationToken ct = default);
3120
}

0 commit comments

Comments
 (0)