Merge pull request #13 from nventive/dev/cdb/immutable-generateequalitybydefault
Added new global option `GenerateEqualityByDefault`
This commit is contained in:
Коммит
9b9217c158
|
@ -241,11 +241,31 @@ The generation logic for fields/properties in the class, using a first-match rul
|
|||
|
||||
# FAQ
|
||||
|
||||
## I'm getting the error `warning CS0282: There is no defined ordering between fields
|
||||
## in multiple declarations of partial struct '<mystruct>'. To specify an ordering, all
|
||||
## instance fields must be in the same declaration.`
|
||||
You have this error because the generator is using partial declarations for a `struct`.
|
||||
## Why am I getting the `warning CS0282`?
|
||||
The complete warning message is :
|
||||
```
|
||||
warning CS0282: There is no defined ordering between fields in multiple declarations of partial struct '<mystruct>'. To specify an ordering, all instance fields must be in the same declaration.
|
||||
```
|
||||
You have this warning because the generator is using partial declarations for a `struct`.
|
||||
If the layout is not important for your structs (mostly used for native interop), you
|
||||
can mute this warning in your project.
|
||||
|
||||
You can also consider replacing the `struct` by a `class`.
|
||||
|
||||
## Are equality generation automatic for generated immutables?
|
||||
Yes they are by default. If you want to chagne this behavior, use the global
|
||||
`[ImmutableGenerationOptions]` attribute. Example:
|
||||
``` csharp
|
||||
[assembly: Uno.ImmutableGenerationOptions(GenerateEqualityByDefault = true)]
|
||||
```
|
||||
|
||||
You can also override this default by specifying per-type:
|
||||
```csharp
|
||||
[GeneratedImmutable(GenerateEquality = false)]
|
||||
public class MyImmutable
|
||||
{
|
||||
}
|
||||
```
|
||||
> GOOD TO KNOW: The `[GenerateEquality]` attribute is _inheritable_. It means
|
||||
> any inherited class will be generated too, even if they are defined in another
|
||||
> assembly. (Assuming the `Uno.CodeGen` package is used, obviously)
|
||||
|
|
|
@ -335,4 +335,27 @@ by setting this attribute on your assembly:
|
|||
|
||||
``` csharp
|
||||
[assembly: Uno.ImmutableGenerationOptions(TreatArrayAsImmutable = true)]
|
||||
```
|
||||
```
|
||||
|
||||
## Are equality generation automatic for generated immutables?
|
||||
Yes they are by default. If you want to chagne this behavior, use the global
|
||||
`[ImmutableGenerationOptions]` attribute. Example:
|
||||
``` csharp
|
||||
[assembly: Uno.ImmutableGenerationOptions(GenerateEqualityByDefault = true)]
|
||||
```
|
||||
|
||||
You can also override this default by specifying per-type:
|
||||
```csharp
|
||||
[GeneratedImmutable(GenerateEquality = false)]
|
||||
public class MyImmutable
|
||||
{
|
||||
}
|
||||
```
|
||||
> GOOD TO KNOW: Both `[GeneratedImmutable]` and `[GeneratedEquality]` attributes
|
||||
> are _inheritable_. It means > any inherited class will be generated too,
|
||||
> even if they are defined in another assembly
|
||||
> (Assuming the `Uno.CodeGen` package is used, obviously).
|
||||
> So, disabling equality generation
|
||||
> (`[GeneratedImmutable(GenerateEquality = false)]`)
|
||||
> won't have any effect in inherited class if the generation is active on the
|
||||
> base class.
|
||||
|
|
|
@ -21,7 +21,7 @@ using System.ComponentModel.DataAnnotations;
|
|||
using FluentAssertions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
[assembly: Uno.ImmutableGenerationOptions(TreatArrayAsImmutable = true)]
|
||||
[assembly: Uno.ImmutableGenerationOptions(TreatArrayAsImmutable = true, GenerateEqualityByDefault = true)]
|
||||
|
||||
namespace Uno.CodeGen.Tests
|
||||
{
|
||||
|
@ -206,7 +206,7 @@ namespace Uno.CodeGen.Tests
|
|||
}
|
||||
|
||||
[ImmutableAttributeCopyIgnore("RequiredAttribute")]
|
||||
[GeneratedImmutable(GenerateEquality = true)]
|
||||
[GeneratedImmutable()]
|
||||
public abstract partial class MySuperGenericImmutable<T1, T2, T3, T4, T5, T6>
|
||||
where T1: MyImmutableEntity
|
||||
where T2: T1
|
||||
|
|
|
@ -44,7 +44,7 @@ namespace Uno
|
|||
private INamedTypeSymbol _immutableGenerationOptionsAttributeSymbol;
|
||||
|
||||
private bool _generateOptionCode = true;
|
||||
private (bool generateOptionCode, bool treatArrayAsImmutable) _generationOptions;
|
||||
private (bool generateOptionCode, bool treatArrayAsImmutable, bool generateEqualityByDefault) _generationOptions;
|
||||
|
||||
private Regex[] _copyIgnoreAttributeRegexes;
|
||||
|
||||
|
@ -90,10 +90,11 @@ namespace Uno
|
|||
.Select(a => new Regex(a.ConstructorArguments[0].Value.ToString()));
|
||||
}
|
||||
|
||||
private (bool generateOptionCode, bool treatArrayAsImmutable) ExtractGenerationOptions(IAssemblySymbol assembly)
|
||||
private (bool generateOptionCode, bool treatArrayAsImmutable, bool generateEqualityByDefault) ExtractGenerationOptions(IAssemblySymbol assembly)
|
||||
{
|
||||
var generateOptionCode = true;
|
||||
var treatArrayAsImmutable = false;
|
||||
var generateEqualityByDefault = true;
|
||||
|
||||
var attribute = assembly
|
||||
.GetAttributes()
|
||||
|
@ -111,19 +112,24 @@ namespace Uno
|
|||
case nameof(ImmutableGenerationOptionsAttribute.TreatArrayAsImmutable):
|
||||
treatArrayAsImmutable = (bool) argument.Value.Value;
|
||||
break;
|
||||
case nameof(ImmutableGenerationOptionsAttribute.GenerateEqualityByDefault):
|
||||
generateEqualityByDefault = (bool)argument.Value.Value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (generateOptionCode, treatArrayAsImmutable);
|
||||
return (generateOptionCode, treatArrayAsImmutable, generateEqualityByDefault);
|
||||
}
|
||||
|
||||
private bool GetShouldGenerateEquality(AttributeData attribute)
|
||||
{
|
||||
return attribute.NamedArguments
|
||||
.Where(na => na.Key.Equals("GenerateEquality"))
|
||||
.Select(na => (bool) na.Value.Value)
|
||||
var shouldGenerateEquality = attribute.NamedArguments
|
||||
.Where(na => na.Key.Equals(nameof(GeneratedImmutableAttribute.GenerateEquality)))
|
||||
.Select(na => (bool?) na.Value.Value)
|
||||
.FirstOrDefault();
|
||||
|
||||
return shouldGenerateEquality ?? _generationOptions.generateEqualityByDefault;
|
||||
}
|
||||
|
||||
private (bool isBaseType, string baseType, string builderBaseType, bool isImmutablePresent) GetTypeInfo(
|
||||
|
|
|
@ -18,11 +18,36 @@ using System;
|
|||
|
||||
namespace Uno
|
||||
{
|
||||
/// <summary>
|
||||
/// Global settings for [GenerateImmutable] generator.
|
||||
/// </summary>
|
||||
[System.AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = false)]
|
||||
public sealed class ImmutableGenerationOptionsAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// If you want arrays to be treat as immutable in the validation.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Recommendation is to set this to false if you want actually immutable entities.
|
||||
/// </remarks>
|
||||
public bool TreatArrayAsImmutable { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// If you want to generate `Option`-specific Code
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// No effect if not using `Uno.Core`.
|
||||
/// Default to true.
|
||||
/// </remarks>
|
||||
public bool GenerateOptionCode { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// If you want to generate equality by default.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Default is true. Can be overridden by type on the attribute declaration
|
||||
/// `[GenerateImmutable(GenerateEquality=XXX)]`
|
||||
/// </remarks>
|
||||
public bool GenerateEqualityByDefault { get; set; } = true;
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче