Skip to content

Commit fcd71d6

Browse files
committed
Use partial properties for model generator
1 parent 5f9e944 commit fcd71d6

File tree

9 files changed

+47
-48
lines changed

9 files changed

+47
-48
lines changed

src/DragonFly.App.Server/Models/Customer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ namespace DragonFlyABC;
1111
public partial class Customer
1212
{
1313
[StringField(Required = true, MaxLength = 1024)]
14-
private string? _title;
14+
public partial string? Title { get; set; }
1515

1616
[StringField]
17-
private string? _lastname;
17+
public partial string? Lastname { get; set; }
1818

1919
[StringField]
20-
private string? _firstname;
20+
public partial string? Firstname { get; set; }
2121

2222
}

src/DragonFly.App.Server/Models/Product2.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,44 @@ namespace DragonFlyTEST;
1212
public partial class Product2
1313
{
1414
[StringField(Required = true, Index = true, MaxLength = 1024, ReferenceField = true, ListField = true, QueryField = true)]
15-
private string? _title;
15+
public partial string? Title { get; set; }
1616

1717
[SlugField(Index = true, QueryField = true, TargetField = nameof(Title))]
18-
private SlugField _slug;
18+
public partial SlugField Slug { get; set; }
1919

2020
[BoolField(Index = true, QueryField = true)]
21-
private bool? _isActive;
21+
public partial bool? IsActive { get; set; }
2222

2323
[IntegerField(Index = true, QueryField = true)]
24-
private long? _quantity;
24+
public partial long? Quantity { get; set; }
2525

2626
[FloatField]
27-
private double? _quantity2;
27+
public partial double? Quantity2 { get; set; }
2828

2929
[BlockField]
30-
private BlockField _mainContent;
30+
public partial BlockField MainContent { get; set; }
3131

3232
[AssetField(ShowPreview = true, Index = true, QueryField = true)]
33-
private AssetField _image;
33+
public partial AssetField Image { get; set; }
3434

3535
[AssetField(ShowPreview = true)]
36-
private Asset _image2;
36+
public partial Asset Image2 { get; set; }
3737

3838
[ReferenceField]
39-
private ReferenceField _customerA;
39+
public partial ReferenceField CustomerA { get; set; }
4040

4141
[ReferenceField]
42-
private ContentItem? _customerB;
42+
public partial ContentItem? CustomerB { get; set; }
4343

4444
[ReferenceField]
45-
private Customer? _customerC;
45+
public partial Customer? CustomerC { get; set; }
4646

4747
[GeolocationField]
48-
private GeolocationField _location;
48+
public partial GeolocationField Location { get; set; }
4949

5050
[TextField]
51-
private TextField _text;
51+
public partial TextField Text { get; set; }
5252

5353
[UrlField]
54-
private UrlField _url;
54+
public partial UrlField Url { get; set; }
5555
}

src/DragonFly.App.WebAssembly.Server/Models/Customer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ namespace DragonFlyABC;
1111
public partial class Customer
1212
{
1313
[StringField(Required = true, MaxLength = 1024)]
14-
private string? _title;
14+
public partial string? Title { get; set; }
1515

1616
[StringField]
17-
private string? _lastname;
17+
public partial string? Lastname { get; set; }
1818

1919
[StringField]
20-
private string? _firstname;
20+
public partial string? Firstname { get; set; }
2121

2222
}

src/DragonFly.App.WebAssembly.Server/Models/Product2.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,44 @@ namespace DragonFlyTEST;
1212
public partial class Product2
1313
{
1414
[StringField(Required = true, Index = true, MaxLength = 1024, ReferenceField = true, ListField = true, QueryField = true)]
15-
private string? _title;
15+
public partial string? Title { get; set; }
1616

1717
[SlugField(Index = true, QueryField = true, TargetField = nameof(Title))]
18-
private SlugField _slug;
18+
public partial SlugField Slug { get; set; }
1919

2020
[BoolField(Index = true, QueryField = true)]
21-
private bool? _isActive;
21+
public partial bool? IsActive { get; set; }
2222

2323
[IntegerField(Index = true, QueryField = true)]
24-
private long? _quantity;
24+
public partial long? Quantity { get; set; }
2525

2626
[FloatField]
27-
private double? _quantity2;
27+
public partial double? Quantity2 { get; set; }
2828

2929
[BlockField]
30-
private BlockField _mainContent;
30+
public partial BlockField MainContent { get; set; }
3131

3232
[AssetField(ShowPreview = true, Index = true, QueryField = true)]
33-
private AssetField _image;
33+
public partial AssetField Image { get; set; }
3434

3535
[AssetField(ShowPreview = true)]
36-
private Asset _image2;
36+
public partial Asset Image2 { get; set; }
3737

3838
[ReferenceField]
39-
private ReferenceField _customerA;
39+
public partial ReferenceField CustomerA { get; set; }
4040

4141
[ReferenceField]
42-
private ContentItem? _customerB;
42+
public partial ContentItem? CustomerB { get; set; }
4343

4444
[ReferenceField]
45-
private Customer? _customerC;
45+
public partial Customer? CustomerC { get; set; }
4646

4747
[GeolocationField]
48-
private GeolocationField _location;
48+
public partial GeolocationField Location { get; set; }
4949

5050
[TextField]
51-
private TextField _text;
51+
public partial TextField Text { get; set; }
5252

5353
[UrlField]
54-
private UrlField _url;
54+
public partial UrlField Url { get; set; }
5555
}

