Skip to content

Commit

Permalink
feat(UxmlTraitAttribute): remove obsolete DefaultValue property
Browse files Browse the repository at this point in the history
BREAKING CHANGE: UxmlTraitAttribute's obsolete DefaultValue property has been removed. Use field and property initializers instead.
  • Loading branch information
jonisavo committed Mar 12, 2023
1 parent d21e194 commit adb4715
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 46 deletions.
8 changes: 4 additions & 4 deletions Assets/UIComponents.Tests/Roslyn/RoslynTestComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public enum Greetings
[UxmlTrait(Name = "time")]
public double CurrentTime;

[UxmlTrait(DefaultValue = Greetings.Morning)]
public Greetings Greeting;
[UxmlTrait]
public Greetings Greeting = Greetings.Morning;

[UxmlTrait(DefaultValue = true)]
public bool Enabled;
[UxmlTrait]
public bool Enabled = true;
}
}
8 changes: 4 additions & 4 deletions Assets/UIComponents.Tests/Roslyn/RoslynTestVisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace UIComponents.Tests.Roslyn
{
public partial class RoslynTestVisualElement : VisualElement
{
[UxmlTrait(DefaultValue = "None set")]
public string Description;
[UxmlTrait]
public string Description = "None set";

[UxmlTrait]
public int Age;

[UxmlTrait(Name = "timestamp", DefaultValue = -1)]
public long UnixTimestamp;
[UxmlTrait(Name = "timestamp")]
public long UnixTimestamp = -1;

[UxmlTrait]
public float MusicVolume { get; set; } = 1.0f;
Expand Down
10 changes: 2 additions & 8 deletions Assets/UIComponents/Core/UxmlTraitAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace UIComponents
/// [UxmlTrait(Name = "a-color")]
/// public Color Color;
///
/// [UxmlTrait(DefaultValue = 3)]
/// public int Lives;
/// [UxmlTrait]
/// public int Lives = 3;
/// }
///
/// // This generates:
Expand Down Expand Up @@ -53,11 +53,5 @@ public sealed class UxmlTraitAttribute : Attribute
/// Defines a custom UXML name for the trait.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Defines a default value for the trait.
/// </summary>
[Obsolete("Assign a default value using field and property initializers.")]
public object DefaultValue { get; set; }
}
}
Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.dll
Binary file not shown.
Binary file modified Assets/UIComponents/Roslyn/UIComponents.Roslyn.Generation.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ namespace UIComponents
public sealed class UxmlTraitAttribute : Attribute
{
public string? Name { get; set; }

public object? DefaultValue { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext

m_Description.defaultValue = "Description not set.";
m_Lives.defaultValue = 3;
m_MyValue.defaultValue = (Some.Place.Where.Enum.Is.TheEnum) 1;
m_MyValue.defaultValue = Some.Place.Where.Enum.Is.TheEnum.VALUE_B;
element.Description = m_Description.GetValueFromBag(bag, cc);
element.Lives = m_Lives.GetValueFromBag(bag, cc);
element.MyValue = m_MyValue.GetValueFromBag(bag, cc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ public enum TheEnum
public partial class ComponentWithDefaultValueTraits : UIComponent
{
[UxmlTrait(Name = ""description"", DefaultValue = ""Description not set."")]
public string Description;
[UxmlTrait(Name = ""description"")]
public string Description = ""Description not set."";
[UxmlTrait(Name = ""lives"", DefaultValue = 3)]
public int Lives;
[UxmlTrait(Name = ""lives"")]
public int Lives = 3;
[UxmlTrait(Name = ""custom-value"", DefaultValue = Some.Place.Where.Enum.Is.TheEnum.VALUE_B)]
public Some.Place.Where.Enum.Is.TheEnum MyValue;
[UxmlTrait(Name = ""custom-value"")]
public Some.Place.Where.Enum.Is.TheEnum MyValue = Some.Place.Where.Enum.Is.TheEnum.VALUE_B;
}";

return GeneratorTester.Verify<UxmlAugmentGenerator>(source);
Expand All @@ -287,8 +287,8 @@ public partial class FirstTraitClass
public partial class SecondTraitClass
{
[UxmlTrait(DefaultValue = true)]
public bool Enabled;
[UxmlTrait]
public bool Enabled = true;
[UxmlTrait(Name = ""secret"")]
public long SomeValue;
Expand Down Expand Up @@ -327,8 +327,8 @@ public Task Generates_Both_Traits_And_UxmlFactory()
[UxmlName(""AwesomeUxmlName"")]
public partial class ComponentWithUxmlNameAndTraits : UIComponent
{
[UxmlTrait(DefaultValue = true)]
public bool Value;
[UxmlTrait]
public bool Value = true;
}
";
return GeneratorTester.Verify<UxmlAugmentGenerator>(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UIComponents.Roslyn.Generation.Utilities;

namespace UIComponents.Roslyn.Generation.Generators.Uxml
{
Expand All @@ -16,7 +15,6 @@ namespace UIComponents.Roslyn.Generation.Generators.Uxml
public readonly string DefaultValue;

private const string NameArgumentName = "Name";
private const string DefaultValueArgumentName = "DefaultValue";

public TraitDescription(string classMemberName, string uxmlName, INamedTypeSymbol typeSymbol, string defaultValue = null)
{
Expand Down Expand Up @@ -101,21 +99,6 @@ private static string StringToKebabCase(string inputString)
if (!string.IsNullOrEmpty(initializer))
return (uxmlName, initializer);

if (traitArguments.ContainsKey(DefaultValueArgumentName))
{
var defaultValueString = traitArguments[DefaultValueArgumentName].Value.ToString();

var defaultValueObject = traitArguments[DefaultValueArgumentName].Value;

if (!string.IsNullOrEmpty(defaultValueString) && defaultValueObject is string)
defaultValueString = StringUtilities.AddQuotesToString(defaultValueString);
if (!string.IsNullOrEmpty(defaultValueString) && defaultValueObject is bool)
defaultValueString = defaultValueString.ToLower();
if (!string.IsNullOrEmpty(defaultValueString) && typeSymbol.TypeKind == TypeKind.Enum)
defaultValueString = $"({typeSymbol.ToDisplayString()}) {defaultValueString}";
defaultValue = defaultValueString;
}

return (uxmlName, defaultValue);
}

Expand Down

0 comments on commit adb4715

Please sign in to comment.