Skip to content

Commit 5ffe65c

Browse files
authored
Feature Allow Default value on OAPH generator (#28)
1 parent bc0db32 commit 5ffe65c

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ using ReactiveUI.SourceGenerators;
8181
public partial class MyReactiveClass : ReactiveObject
8282
{
8383
[ObservableAsProperty]
84-
private string _myProperty;
84+
private string _myProperty = "Default Value";
85+
86+
public MyReactiveClass()
87+
{
88+
_myPrpertyHelper = MyPropertyObservable()
89+
.ToProperty(this, x => x.MyProperty);
90+
}
91+
92+
IObservable<string> MyPropertyObservable() => Observable.Return("Test Value");
8593
}
8694
```
8795

src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public partial class TestViewModel : ReactiveObject
2121
[JsonInclude]
2222
[DataMember]
2323
[ObservableAsProperty]
24-
private double _test2Property;
24+
private double _test2Property = 1.1d;
2525

2626
[JsonInclude]
2727
[Reactive]
@@ -53,10 +53,11 @@ public TestViewModel()
5353
Test6ArgOnlyCommand?.Execute("Hello World").Subscribe();
5454
Test7ObservableCommand?.Execute().Subscribe();
5555

56+
Console.Out.WriteLine($"Test2Property default Value: {Test2Property}");
5657
_test2PropertyHelper = Test8ObservableCommand!.ToProperty(this, x => x.Test2Property);
5758

5859
Test8ObservableCommand?.Execute(100).Subscribe(Console.Out.WriteLine);
59-
Console.Out.WriteLine($"Test2Property Value: {Test2}");
60+
Console.Out.WriteLine($"Test2Property Value: {Test2Property}");
6061
Console.Out.WriteLine($"Test2Property underlying Value: {_test2Property}");
6162

6263
Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine);

src/ReactiveUI.SourceGenerators/CodeAnalyzers/PropertyToReactiveFieldAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public class PropertyToReactiveFieldAnalyzer : DiagnosticAnalyzer
3434
/// <param name="context">The context.</param>
3535
public override void Initialize(AnalysisContext context)
3636
{
37-
if (context == null)
37+
if (context is null)
3838
{
39-
return;
39+
throw new System.ArgumentNullException(nameof(context));
4040
}
4141

4242
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

src/ReactiveUI.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ internal static class DiagnosticDescriptors
137137
category: typeof(ReactiveCommandGenerator).FullName,
138138
defaultSeverity: DiagnosticSeverity.Error,
139139
isEnabledByDefault: true,
140-
description: "All asynchronous methods annotated with [ReactiveCommand] should return a Task type, to benefit from the additional support provided by ReactiveCommand and ReactiveCommand<T>.",
140+
description: "All asynchronous methods annotated with [ReactiveCommand] should return a Task type, to benefit from the additional support provided by ReactiveCommand.FromTask.",
141141
helpLinkUri: "https://www.reactiveui.net/errors/RXUISG0008");
142142

143143
/// <summary>

src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator.Execute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal static ImmutableArray<MemberDeclarationSyntax> GetPropertySyntax(Proper
6464
getterFieldIdentifierName = propertyInfo.FieldName;
6565
}
6666

67-
var getterArrowExpression = ArrowExpressionClause(ParseExpression($"{getterFieldIdentifierName} = {getterFieldIdentifierName + "Helper"}.Value"));
67+
var getterArrowExpression = ArrowExpressionClause(ParseExpression($"{getterFieldIdentifierName} = ({getterFieldIdentifierName}Helper?.Value ?? {getterFieldIdentifierName})"));
6868

6969
// Prepare the forwarded attributes, if any
7070
var forwardedAttributes =

0 commit comments

Comments
 (0)