src/DragonFly.Core/Modules/ContentModels/Attributes/Fields/BaseFieldAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace DragonFly.Generator;
66

7-
[AttributeUsage(AttributeTargets.Field)]
7+
[AttributeUsage(AttributeTargets.Property)]
88
public abstract class BaseFieldAttribute : Attribute
99
{
1010
public bool Required { get; set; }

src/DragonFly.Generator.Tests/Models/Customer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ namespace DragonFly.Tests;
1010
public partial class Customer
1111
{
1212
[StringField]
13-
private string? _firstname;
13+
public partial string? Firstname { get; set; }
1414

1515
[StringField]
16-
private string? _lastname;
16+
public partial string? Lastname { get; set; }
1717

1818
[StringField]
19-
private string? _street;
19+
public partial string? Street { get;set; }
2020

2121
[SlugField]
22-
private SlugField _slug;
22+
public partial SlugField Slug { get; set; }
2323

2424
[StringField]
25-
private string? _remark;
25+
public partial string? Remark { get; set; }
2626
}

src/DragonFly.Generator.Tests/Models/Product.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace DragonFly.Tests;
1010
public partial class Product
1111
{
1212
[StringField(Required = true, MaxLength = 1024)]
13-
private string? _title;
13+
public partial string? Title { get; set; }
1414

1515
[SlugField]
16-
private SlugField _slug;
16+
public partial SlugField Slug { get;set; }
1717
}

src/DragonFly.Generator/Generators/ModelGenerator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5252
string? contentAttributeParameters = item.ClassSyntax.AttributeLists[0].Attributes[0].ArgumentList?.Arguments.ToString();
5353

5454
//properties of contentitems
55-
foreach (var field in item.ClassSyntax.Members.OfType<FieldDeclarationSyntax>().Where(x => x.AttributeLists.Count > 0))
55+
foreach (var field in item.ClassSyntax.Members.OfType<PropertyDeclarationSyntax>().Where(x => x.AttributeLists.Count > 0))
5656
{
5757
TypeInfo attributeTypeInfo = item.GeneratorContext.SemanticModel.GetTypeInfo(field.AttributeLists[0].Attributes[0]);
5858

@@ -61,7 +61,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
6161
continue;
6262
}
6363

64-
TypeInfo propertyTypeInfo = item.GeneratorContext.SemanticModel.GetTypeInfo(field.Declaration.Type);
64+
TypeInfo propertyTypeInfo = item.GeneratorContext.SemanticModel.GetTypeInfo(field.Type);
6565

6666
if (propertyTypeInfo.Type == null)
6767
{
@@ -70,8 +70,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
7070

7171
string attributeParameters = field.AttributeLists[0].Attributes[0].ArgumentList?.Arguments.ToString() ?? string.Empty;
7272

73-
string fieldName = field.Declaration.Variables[0].Identifier.Text;
74-
string propertyName = fieldName.TrimStart('_').FirstCharToUpper();
73+
string propertyName = field.Identifier.ValueText;
7574

7675
properties.Add(new ContentItemProperty()
7776
{

src/DragonFly.Generator/SourceBuilder/SourceBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ public static SourceBuilder AddContentProperty(this SourceBuilder builder, Conte
183183
builder.AppendTabs();
184184

185185
string propertyType = property.PropertyTypeSymbol.ToDisplayString();
186-
string propertyTypeMaybeNull = property.PropertyTypeSymbol.ToDisplayString(NullableFlowState.MaybeNull);
186+
string propertyTypeMaybeNull = property.PropertyTypeSymbol.ToDisplayString(property.PropertyTypeSymbol.NullableAnnotation == NullableAnnotation.Annotated ? NullableFlowState.MaybeNull : NullableFlowState.NotNull);
187187

188188
bool IsSingleValueType = property.PropertyTypeSymbol.IsValueType == true || property.PropertyTypeSymbol.Name == "String";
189189
bool isDirectReferenceField = property.AttributeTypeSymbol.Name == "ReferenceFieldAttribute" && property.PropertyTypeSymbol.Name != "ReferenceField";
190190
bool isDirectAssetField = property.AttributeTypeSymbol.Name == "AssetFieldAttribute" && property.PropertyTypeSymbol.Name != "AssetField";
191191

192-
builder.Append($"public {(IsSingleValueType || isDirectReferenceField || isDirectAssetField ? propertyTypeMaybeNull : propertyType)} {property.PropertyName}");
192+
builder.Append($"public partial {propertyTypeMaybeNull} {property.PropertyName}");
193193
builder.AppendLineBreak();
194194

195195
//primitive type or string?

0 commit comments

Comments
 (0)