From e4af5305a01dff23e692036265d927b30173f368 Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Mon, 12 Aug 2024 00:24:27 +0100 Subject: [PATCH] Feature Allow Default value on OAPH generator --- README.md | 10 +++++++++- .../TestViewModel.cs | 5 +++-- .../CodeAnalyzers/PropertyToReactiveFieldAnalyzer.cs | 11 ++++++++--- .../Diagnostics/DiagnosticDescriptors.cs | 2 +- .../ObservableAsPropertyGenerator.Execute.cs | 2 +- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b4e6cc7..0c5fb3a 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,15 @@ using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [ObservableAsProperty] - private string _myProperty; + private string _myProperty = "Default Value"; + + public MyReactiveClass() + { + _myPrpertyHelper = MyPropertyObservable() + .ToProperty(this, x => x.MyProperty); + } + + IObservable MyPropertyObservable() => Observable.Return("Test Value"); } ``` diff --git a/src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs b/src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs index 1f34edb..d65e054 100644 --- a/src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs +++ b/src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs @@ -21,7 +21,7 @@ public partial class TestViewModel : ReactiveObject [JsonInclude] [DataMember] [ObservableAsProperty] - private double _test2Property; + private double _test2Property = 1.1d; [JsonInclude] [Reactive] @@ -53,10 +53,11 @@ public TestViewModel() Test6ArgOnlyCommand?.Execute("Hello World").Subscribe(); Test7ObservableCommand?.Execute().Subscribe(); + Console.Out.WriteLine($"Test2Property default Value: {Test2Property}"); _test2PropertyHelper = Test8ObservableCommand!.ToProperty(this, x => x.Test2Property); Test8ObservableCommand?.Execute(100).Subscribe(Console.Out.WriteLine); - Console.Out.WriteLine($"Test2Property Value: {Test2}"); + Console.Out.WriteLine($"Test2Property Value: {Test2Property}"); Console.Out.WriteLine($"Test2Property underlying Value: {_test2Property}"); Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine); diff --git a/src/ReactiveUI.SourceGenerators/CodeAnalyzers/PropertyToReactiveFieldAnalyzer.cs b/src/ReactiveUI.SourceGenerators/CodeAnalyzers/PropertyToReactiveFieldAnalyzer.cs index a17b9a8..c6fb054 100644 --- a/src/ReactiveUI.SourceGenerators/CodeAnalyzers/PropertyToReactiveFieldAnalyzer.cs +++ b/src/ReactiveUI.SourceGenerators/CodeAnalyzers/PropertyToReactiveFieldAnalyzer.cs @@ -34,9 +34,14 @@ public class PropertyToReactiveFieldAnalyzer : DiagnosticAnalyzer /// The context. public override void Initialize(AnalysisContext context) { - context?.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); - context?.EnableConcurrentExecution(); - context?.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.PropertyDeclaration); + if (context is null) + { + throw new System.ArgumentNullException(nameof(context)); + } + + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); + context.EnableConcurrentExecution(); + context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.PropertyDeclaration); } private void AnalyzeNode(SyntaxNodeAnalysisContext context) diff --git a/src/ReactiveUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs b/src/ReactiveUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs index 5a5ec5f..a9172da 100644 --- a/src/ReactiveUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs +++ b/src/ReactiveUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs @@ -137,7 +137,7 @@ internal static class DiagnosticDescriptors category: typeof(ReactiveCommandGenerator).FullName, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true, - description: "All asynchronous methods annotated with [ReactiveCommand] should return a Task type, to benefit from the additional support provided by ReactiveCommand and ReactiveCommand.", + description: "All asynchronous methods annotated with [ReactiveCommand] should return a Task type, to benefit from the additional support provided by ReactiveCommand.FromTask.", helpLinkUri: "https://www.reactiveui.net/errors/RXUISG0008"); /// diff --git a/src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator.Execute.cs b/src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator.Execute.cs index c4ea3fb..89505ff 100644 --- a/src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator.Execute.cs +++ b/src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator.Execute.cs @@ -64,7 +64,7 @@ internal static ImmutableArray GetPropertySyntax(Proper getterFieldIdentifierName = propertyInfo.FieldName; } - var getterArrowExpression = ArrowExpressionClause(ParseExpression($"{getterFieldIdentifierName} = {getterFieldIdentifierName + "Helper"}.Value")); + var getterArrowExpression = ArrowExpressionClause(ParseExpression($"{getterFieldIdentifierName} = ({getterFieldIdentifierName}Helper?.Value ?? {getterFieldIdentifierName})")); // Prepare the forwarded attributes, if any var forwardedAttributes =