Skip to content

Commit 7db3895

Browse files
authored
Merge pull request #22 from astar-development/features/sonarcloud-fixes
Add some of the fixes, accept some others
2 parents 4403bb8 + 2037b10 commit 7db3895

File tree

8 files changed

+50
-39
lines changed

8 files changed

+50
-39
lines changed

samples/AStar.Dev.ConsoleSample/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
Option.Some("Jason");
5050
Option.None<string>();
5151
var (isSome, value) = Option.Some("hello");
52+
Console.WriteLine(isSome);
53+
Console.WriteLine(value);
5254

5355
return;
5456

samples/AStar.Dev.SampleApi/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
ex => Results.BadRequest(new { Error = ex.Message }));
3535
});
3636

37-
app.Run();
37+
await app.RunAsync();

samples/AStar.Dev.SampleBlazor/Components/Pages/OptionDemo.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private async Task CheckUsernameAsync()
1919
Timestamp = DateTime.Now
2020
};
2121

22-
var validated = await UserService.TryValidateAsync(userInput);
22+
var validated = await UsernameService.TryValidateAsync(userInput);
2323

2424
pipelineSteps.Validated = validated;
2525
var mapped = validated.Map(name => name.ToUpper());
@@ -54,7 +54,7 @@ private void ToggleDebug()
5454
debugVisible = !debugVisible;
5555
}
5656

57-
private class PipelineLog
57+
private sealed class PipelineLog
5858
{
5959
public string Input { get; set; } = "";
6060
public Option<string>? Validated { get; set; }

samples/AStar.Dev.SampleBlazor/Components/Pages/UsernameService.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ namespace AStar.Dev.SampleBlazor.Components.Pages;
44

55
public class UsernameService
66
{
7-
public Task<Option<string>> TryValidateAsync(string input)
7+
protected UsernameService()
88
{
9-
if (string.IsNullOrWhiteSpace(input))
10-
return Task.FromResult(Option.None<string>());
9+
}
1110

12-
return Task.FromResult(Option.Some(input.Trim()));
11+
public static Task<Option<string>> TryValidateAsync(string input)
12+
{
13+
return Task.FromResult(string.IsNullOrWhiteSpace(input)
14+
? Option.None<string>()
15+
: Option.Some(input.Trim()));
1316
}
1417
}

samples/AStar.Dev.SampleBlazor/Components/Pages/Weather.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ else
5656
}).ToArray();
5757
}
5858

59-
private class WeatherForecast
59+
private sealed class WeatherForecast
6060
{
6161
public DateOnly Date { get; set; }
6262
public int TemperatureC { get; set; }
6363
public string? Summary { get; set; }
6464
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
6565
}
6666

67-
}
67+
}

samples/AStar.Dev.SampleBlazor/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
app.MapRazorComponents<App>()
3232
.AddInteractiveServerRenderMode();
3333

34-
app.Run();
34+
await app.RunAsync();

src/AStar.Dev.Functional.Extensions/OptionExtensions.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,6 @@ public static Option<T> ToOption<T>(this T value)
3535
: new Option<T>.Some(value);
3636
}
3737

38-
/// <summary>
39-
/// Enables deconstruction of an option into a boolean and value pair.
40-
/// </summary>
41-
/// <param name="option"></param>
42-
/// <param name="isSome"></param>
43-
/// <param name="value"></param>
44-
/// <typeparam name="T"></typeparam>
45-
public static void Deconstruct<T>(this Option<T> option, out bool isSome, out T? value)
46-
{
47-
isSome = option is Option<T>.Some;
48-
value = isSome ? ((Option<T>.Some)option).Value : default;
49-
}
50-
5138
/// <summary>
5239
/// Converts a value to an <see cref="Option{T}" /> if it satisfies the predicate.
5340
/// </summary>
@@ -143,4 +130,17 @@ public static T OrThrow<T>(this Option<T> option, Exception? ex = null)
143130
{
144131
return option is Option<T>.Some some ? some.Value : throw ex ?? new InvalidOperationException("No value present");
145132
}
133+
134+
/// <summary>
135+
/// Enables deconstruction of an option into a boolean and value pair.
136+
/// </summary>
137+
/// <param name="option"></param>
138+
/// <param name="isSome"></param>
139+
/// <param name="value"></param>
140+
/// <typeparam name="T"></typeparam>
141+
public static void Deconstruct<T>(this Option<T> option, out bool isSome, out T? value)
142+
{
143+
isSome = option is Option<T>.Some;
144+
value = isSome ? ((Option<T>.Some)option).Value : default;
145+
}
146146
}

src/AStar.Dev.Functional.Extensions/Option{T}.cs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,15 @@ private Option()
1212
{
1313
}
1414

15-
/// <summary>
16-
/// Overrides the ToString method to return both the type and, if present, the value.
17-
/// </summary>
18-
/// <returns></returns>
19-
public override string ToString()
20-
{
21-
return this switch
22-
{
23-
Some some => $"Some({some.Value})",
24-
None => "None",
25-
_ => "Invalid"
26-
};
27-
}
28-
2915
/// <summary>
3016
/// Implicitly converts a value to an <see cref="Option{T}" />.
3117
/// </summary>
3218
/// <param name="value">The value to wrap. Null becomes <see cref="None" />.</param>
3319
public static implicit operator Option<T>(T value)
3420
{
35-
return value != null ? new Some(value) : None.Instance;
21+
return value != null
22+
? new Some(value)
23+
: None.Instance;
3624
}
3725

3826
/// <summary>
@@ -73,6 +61,15 @@ public Some(T value)
7361
/// The wrapped value.
7462
/// </summary>
7563
public T Value { get; }
64+
65+
/// <summary>
66+
/// Overrides the ToString method to return both the type and the value.
67+
/// </summary>
68+
/// <returns>The overridden ToString</returns>
69+
public override string ToString()
70+
{
71+
return $"Some({Value})";
72+
}
7673
}
7774

7875
/// <summary>
@@ -81,12 +78,21 @@ public Some(T value)
8178
public sealed class None : Option<T>
8279
{
8380
/// <summary>
84-
/// A helper method to create an instance of <see cref="None" />
81+
/// A helper method to create an instance of <see cref="Option{T}.None" />
8582
/// </summary>
8683
public static readonly None Instance = new ();
8784

8885
private None()
8986
{
9087
}
88+
89+
/// <summary>
90+
/// Overrides the ToString method to return the type as a simple string.
91+
/// </summary>
92+
/// <returns>The overridden ToString</returns>
93+
public override string ToString()
94+
{
95+
return "None";
96+
}
9197
}
9298
}

0 commit comments

Comments
 (0)