-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
190 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
samples/LoggerSample.LoggerIndependentLibrary/SourceReferenceTarget.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
samples/LoggerSample.LoggerIndependentProject/SourceReferenceTarget.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/dotnetCampus.Logger.Analyzer/Utils/CodeAnalysis/AnalyzerConfigOptionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace dotnetCampus.Logger.Utils.CodeAnalysis; | ||
|
||
internal static class AnalyzerConfigOptionsExtensions | ||
{ | ||
public static AnalyzerConfigOptionResult TryGetValue<T>( | ||
this AnalyzerConfigOptions options, | ||
string key, | ||
out T value) | ||
where T : notnull | ||
{ | ||
if (options.TryGetValue($"build_property.{key}", out var stringValue)) | ||
{ | ||
value = ConvertFromString<T>(stringValue); | ||
return new AnalyzerConfigOptionResult(options, true) | ||
{ | ||
UnsetPropertyNames = [], | ||
}; | ||
} | ||
|
||
value = default!; | ||
return new AnalyzerConfigOptionResult(options, false) | ||
{ | ||
UnsetPropertyNames = [key], | ||
}; | ||
} | ||
|
||
public static AnalyzerConfigOptionResult TryGetValue<T>( | ||
this AnalyzerConfigOptionResult builder, | ||
string key, | ||
out T value) | ||
where T : notnull | ||
{ | ||
var options = builder.Options; | ||
|
||
if (options.TryGetValue($"build_property.{key}", out var stringValue)) | ||
{ | ||
value = ConvertFromString<T>(stringValue); | ||
return builder.Link(true, key); | ||
} | ||
|
||
value = default!; | ||
return builder.Link(false, key); | ||
} | ||
|
||
private static T ConvertFromString<T>(string value) | ||
{ | ||
if (typeof(T) == typeof(string)) | ||
{ | ||
return (T)(object)value; | ||
} | ||
if (typeof(T) == typeof(bool)) | ||
{ | ||
return (T)(object)value.Equals("true", StringComparison.OrdinalIgnoreCase); | ||
} | ||
return default!; | ||
} | ||
} | ||
|
||
public readonly record struct AnalyzerConfigOptionResult(AnalyzerConfigOptions Options, bool GotValue) | ||
{ | ||
public required ImmutableList<string> UnsetPropertyNames { get; init; } | ||
|
||
public AnalyzerConfigOptionResult Link(bool result, string propertyName) | ||
{ | ||
if (result) | ||
{ | ||
return this; | ||
} | ||
|
||
if (propertyName is null) | ||
{ | ||
throw new ArgumentNullException(nameof(propertyName), @"The property name must be specified if the result is false."); | ||
} | ||
|
||
return this with | ||
{ | ||
GotValue = false, | ||
UnsetPropertyNames = UnsetPropertyNames.Add(propertyName), | ||
}; | ||
} | ||
|
||
public static implicit operator bool(AnalyzerConfigOptionResult result) => result.GotValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters