From 97cbc63a03b6d19f5c4705684e81d57da8ef8aca Mon Sep 17 00:00:00 2001 From: Jorge Rangel Date: Fri, 31 May 2024 10:27:09 -0500 Subject: [PATCH 1/2] chore: update fielddeclaration to a class --- .../OutputTypes/ExtensibleEnumTypeProvider.cs | 10 +++--- .../src/OutputTypes/FieldDeclaration.cs | 34 +++++++++++-------- .../src/OutputTypes/FixedEnumTypeProvider.cs | 10 +++--- .../test/CodeWriterTests.cs | 9 ++--- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/ExtensibleEnumTypeProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/ExtensibleEnumTypeProvider.cs index bc5c34be96..e105ea2511 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/ExtensibleEnumTypeProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/ExtensibleEnumTypeProvider.cs @@ -50,11 +50,11 @@ protected override IReadOnlyList BuildMembers() // for initializationValue, if the enum is extensible, we always need it var initializationValue = Literal(inputValue.Value); var field = new FieldDeclaration( - Description: FormattableStringHelpers.FromString(inputValue.Description), - Modifiers: modifiers, - Type: ValueType, - Name: name, - InitializationValue: initializationValue); + modifiers, + ValueType, + name, + FormattableStringHelpers.FromString(inputValue.Description), + initializationValue); values[i] = new EnumTypeMember(valueName, field, inputValue.Value); } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FieldDeclaration.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FieldDeclaration.cs index 9e17fde4cf..1d4e2e98e8 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FieldDeclaration.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FieldDeclaration.cs @@ -6,27 +6,31 @@ namespace Microsoft.Generator.CSharp { - public sealed record FieldDeclaration(FormattableString? Description, FieldModifiers Modifiers, CSharpType Type, string Name, ValueExpression? InitializationValue) + public sealed class FieldDeclaration { + public FormattableString? Description { get; } + public FieldModifiers Modifiers { get; } + public CSharpType Type { get; } + public string Name { get; } + public ValueExpression? InitializationValue { get; } public string Accessibility => (Modifiers & FieldModifiers.Public) > 0 ? "public" : "internal"; private CodeWriterDeclaration? _declaration; public CodeWriterDeclaration Declaration => _declaration ??= new CodeWriterDeclaration(Name); - public FieldDeclaration(FieldModifiers modifiers, CSharpType type, string name) - : this(description: null, - modifiers: modifiers, - type: type, - name: name) - { } - - public FieldDeclaration(FormattableString? description, FieldModifiers modifiers, CSharpType type, string name) - : this(Description: description, - Modifiers: modifiers, - Type: type, - Name: name, - InitializationValue: null) - { } + public FieldDeclaration( + FieldModifiers modifiers, + CSharpType type, + string name, + FormattableString? description = null, + ValueExpression? initializationValue = null) + { + Modifiers = modifiers; + Type = type; + Name = name; + Description = description; + InitializationValue = initializationValue; + } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FixedEnumTypeProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FixedEnumTypeProvider.cs index 293b545631..0813293d0d 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FixedEnumTypeProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FixedEnumTypeProvider.cs @@ -44,11 +44,11 @@ protected override IReadOnlyList BuildMembers() // for fixed enum, we only need it for int values, for other value typed fixed enum, we use the serialization extension method to give the values (because assigning them to enum members cannot compile) var initializationValue = IsIntValueType ? Literal(inputValue.Value) : null; var field = new FieldDeclaration( - Description: FormattableStringHelpers.FromString(inputValue.Description), - Modifiers: modifiers, - Type: ValueType, - Name: name, - InitializationValue: initializationValue); + modifiers, + ValueType, + name, + FormattableStringHelpers.FromString(inputValue.Description), + initializationValue); values[i] = new EnumTypeMember(name, field, inputValue.Value); } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CodeWriterTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CodeWriterTests.cs index 652caca47b..fe0518d5de 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CodeWriterTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/CodeWriterTests.cs @@ -270,12 +270,9 @@ public void TestWriteMethodDeclarationNoScope_ConstructorSignature() [Test] public void CodeWriter_WriteField() { - var field1 = new FieldDeclaration($"To test int", FieldModifiers.Private, typeof(int), "_intConst"); - var field2 = new FieldDeclaration($"To test string", FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, typeof(string), "_stringValue"); - var field3 = new FieldDeclaration($"To test a field with initialization value", FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, typeof(string), "withValue") - { - InitializationValue = Literal("abc") - }; + var field1 = new FieldDeclaration(FieldModifiers.Private, typeof(int), "_intConst", $"To test int"); + var field2 = new FieldDeclaration(FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, typeof(string), "_stringValue", $"To test string"); + var field3 = new FieldDeclaration(FieldModifiers.Private | FieldModifiers.Static | FieldModifiers.ReadOnly, typeof(string), "withValue", $"To test a field with initialization value", Literal("abc")); var codeWriter = new CodeWriter(); codeWriter.WriteField(field1); From 71c93626a6de39da9be8753679e2815597bc0686 Mon Sep 17 00:00:00 2001 From: Jorge Rangel Date: Fri, 31 May 2024 12:57:39 -0500 Subject: [PATCH 2/2] remove unused property --- .../src/OutputTypes/FieldDeclaration.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FieldDeclaration.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FieldDeclaration.cs index 1d4e2e98e8..cb5d96095d 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FieldDeclaration.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/OutputTypes/FieldDeclaration.cs @@ -13,7 +13,6 @@ public sealed class FieldDeclaration public CSharpType Type { get; } public string Name { get; } public ValueExpression? InitializationValue { get; } - public string Accessibility => (Modifiers & FieldModifiers.Public) > 0 ? "public" : "internal"; private CodeWriterDeclaration? _declaration;