Skip to content

Commit

Permalink
chore: update fielddeclaration to a class (#3492)
Browse files Browse the repository at this point in the history
Follow up to #3450. This PR
updates the `FieldDeclaration` from a record to a class to follow the
pattern set by PropertyDeclaration.
  • Loading branch information
jorgerangel-msft authored Jun 3, 2024
1 parent 12344d1 commit 085129c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected override IReadOnlyList<EnumTypeMember> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@

namespace Microsoft.Generator.CSharp
{
public sealed record FieldDeclaration(FormattableString? Description, FieldModifiers Modifiers, CSharpType Type, string Name, ValueExpression? InitializationValue)
public sealed class FieldDeclaration
{
public string Accessibility => (Modifiers & FieldModifiers.Public) > 0 ? "public" : "internal";
public FormattableString? Description { get; }
public FieldModifiers Modifiers { get; }
public CSharpType Type { get; }
public string Name { get; }
public ValueExpression? InitializationValue { get; }

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ protected override IReadOnlyList<EnumTypeMember> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 085129c

Please sign in to comment.