Skip to content

Commit

Permalink
#2 Fix nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Jun 30, 2022
1 parent f4f8727 commit 570e9c2
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>

<!-- Display name -->
<ApplicationTitle>Maui.BindableProperty.Generator.Demo</ApplicationTitle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ namespace Maui.BindableProperty.Generator.Demo.ViewModels
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private string _firstName;
private string? _firstName;

[ObservableProperty]
private string _lastName;
private string? _lastName;

[ObservableProperty]
private DateTime _birthDate;

[ObservableProperty]
private string _country;

public MainViewModel(){}
private string? _country;

[RelayCommand]
private void LogClicked()
Expand All @@ -28,7 +26,7 @@ private void LogClicked()
System.Diagnostics.Debug.WriteLine(@$"BirthDate -> {this.BirthDate}");
}

partial void OnFirstNameChanged(string value)
partial void OnFirstNameChanged(string? value)
{
System.Diagnostics.Debug.WriteLine("Method OnFirstNameChanged fired");
System.Diagnostics.Debug.WriteLine(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class AutoBindablePropertyGenerator : ISourceGenerator
private readonly List<IImplementation> CustomImplementations = new() { new DefaultValue(), new PropertyChanged(), new DefaultBindingMode() };

private const string attributeText = @"
#nullable enable
using System;
namespace Maui.BindableProperty.Generator.Core
{
Expand All @@ -24,13 +25,13 @@ public sealed class AutoBindableAttribute : Attribute
{
public AutoBindableAttribute(){}
public string PropertyName { get; set; }
public string PropertyName { get; set; } = string.Empty;
public string OnChanged { get; set; }
public string? OnChanged { get; set; }
public string DefaultValue { get; set; }
public string? DefaultValue { get; set; }
public string DefaultBindingMode { get; set; }
public string? DefaultBindingMode { get; set; }
public bool HidesUnderlyingProperty { get; set; } = false;
}
Expand All @@ -53,7 +54,7 @@ private string ProcessClass(INamedTypeSymbol classSymbol, List<IFieldSymbol> fie

var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
var w = new CodeWriter(CodeWriterSettings.CSharpDefault);
using (w.B(@$"namespace {namespaceName}"))
using (w.B("#nullable enable", @$"namespace {namespaceName}"))
{
w._(AutoBindableConstants.AttrGeneratedCodeString);
using (w.B(@$"public partial class {classSymbol.Name}"))
Expand Down Expand Up @@ -111,7 +112,7 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy
using (w.B(@$"set"))
{
w._($@"SetValue({bindablePropertyName}, value);");
this.ProcessBodyStter(w);
this.ProcessBodySetter(w);
}
}
else
Expand All @@ -133,15 +134,15 @@ private string ProcessBindableParameters()
{
var parameters = this.CustomImplementations
.Select(i => i.ProcessBindableParameters())
.Where(x => !string.IsNullOrEmpty(x));
.Where(x => !string.IsNullOrEmpty(x)).ToArray();

return parameters.Any() ? $@",{ string.Join(",", parameters) }" : string.Empty;
return parameters.Length > 0 ? $@",{ string.Join(",", parameters) }" : string.Empty;
}

private void ProcessBodyStter(CodeWriter w)
private void ProcessBodySetter(CodeWriter w)
{
this.CustomImplementations
.ForEach(i => i.ProcessBodyStter(w));
.ForEach(i => i.ProcessBodySetter(w));
}

private void ProcessImplementationLogic(CodeWriter w)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public string ProcessBindableParameters()
return defaultValue != default ? $"defaultBindingMode: {defaultValue}" : default;
}

public void ProcessBodyStter(CodeWriter w)
public void ProcessBodySetter(CodeWriter w)
{
// Not implemented
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public string ProcessBindableParameters()
return defaultValue != null ? $"defaultValue: {defaultValue}" : $"defaultValue: default({fieldType})";
}

public void ProcessBodyStter(CodeWriter w)
public void ProcessBodySetter(CodeWriter w)
{
// Not implemented
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IImplementation
void Initialize(TypedConstant nameProperty, IFieldSymbol fieldSymbol, ISymbol attributeSymbol, INamedTypeSymbol classSymbol);
bool SetterImplemented();
string ProcessBindableParameters();
void ProcessBodyStter(CodeWriter w);
void ProcessBodySetter(CodeWriter w);
void ProcessImplementationLogic(CodeWriter w);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public string ProcessBindableParameters()
});
}

public void ProcessBodyStter(CodeWriter w)
public void ProcessBodySetter(CodeWriter w)
{
// Not implemented
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void EachField<T>(

// Group the fields by class, and generate the source
#pragma warning disable RS1024 // Symbols should be compared for equality
foreach (IGrouping<INamedTypeSymbol, IFieldSymbol> group in receiver.Fields?.GroupBy(f => f.ContainingType))
foreach (IGrouping<INamedTypeSymbol, IFieldSymbol> group in receiver.Fields?.GroupBy(f => f.ContainingType) ?? Enumerable.Empty<IGrouping<INamedTypeSymbol, IFieldSymbol>>())
{
action?.Invoke(attributeSymbol, group);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static TypedConstant GetTypedConstant(
string key)
{
// Get the AutoNotify attribute from the field, and any associated data
var attributeData = fieldSymbol.GetAttributes().Single(ad => ad.AttributeClass.Equals(attributeSymbol, SymbolEqualityComparer.Default));
var attributeData = fieldSymbol.GetAttributes().Single(ad => attributeSymbol.Equals(ad.AttributeClass, SymbolEqualityComparer.Default));
return attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == key).Value;
}

Expand Down Expand Up @@ -38,7 +38,7 @@ public static T GetValue<T>(
{

var value = TypedConstant.Value;
if (value.GetType() == typeof(T))
if (value?.GetType() == typeof(T))
{
if (onSuccess != null)
{
Expand Down

0 comments on commit 570e9c2

Please sign in to comment.